Basit Bir Rest Api Client Geliştirelim 7

2023-05-12

Birkaç özelleştirilebilir başlık ayarlama yeteneği ekleyelim. Bu özellik, belirli bir istek için özel HTTP başlıkları ayarlamayı kolaylaştıracaktır.

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;
    }

    public void SetDefaultHeader(string name, string value)
    {
        if (_client.DefaultRequestHeaders.Contains(name))
        {
            _client.DefaultRequestHeaders.Remove(name);
        }

        _client.DefaultRequestHeaders.Add(name, value);
    }

    public void RemoveDefaultHeader(string name)
    {
        _client.DefaultRequestHeaders.Remove(name);
    }

    // ...
    // ...
    // ...
}

Bu kod parçacığı, SetDefaultHeader ve RemoveDefaultHeader adında iki yeni yöntem ekler. SetDefaultHeader yöntemi, belirli bir isme sahip bir HTTP başlığını varsayılan değer olarak ayarlar. Eğer bu isimde bir başlık zaten varsa, önce mevcut başlık kaldırılır. RemoveDefaultHeader yöntemi, belirli bir isme sahip bir HTTP başlığını kaldırır.

Bu özellikler, belirli bir istek için özel HTTP başlıkları ayarlamanızı sağlar. Örneğin, belirli bir API’nin belirli bir özelliğini kullanırken özel bir User-Agent başlığı veya bir Accept-Language başlığı ayarlamak isteyebilirsiniz. Bu tür durumlar için, bu yöntemlerin her ikisi de çok yararlı olabilir.

Bu noktada, HTTP talepleri yapabilen ve yanıtları işleyebilen oldukça sağlam bir HTTP istemci sınıfımız var. Ancak, bir sonraki adım olarak, HTTP taleplerinin sonuçlarını izlemek ve hataları loga kaydetmek için daha gelişmiş bir hata yakalama ve log kaydetme mekanizması ekleyebiliriz. Bunun için, hataların izlenmesini ve loga kaydedilmesini sağlayacak bir hata işleyicisi ekleyelim.

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;
    }

    public void SetDefaultHeader(string name, string value)
    {
        if (_client.DefaultRequestHeaders.Contains(name))
        {
            _client.DefaultRequestHeaders.Remove(name);
        }

        _client.DefaultRequestHeaders.Add(name, value);
    }

    public void RemoveDefaultHeader(string name)
    {
        _client.DefaultRequestHeaders.Remove(name);
    }

    private async Task<T> HandleExceptions<T>(Func<Task<T>> action)
    {
        try
        {
            return await action();
        }
        catch (HttpRequestException ex)
        {
            _logger.LogError(ex, "An HTTP request exception occurred.");
            throw;
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "An unexpected exception occurred.");
            throw;
        }
    }

    // ...
    // ...
}

HandleExceptions metodu, bir işlemi gerçekleştirmeye çalışır ve bu işlem sırasında bir hata oluşursa, bu hatayı loga kaydeder. Bu, HTTP talepleri sırasında ortaya çıkan hataları belgelemek için kullanılabilir.

Daha sonra, bu hata işleyiciyi SendRequestAsync ve diğer HTTP istek metotlarına uygulayabiliriz. Bu, hataların daha iyi izlenmesini ve loga kaydedilmesini sağlar ve hata ayıklama sürecini kolaylaştırır.

Geliştirmeye devam ederken, HandleExceptions fonksiyonunu her bir HTTP talep yöntemine entegre edelim. Bu sayede, her talep hata yakalama mekanizmasından geçer ve hatalar uygun bir şekilde loga kaydedilir.

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;
    }

    public void SetDefaultHeader(string name, string value)
    {
        if (_client.DefaultRequestHeaders.Contains(name))
        {
            _client.DefaultRequestHeaders.Remove(name);
        }

        _client.DefaultRequestHeaders.Add(name, value);
    }

    public void RemoveDefaultHeader(string name)
    {
        _client.DefaultRequestHeaders.Remove(name);
    }

    private async Task<T> HandleExceptions<T>(Func<Task<T>> action)
    {
        try
        {
            return await action();
        }
        catch (HttpRequestException ex)
        {
            _logger.LogError(ex, "An HTTP request exception occurred.");
            throw;
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "An unexpected exception occurred.");
            throw;
        }
    }

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

            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);
        });
    }

    // ...
    // ...
}

Bu değişikliklerle, SendRequestAsync metodu şimdi HandleExceptions fonksiyonunu kullanıyor. Bu, her HTTP talebinin hata yakalama ve loga kaydetme mekanizmasından geçmesini sağlar.

HTTP yanıtlarında hata durumlarını daha iyi ele almak için bir sonraki adımımızı yapalım. Şu anda, bir HTTP yanıtı başarısız durum kodları (4xx veya 5xx) içerdiğinde, sadece durum kodunu loga kaydediyoruz. Ancak, bazen yanıtın içeriği de hata hakkında yararlı bilgiler içerebilir. Bu nedenle, yanıtın içeriğini de loga kaydedelim.

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

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

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

        var responseBody = await response.Content.ReadAsStringAsync();
        if (!response.IsSuccessStatusCode)
        {
            _logger.LogWarning($"Received HTTP {response.StatusCode} for {method} request to {uri}. Response body: {responseBody}");
            throw new HttpRequestException($"Request to {uri} failed with status code {response.StatusCode}. Response body: {responseBody}");
        }

        response.EnsureSuccessStatusCode();

        return JsonConvert.DeserializeObject<T>(responseBody);
    });
}

Bu geliştirmelerle, SendRequestAsync metodu artık başarısız durum kodlarına sahip HTTP yanıtlarının içeriğini de loga kaydediyor.



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 