Esempio n. 1
0
        public async Task <TItem> AddAsync(TItem item, CancellationToken cancellationToken = default)
        {
            try
            {
                using (var client = new snh.HttpClient())
                {
                    var response = await client.PostWithAuthenticationAsync(
                        $"{this.Config.BaseControllerPath}{this.ItemControllerPath}",
                        item.ToJson(),
                        this.AuthenticationHeaderGenerator,
                        this.Logger,
                        cancellationToken).ConfigureAwait(false);

                    if (!response.IsSuccessStatusCode)
                    {
                        throw new RestfulRepositoryHttpClientException("Unsuccessful service call response")
                              {
                                  ErrorResponse = response
                              };
                    }
                    using (var streamReader = new StreamReader(await response.Content.ReadAsStreamAsync().ConfigureAwait(false)))
                        using (var jsonTextReader = new JsonTextReader(streamReader))
                        {
                            cancellationToken.ThrowIfCancellationRequested();
                            return(new JsonSerializer().Deserialize <TItem>(jsonTextReader));
                        }
                }
            }
            catch (Exception ex)
            {
                this.Logger?.LogError(ex, "Failure to AddAsync. Item {Item}", item.ToJson());
                throw;
            }
        }