示例#1
0
        public async Task <TResponseType> SendAsync <TResponseType>(HttpRequestMessageBuilder builder, HttpCompletionOption completionOption, CancellationToken cancellationToken)
        {
            // Add Authorization Header:
            var accessToken = await CreateAccessTokenAsync(cancellationToken).ConfigureAwait(false);

            builder.AddHeader("Authorization", $"Bearer {accessToken}");

            // Build the Request Message:
            var httpRequestMessage = builder.Build();

            // Invoke actions before the Request:
            OnBeforeRequest(httpRequestMessage);

            // Invoke the Request:
            HttpResponseMessage httpResponseMessage = await client
                                                      .SendAsync(httpRequestMessage, completionOption, cancellationToken)
                                                      .ConfigureAwait(false);

            // Invoke actions after the Request:
            OnAfterResponse(httpRequestMessage, httpResponseMessage);

            // Apply the Response Interceptors:
            EvaluateResponse(httpResponseMessage);

            // Now read the Response Content as String:
            string httpResponseContentAsString = await httpResponseMessage.Content
                                                 .ReadAsStringAsync()
                                                 .ConfigureAwait(false);

            // And finally return the Object:
            return(serializer.DeserializeObject <TResponseType>(httpResponseContentAsString));
        }
示例#2
0
        async public Task <LogInStatus> TryLogIn(string logIn, string password)
        {
            var encryptionLogIn       = EncryptionService.Encrypt(logIn, EncryptionPassword);
            var encryptionPassword    = EncryptionService.Encrypt(password, EncryptionPassword);
            var encryptionApiPassword = EncryptionService.Encrypt(ApiPassword, EncryptionPassword);

            var requestbBuilder = new HttpRequestMessageBuilder(HttpMethod.Get, $"{LogInEndpoint}/{LogInToSystem}");
            var request         = requestbBuilder
                                  .AddHeader(LogInHeader, encryptionLogIn)
                                  .AddHeader(PasswordHeader, encryptionPassword)
                                  .AddHeader(ApiHeader, encryptionApiPassword)
                                  .Make();

            var webApiAdress = RegisterKeyReader.GetValueFromLocalMachineRegistry(BuildManagerKeys, WebApiKey);

            using (var api = new ApiService(webApiAdress))
            {
                var response = await api.SendAsync(request).ConfigureAwait(false);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    var result = JsonConvert.DeserializeObject <LogInStatus>(content);
                    return(result);
                }
                else
                {
                    return(LogInStatus.AnotherNotAuthorizationStatus);
                }
            }
        }
示例#3
0
        public async Task <TResponseType[]> SendBatchAsync <TResponseType>(HttpRequestMessageBuilder builder, CancellationToken cancellationToken)
        {
            // Add Authorization Header:
            var accessToken = await CreateAccessTokenAsync(cancellationToken).ConfigureAwait(false);

            builder.AddHeader("Authorization", $"Bearer {accessToken}");

            // Build the Request Message:
            var httpRequestMessage = builder.Build();

            // Invoke actions before the Request:
            OnBeforeRequest(httpRequestMessage);

            // Invoke the Request:
            HttpResponseMessage httpResponseMessage = await client
                                                      .SendAsync(httpRequestMessage, cancellationToken)
                                                      .ConfigureAwait(false);

            // Invoke actions after the Request:
            OnAfterResponse(httpRequestMessage, httpResponseMessage);

            // Apply the Response Interceptors:
            EvaluateResponse(httpResponseMessage);

            var multipart = await httpResponseMessage.Content.ReadAsMultipartAsync(cancellationToken);

            List <TResponseType> result = new List <TResponseType>();

            foreach (var content in multipart.Contents)
            {
                string part = await content.ReadAsStringAsync();

                // This is quite a hack approach, which might or might not work for all scenarios.
                // I am splitting the multipart response into lines, which in turn is skipped until
                // we hit a line with a single "{", which indicates we have found some JSON:
                IEnumerable <string> jsonLines = part.Split('\n').SkipWhile(x => !string.Equals(x.Trim(), "{"));

                // Then we turn the lines into a String again:
                var jsonString = string.Join("\n", jsonLines);

                // So Newtonsoft.JSON can deserialize it again:
                var response = serializer.DeserializeObject <TResponseType>(jsonString);

                // And add it to the result:
                result.Add(response);
            }

            // And finally return the Object:
            return(result.ToArray());
        }
示例#4
0
        public async Task SendAsync(HttpRequestMessageBuilder builder, HttpCompletionOption completionOption, CancellationToken cancellationToken)
        {
            // Add Authorization Header:
            var accessToken = await CreateAccessTokenAsync(cancellationToken).ConfigureAwait(false);

            builder.AddHeader("Authorization", $"Bearer {accessToken}");

            // Build the Request Message:
            var httpRequestMessage = builder.Build();

            // Invoke actions before the Request:
            OnBeforeRequest(httpRequestMessage);

            // Invoke the Request:
            HttpResponseMessage httpResponseMessage = await client.SendAsync(httpRequestMessage, completionOption, cancellationToken).ConfigureAwait(false);

            // Invoke actions after the Request:
            OnAfterResponse(httpRequestMessage, httpResponseMessage);

            // Apply the Response Interceptors:
            EvaluateResponse(httpResponseMessage);
        }