Пример #1
0
        /// <summary>
        /// The post async.
        /// </summary>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <param name="route">
        /// The route.
        /// </param>
        /// <param name="policy">
        /// The policy.
        /// </param>
        /// <typeparam name="T">
        /// Any class
        /// </typeparam>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        public async Task <T> PostAsync <T>(T message, string route, IPolicy <HttpResponseMessage> policy)
            where T : new()
        {
            this.logger.LogInformation($"about to make a GET request to {route}");

            try
            {
                using (var httpClient = this.httpClientFactory.ClientServiceHttpClient(
                           this.apiClientConfiguration.ClientServiceUrl,
                           this.apiClientConfiguration.SurveillanceUserApiAccessToken))
                {
                    var json = JsonConvert.SerializeObject(message);

                    HttpResponseMessage response = null;
                    await policy.ExecuteAsync(
                        async() =>
                    {
                        this.logger.LogInformation("policy about to call post");
                        response = await httpClient
                                   .PostAsync(
                            route,
                            new StringContent(json, Encoding.UTF8, "application/json"))
                        ;
                        this.logger.LogInformation("policy received post response or timed out");
                        return(response);
                    })
                    ;

                    if (response == null || !response.IsSuccessStatusCode)
                    {
                        this.logger.LogWarning(
                            $"unsuccessful repository GET request to {route}. {response?.StatusCode}");

                        return(new T());
                    }

                    var jsonResponse = await response.Content.ReadAsStringAsync();

                    var deserialisedResponse = JsonConvert.DeserializeObject <T>(jsonResponse);

                    if (deserialisedResponse == null)
                    {
                        this.logger.LogError($"was unable to deserialise the response at {route} {jsonResponse}");
                        return(new T());
                    }

                    this.logger.LogInformation($"returning deserialised GET response from {route}");

                    return(deserialisedResponse);
                }
            }
            catch (Exception e)
            {
                this.logger.LogError(e, $"Exception in {nameof(BaseClientServiceApi)}");
            }

            return(new T());
        }
Пример #2
0
        /// <summary>
        /// The get async.
        /// </summary>
        /// <param name="route">
        /// The route.
        /// </param>
        /// <param name="policy">
        /// The policy.
        /// </param>
        /// <typeparam name="T">
        /// any class
        /// </typeparam>
        /// <returns>
        /// The <see cref="Task"/>.
        /// </returns>
        public async Task <T> GetAsync <T>(string route, IPolicy <HttpResponseMessage> policy)
            where T : class
        {
            this.logger.LogInformation($"get request initiating for {route}");

            try
            {
                using (var httpClient = this.httpClientFactory.ClientServiceHttpClient(
                           this.apiClientConfiguration.ClientServiceUrl,
                           this.apiClientConfiguration.SurveillanceUserApiAccessToken))
                {
                    HttpResponseMessage response = null;
                    await policy.ExecuteAsync(async() =>
                    {
                        this.logger.LogInformation($"policy about to call get at {route}");
                        response = await httpClient.GetAsync(route);
                        this.logger.LogInformation($"policy received post response or timed out for {route}");
                        return(response);
                    });

                    if (response == null || !response.IsSuccessStatusCode)
                    {
                        this.logger.LogWarning($"failed get request at {route} {response?.StatusCode}");
                        return(null);
                    }

                    var jsonResponse = await response.Content.ReadAsStringAsync();

                    var deserialisedResponse = JsonConvert.DeserializeObject <T>(jsonResponse);

                    if (deserialisedResponse == null)
                    {
                        this.logger.LogWarning($"had a null deserialised response for {route}");
                    }

                    this.logger.LogInformation($"returning get result from {route}");
                    return(deserialisedResponse);
                }
            }
            catch (Exception e)
            {
                this.logger.LogError(e, $"exception on get request to");
            }

            return(null);
        }