Skip to content

Instantly share code, notes, and snippets.

@ByteDev
Last active September 22, 2021 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ByteDev/e5a5d656474186bcd05b23857c0b4e7c to your computer and use it in GitHub Desktop.
Save ByteDev/e5a5d656474186bcd05b23857c0b4e7c to your computer and use it in GitHub Desktop.
public interface ICustomerApiClient
{
    Task<SearchCustomerResponse> SearchAsync(SearchCustomerRequest request, CancellationToken cancellationToken = default);
}

// ...

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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment