Example #1
0
        public async Task <TItem> UpdateAsync(TItem updated, CancellationToken cancellationToken = default)
        {
            var identity = updated.Identity;

            try
            {
                using (var client = new snh.HttpClient())
                {
                    var response = await client.PutWithAuthenticationAsync(
                        $"{this.Config.BaseControllerPath}{this.ItemControllerPath}/{identity.ToUrlSegment()}",
                        updated.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 UpdateAsync. Item {Identity}", updated.ToJson());
                throw;
            }
        }