diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-10-26 13:09:05 +0000 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-10-26 13:09:05 +0000 |
commit | b75135d8cdb702423d693558ffaec3f025264b98 (patch) | |
tree | 1e7c0d746ac387a0d6b648d4e452169db17debc6 /LibMatrix/Services/HomeserverProviderService.cs | |
parent | Minor cleanup (diff) | |
download | LibMatrix-b75135d8cdb702423d693558ffaec3f025264b98.tar.xz |
split client and server http client for homeservers
Diffstat (limited to 'LibMatrix/Services/HomeserverProviderService.cs')
-rw-r--r-- | LibMatrix/Services/HomeserverProviderService.cs | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/LibMatrix/Services/HomeserverProviderService.cs b/LibMatrix/Services/HomeserverProviderService.cs index c7fa5c3..a43f518 100644 --- a/LibMatrix/Services/HomeserverProviderService.cs +++ b/LibMatrix/Services/HomeserverProviderService.cs @@ -16,23 +16,26 @@ public class HomeserverProviderService { } private static Dictionary<string, SemaphoreSlim> _authenticatedHomeserverSemaphore = new(); - private static Dictionary<string, AuthenticatedHomeserverGeneric> _authenticatedHomeServerCache = new(); + private static Dictionary<string, AuthenticatedHomeserverGeneric> _authenticatedHomeserverCache = new(); private static Dictionary<string, SemaphoreSlim> _remoteHomeserverSemaphore = new(); - private static Dictionary<string, RemoteHomeServer> _remoteHomeServerCache = new(); + private static Dictionary<string, RemoteHomeserver> _remoteHomeserverCache = new(); - public async Task<AuthenticatedHomeserverGeneric> GetAuthenticatedWithToken(string homeserver, string accessToken, - string? proxy = null) { + public async Task<AuthenticatedHomeserverGeneric> GetAuthenticatedWithToken(string homeserver, string accessToken, string? proxy = null) { var sem = _authenticatedHomeserverSemaphore.GetOrCreate(homeserver + accessToken, _ => new SemaphoreSlim(1, 1)); await sem.WaitAsync(); - lock (_authenticatedHomeServerCache) { - if (_authenticatedHomeServerCache.ContainsKey(homeserver + accessToken)) { + lock (_authenticatedHomeserverCache) { + if (_authenticatedHomeserverCache.ContainsKey(homeserver + accessToken)) { sem.Release(); - return _authenticatedHomeServerCache[homeserver + accessToken]; + return _authenticatedHomeserverCache[homeserver + accessToken]; } } - var domain = proxy ?? await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver); + // var domain = proxy ?? (await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver)).client; + + var rhs = await RemoteHomeserver.Create(homeserver); + var serverVersion = await rhs.GetServerVersionAsync(); + AuthenticatedHomeserverGeneric hs; if (true) { @@ -44,15 +47,15 @@ public class HomeserverProviderService { // (() => hs.WhoAmI) = (await hs._httpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami"))!; - lock(_authenticatedHomeServerCache) - _authenticatedHomeServerCache[homeserver + accessToken] = hs; + lock (_authenticatedHomeserverCache) + _authenticatedHomeserverCache[homeserver + accessToken] = hs; sem.Release(); return hs; } - public async Task<RemoteHomeServer> GetRemoteHomeserver(string homeserver, string? proxy = null) { - var hs = await RemoteHomeServer.Create(proxy ?? await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver)); + public async Task<RemoteHomeserver> GetRemoteHomeserver(string homeserver, string? proxy = null) { + var hs = await RemoteHomeserver.Create(proxy ?? homeserver); // hs._httpClient.Dispose(); // hs._httpClient = new MatrixHttpClient { BaseAddress = new Uri(hs.ServerName) }; // hs._httpClient.Timeout = TimeSpan.FromSeconds(120); @@ -65,8 +68,8 @@ public class HomeserverProviderService { Identifier = new LoginRequest.LoginIdentifier { User = user }, Password = password }; - var resp = await hs._httpClient.PostAsJsonAsync("/_matrix/client/v3/login", payload); + var resp = await hs.ClientHttpClient.PostAsJsonAsync("/_matrix/client/v3/login", payload); var data = await resp.Content.ReadFromJsonAsync<LoginResponse>(); return data!; } -} +} \ No newline at end of file |