Skip to content

Instantly share code, notes, and snippets.

@ByteDev
Last active September 22, 2021 06:03

Revisions

  1. ByteDev revised this gist Sep 22, 2021. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions Blog-CustomerApiClient.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,11 @@
    ```csharp
    public interface ICustomerApiClient
    {
    Task<SearchCustomerResponse> SearchAsync(SearchCustomerRequest request, CancellationToken cancellationToken = default);
    }

    // ...
    public class CustomerApiClient : ICustomerApiClient
    {
    private readonly HttpClient _httpClient;
  2. ByteDev revised this gist Sep 22, 2021. 1 changed file with 25 additions and 25 deletions.
    50 changes: 25 additions & 25 deletions Blog-CustomerApiClient.md
    Original file line number Diff line number Diff line change
    @@ -1,34 +1,34 @@
    ```csharp
    public class CustomerApiClient : ICustomerApiClient
    {
    private readonly HttpClient _httpClient;
    private readonly CustomerApiClientConfig _config;
    private readonly RequestFactory _requestFactory;

    public CustomerApiClient(HttpClient httpClient, CustomerApiClientConfig config)
    {
    _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
    _config = config ?? throw new ArgumentNullException(nameof(config));

    _requestFactory = new RequestFactory(config);
    }

    public async Task<SearchCustomerResponse> SearchAsync(SearchCustomerRequest request, CancellationToken cancellationToken = default)
    {
    try
    private readonly HttpClient _httpClient;
    private readonly CustomerApiClientConfig _config;
    private readonly RequestFactory _requestFactory;

    public CustomerApiClient(HttpClient httpClient, CustomerApiClientConfig config)
    {
    var httpRequest = _requestFactory.Create(request, _config.SearchCustomerUri);

    var httpResponse = await _httpClient.SendAsync(httpRequest, cancellationToken);

    return await ResponseHandler.HandleAsync<SearchCustomerResponse>(httpResponse);
    _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
    _config = config ?? throw new ArgumentNullException(nameof(config));

    _requestFactory = new RequestFactory(config);
    }
    catch (Exception ex)

    public async Task<SearchCustomerResponse> SearchAsync(SearchCustomerRequest request, CancellationToken cancellationToken = default)
    {
    throw new CustomerApiClientException("Error occurred while searching for customer.", ex);
    try
    {
    var httpRequest = _requestFactory.Create(request, _config.SearchCustomerUri);

    var httpResponse = await _httpClient.SendAsync(httpRequest, cancellationToken);

    return await ResponseHandler.HandleAsync<SearchCustomerResponse>(httpResponse);
    }
    catch (Exception ex)
    {
    throw new CustomerApiClientException("Error occurred while searching for customer.", ex);
    }
    }
    }

    // ... more methods for API operations

    // ... more methods for API operations
    }
    ```
  3. ByteDev renamed this gist Sep 22, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. ByteDev created this gist Sep 22, 2021.
    34 changes: 34 additions & 0 deletions Blog-CustomerApiClient
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    ```csharp
    public class CustomerApiClient : ICustomerApiClient
    {
    private readonly HttpClient _httpClient;
    private readonly CustomerApiClientConfig _config;
    private readonly RequestFactory _requestFactory;

    public CustomerApiClient(HttpClient httpClient, CustomerApiClientConfig config)
    {
    _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
    _config = config ?? throw new ArgumentNullException(nameof(config));

    _requestFactory = new RequestFactory(config);
    }

    public async Task<SearchCustomerResponse> SearchAsync(SearchCustomerRequest request, CancellationToken cancellationToken = default)
    {
    try
    {
    var httpRequest = _requestFactory.Create(request, _config.SearchCustomerUri);

    var httpResponse = await _httpClient.SendAsync(httpRequest, cancellationToken);

    return await ResponseHandler.HandleAsync<SearchCustomerResponse>(httpResponse);
    }
    catch (Exception ex)
    {
    throw new CustomerApiClientException("Error occurred while searching for customer.", ex);
    }
    }

    // ... more methods for API operations
    }
    ```