Basit Bir Rest Api Client Geliştirelim 6

2023-05-12

HTTP yanıtının durum kodlarını kontrol edebilme yeteneğini ekleyelim. Bu özellik, HTTP isteğinin başarılı olup olmadığını veya belirli bir hata durumunu gösterip göstermediğini belirlememize olanak sağlar.

using Polly;
using Polly.Caching;
using Polly.Caching.Memory;
using Polly.Registry;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

public class MyHttpClient : IDisposable
{
    private readonly HttpClient _client;
    private readonly IPolicyRegistry<string> _policyRegistry;
    private readonly ILogger<MyHttpClient> _logger;

    public MyHttpClient(HttpClient client, IPolicyRegistry<string> policyRegistry, ILogger<MyHttpClient> logger, string baseAddress, TimeSpan? timeout = null, HttpMessageHandler handler = null)
    {
        _client = handler == null ? client : new HttpClient(handler);
        _client.BaseAddress = new Uri(baseAddress);

        if (timeout.HasValue)
        {
            _client.Timeout = timeout.Value;
        }

        _policyRegistry = policyRegistry;
        _logger = logger;
    }

    private HttpRequestMessage CreateRequest(string uri, HttpMethod method, string jsonData = null, Dictionary<string, string> headers = null, TimeSpan? timeout = null)
    {
        var request = new HttpRequestMessage(method, uri);

        if (headers != null)
        {
            foreach (var header in headers)
            {
                request.Headers.Add(header.Key, header.Value);
            }
        }

        if (!string.IsNullOrWhiteSpace(jsonData))
        {
            request.Content = new StringContent(jsonData, Encoding.UTF8, "application/json");
        }

        if (timeout.HasValue)
        {
            _client.Timeout = timeout.Value;
        }

        return request;
    }

    public async Task<HttpResponseMessage> SendRequestAsync(string uri, HttpMethod method, string jsonData = null, Dictionary<string, string> headers = null, TimeSpan? timeout = null)
    {
        var request = CreateRequest(uri, method, jsonData, headers, timeout);

        try
        {
            var policyWrap = Policy.WrapAsync(_policyRegistry.Get<IAsyncPolicy<HttpResponseMessage>>("RetryPolicy"),
                                              _policyRegistry.Get<IAsyncPolicy<HttpResponseMessage>>("CachePolicy"));

            var response = await policyWrap.ExecuteAsync(async () => await _client.SendAsync(request));

            if (!response.IsSuccessStatusCode)
            {
                _logger.LogWarning($"Received HTTP {response.StatusCode} for {method} request to {uri}.");
            }
            
            response.EnsureSuccessStatusCode();
            return response;
        }
        catch (HttpRequestException ex)
        {
            _logger.LogError(ex, $"{method} request to {uri} failed.");
            throw new Exception($"{method} request to {uri} failed.", ex);
        }
    }

    public async Task<string> GetAsync(string uri, Dictionary<string, string> headers = null, TimeSpan? timeout = null) =>
        (await SendRequestAsync(uri, HttpMethod.Get, null, headers, timeout)).Content.ReadAsStringAsync().Result;

    public async Task<string> PostAsync(string uri, string jsonData, Dictionary<string, string> headers = null, TimeSpan? timeout = null) =>
        (await SendRequestAsync(uri, HttpMethod.Post, jsonData, headers, timeout)).Content.ReadAsStringAsync().Result;

    public async Task<string> PutAsync(string uri, string jsonData, Dictionary<string, string> headers = null, TimeSpan? timeout = null) =>
        (await SendRequestAsync(uri, HttpMethod.Put, jsonData, headers, timeout)).Content.ReadAsStringAsync().Result;

    public async Task<string> DeleteAsync(string uri, Dictionary<string, string> headers = null, TimeSpan? timeout = null) =>
        (await SendRequestAsync(uri, HttpMethod.Delete, null, headers, timeout)).Content.ReadAsStringAsync().Result;

    public void SetAuthorizationHeader(string scheme, string parameter)
    {
        _client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(scheme, parameter);
    }

    public void Dispose()
    {
        _client?.Dispose();
    }
}

Bu son değişiklikler, HTTP yanıtının durum kodlarını kontrol edebilmemizi sağlar. IsSuccessStatusCode özelliği, yanıtın durum kodunun başarılı bir durum kodu olup olmadığını kontrol eder (yani, 200-299 arasında). Başarılı olmayan bir durum kodu alındığında, bir uyarı loga kaydedilir ve yanıtın durum kodu ve isteğin URL’si ile birlikte bir hata mesajı oluşturulur.

Bir sonraki adım olarak, JSON verilerini otomatik olarak serileştirme ve çözme yetenekleri ekleyelim. Bu özellik, HTTP isteklerinin gövdesine JSON veri eklemeyi ve HTTP yanıtlarından JSON veri çıkarmayı kolaylaştırır.

Bu amaçla, Newtonsoft.Json paketini kullanacağız.

using Newtonsoft.Json;
using Polly;
using Polly.Caching;
using Polly.Caching.Memory;
using Polly.Registry;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

public class MyHttpClient : IDisposable
{
    private readonly HttpClient _client;
    private readonly IPolicyRegistry<string> _policyRegistry;
    private readonly ILogger<MyHttpClient> _logger;

    public MyHttpClient(HttpClient client, IPolicyRegistry<string> policyRegistry, ILogger<MyHttpClient> logger, string baseAddress, TimeSpan? timeout = null, HttpMessageHandler handler = null)
    {
        _client = handler == null ? client : new HttpClient(handler);
        _client.BaseAddress = new Uri(baseAddress);

        if (timeout.HasValue)
        {
            _client.Timeout = timeout.Value;
        }

        _policyRegistry = policyRegistry;
        _logger = logger;
    }

    private HttpRequestMessage CreateRequest<T>(string uri, HttpMethod method, T content = default, Dictionary<string, string> headers = null, TimeSpan? timeout = null) where T : class
    {
        var request = new HttpRequestMessage(method, uri);

        if (headers != null)
        {
            foreach (var header in headers)
            {
                request.Headers.Add(header.Key, header.Value);
            }
        }

        if (content != null)
        {
            var jsonData = JsonConvert.SerializeObject(content);
            request.Content = new StringContent(jsonData, Encoding.UTF8, "application/json");
        }

        if (timeout.HasValue)
        {
            _client.Timeout = timeout.Value;
        }

        return request;
    }

    public async Task<T> SendRequestAsync<T>(string uri, HttpMethod method, object content = null, Dictionary<string, string> headers = null, TimeSpan? timeout = null) where T : class
    {
        var request = CreateRequest(uri, method, content, headers, timeout);

        try
        {
            var policyWrap = Policy.WrapAsync(_policyRegistry.Get<IAsyncPolicy<HttpResponseMessage>>("RetryPolicy"),
                                              _policyRegistry.Get<IAsyncPolicy<HttpResponseMessage>>("CachePolicy"));

            var response = await policyWrap.ExecuteAsync(async () => await _client.SendAsync(request));

            if (!response.IsSuccessStatusCode)
            {
                _logger.LogWarning($"Received HTTP {response.StatusCode} for {method} request to {uri}.");
            }

            response.EnsureSuccessStatusCode();

            var responseBody = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<T>(responseBody);
        }
        catch (HttpRequestException ex)
        {
            _logger.LogError(ex, $"{method} request to {uri} failed.");
            throw new Exception($"{method} request to {uri} failed.", ex);
        }
    }

    public async Task<T> GetAsync<T>(string uri, Dictionary<string, string> headers = null, TimeSpan? timeout = null) where T : class =>
        await SendRequestAsync<T>(uri, HttpMethod.Get, null, headers, timeout);

    public async Task<T> PostAsync<T>(string uri, object content, Dictionary<string, string> headers = null, TimeSpan? timeout = null) where T : class =>
        await SendRequestAsync<T>(uri, HttpMethod.Post, content, headers, timeout);

    public async Task<T> PutAsync<T>(string uri, object content, Dictionary<string, string> headers = null, TimeSpan? timeout = null) where T : class =>
        await SendRequestAsync<T>(uri, HttpMethod.Put, content, headers, timeout);

    public async Task<T> DeleteAsync<T>(string uri, Dictionary<string, string> headers = null, TimeSpan? timeout = null) where T : class =>
        await SendRequestAsync<T>(uri, HttpMethod.Delete, null, headers, timeout);

    public void SetAuthorizationHeader(string scheme, string parameter)
    {
        _client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(scheme, parameter);
    }

    public void Dispose()
    {
        _client?.Dispose();
    }
}

SendRequestAsync fonksiyonu artık bir dönüş türü parametresi alıyor ve isteğin yanıt gövdesini bu türe çözümlüyor. Aynı şekilde, GetAsync, PostAsync, PutAsync, ve DeleteAsync fonksiyonları da bu özellikten yararlanıyorlar. Bu, HTTP isteklerini gönderirken ve yanıtları alırken JSON verileriyle çalışmayı çok daha kolay hale getirecektir.



More posts like this

The SemaphoreSlim Class: Multitask-Based Programming in C#

2023-06-11 | #net #semaphoreslim

Overview The SemaphoreSlim class is a structure used in C# to control one or more threads using a specific resource or operation concurrently. SemaphoreSlim limits the number of threads that can access the resource at the same time. The use of SemaphoreSlim is often used to prevent deadlock situations in multi-threaded applications and to ensure that a specific resource is used by only one or more threads at the same time.

Continue reading 


LINQ and Optimistic Concurrency: Methods for Ensuring Data Integrity

2023-06-11 | #linq #net #optimistic-concurrency

When working with databases, it is of great importance to properly manage concurrent operations and ensure data integrity. Concurrency control is used to manage multiple users or processes accessing the same data concurrently. In this article, we will explore how to ensure data integrity using LINQ (Language Integrated Query) with optimistic concurrency. What is Optimistic Concurrency? Optimistic concurrency is a method where resources are not locked when a transaction begins, but changes are checked before the transaction is completed.

Continue reading 


LINQ and Pessimistic Concurrency: Methods for Ensuring Data Integrity

2023-06-11 | #linq #net #pessimistic-concurrency

When working with databases, managing concurrent operations and ensuring data integrity are of great importance. Concurrency control is used to manage multiple users or processes accessing the same data concurrently. In this article, we will explore how to use LINQ (Language Integrated Query) with pessimistic concurrency to maintain data integrity. What is Pessimistic Concurrency? Pessimistic concurrency is based on the principle of locking the relevant data when a transaction begins and maintaining the lock until the transaction is completed.

Continue reading 