Ejemplo n.º 1
0
        public async Task <Failable <User> > LoginAsync(Login login)
        {
            const string settingsName = "LoginClientBaseAddress";

            try
            {
                var uri = AssemblyConfig <RegisterClient> .GetValue(settingsName);

                var response = await Client.PostAsync(new Uri(uri), login.ToJsonContent());

                var result = await HandleResponseAsync <User>(response);

                if (result.IsSucceeded())
                {
                    await UpdateCookiesAsync(response);
                }

                await UpdateCurrentUserAsync(result.Value);

                return(result);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(new Failable <User>(e));
            }
        }
Ejemplo n.º 2
0
        /// <inheritdoc />
        public async Task <Failable> LogoutAsync()
        {
            const string settingsName = "LogoutClientBaseAddress";

            try
            {
                var uri = AssemblyConfig <RegisterClient> .GetValue(settingsName);

                var message = await GetHttpMessageWithSessionCookie(uri, HttpMethod.Get);

                var response = await Client.SendAsync(message);

                var result = await HandleResponseAsync <NoReturnValue>(response);

                //Delete anyways -- even if result failed
                await UpdateCookiesAsync(null);
                await UpdateCurrentUserAsync(null);

                if (!result.IsSucceeded())
                {
                    throw new InvalidOperationException(result.ThrownException?.Message ?? "Logout rejected by backend");
                }

                return(result);
            }
            catch (Exception e)
            {
                return(new Failable(e));
            }
        }
Ejemplo n.º 3
0
        public async Task <string> GetUserNameAsync(string userId)
        {
            if (UsernameCache.TryGetValue(userId, out var userName))
            {
                return(userName);
            }

            const string settingsName = "UserClientGetNameBaseAddress";
            const string unknownUser  = "******";

            await Semaphore.WaitAsync();

            try
            {
                //double check
                if (UsernameCache.TryGetValue(userId, out var username))
                {
                    return(username);
                }

                var uri = AssemblyConfig <RegisterClient> .GetValue(settingsName);

                var data = new RequestUsernameData {
                    Id = userId
                };
                var request = new HttpRequestMessage
                {
                    Method     = HttpMethod.Get,
                    RequestUri = new Uri(uri),
                    Content    = data.ToJsonContent(),
                };

                var response = await Client.SendAsync(request);

                var result = await HandleResponseAsync <RequestUsernameData>(response);

                //No errorhandling here (non vital action)
                UsernameCache.TryAdd(userId, result?.Value.Result ?? unknownUser);
                return(result?.Value.Result);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                UsernameCache.TryAdd(userId, unknownUser);
                return(unknownUser);
            }
            finally
            {
                Semaphore.Release();
            }
        }
Ejemplo n.º 4
0
        //// private methods

        private async Task <Failable <T> > PerformBarrierRequest <T>(string settingsName, object requestData, HttpMethod method)
        {
            var uri = AssemblyConfig <BarrierClient> .GetValue(settingsName);

            var message = await GetHttpMessageWithSessionCookie(uri, method);

            message.Content = requestData.ToJsonContent();

            var response = await Client.SendAsync(message);

            var result = await HandleResponseAsync <T>(response);

            return(result);
        }