Skip to content

nmilcoff/Xablu.WebApiClient

 
 

Repository files navigation

xablu logo

Xablu.WebApiClient

The Xablu WebApiClient is a C# HTTP library which aims to simplify consuming of Web API services in .NET projects.

🚧 ⚠️ Already using this library?
We have been working on a 2nd version of WebApiClient which is based on Refit and will support GraphQL. This version has may new features. Beware, upgrading from version 1 to version 2 has some breaking changes since it’s not backwards compatible.

Table of contents

  1. How Xablu.WebApiClient Works
  2. Download / Install
  3. Build Status
  4. Key Features
  5. Example
  6. Contributions
  7. Feedback

How Xablu.WebApiClient Works

WebApiClient is an open source library, created and maintained by Xablu. It is currently available for .NET / Xamarin. Through experience, we discovered that any .NET client app that has resilient calls to web services, uses a combination of libraries. Therefore, we built:

  • A REST client that is flexible and has no limitations.
  • A GraphQL client that is powerful and includes a query builder.

webapiclient model

Technical detail

We have taken the time to update this package with all kind of new features including different libraries. Including these libraries come with new technologies that you might not know about. Because of this reason we have made a summary below about each of the technologie explaining their use-case.

Refit:

Refit is the backbone for the REST client. It allows making HTTP/S requests to external services. Any app that uses Refit does not require much effort to start using our code. All the features provided by Refit are also exposed by our library without limitation.

Fussilade:

Fusillade is an HttpClient implementation which allows an app to efficiently schedule / create requests with different priorities. As a user you have the ability to set these priorities when you make a request.

Polly:

Polly is a very flexible resilience and transient-fault-handling library that allow apps to react to certain situations through policies like retry and timeout. This package provides a very simple way of using all common available features. Every HTTP call made has Polly implemented and the user has the option to customize this.

GraphQL.Client:

GraphQL.Client is the base of our GraphQL implementation, which also includes all additions from Fussilade and Polly as well. The coolest thing? We built a query builder that translates your common response models into a query (and it also gives you the results back as C# objects).

Download / Install

The Xablu.WebApiClient is written following the multi-target library approach. Meaning you can simply add the Xablu.WebApiClient package through NuGet. Install the NuGet package into your shared .NET Standard project and ALL Client projects.

  • NuGet: Xablu.WebApiClient
  • PM> Install-Package Xablu.WebApiClient
  • Namespace: using Xablu.WebApiClient

Build Status:

Build status Github tag NuGet MyGet

Key Features

The WebApiClient contains new features with respect to the previous version. The list of key features is depicted below:

REST Client:

  • Based on Refit ✔
  • Implemented Retry and Timeout from Polly ✔
  • Implemented Fusillade’s Priorities ✔

GraphQL Client:

  • Based on GraphQL.Client ✔
  • Implemented Retry and Timeout from Polly ✔
  • Implemented Fusillade’s Priorities ✔
  • Implemented a Custom, Object-Oriented Query Builder ✔

Example

Make sure to check out the Test Console App inside the package. Down here is an example call for connecting with a Web API service through Refit:

Abstract:

Task<TResult> Call<TResult>(Func<T, Task<TResult>> operation, Priority priority, int retryCount, Func<Exception, bool> shouldRetry, int timeout); 

Example implementation:

async Task<IEnumerable<MyModel>> GetModelsItemsAsync(bool forceRefresh = false) 
{
  IWebApiClient<IRefitInterface> webApiClient = WebApiClientFactory.Get<IRefitInterface>("baseURL", defaultHeaders: true);
  var jsonresult = await webApiClient.Call(
      operation: myRefitService => myRefitService.GetData(),
      priority: Priority.UserInitiated,
      retryCount: 2,
      shouldRetry: exception => myShouldRetryCondition(exception),
      timeout: 60); 
}

Down here is an example call for connecting with a web API service through GraphQL:

Abstract:

public static IWebApiClient<T> Get<T>(string baseUrl, bool autoRedirectRequests = true, Func<DelegatingHandler> delegatingHandler = default, IDictionary<string, string> defaultHeaders = default) where T : class

Implementation:

async Task GraphqlAsync()
{
  var defaultHeaders = new Dictionary<string, string>
  {
    ["User-Agent"] = "ExampleUser",
    ["Authorization"] = "Bearer ******"
  };
  IWebApiClient<IGitHubApi> webApiClient = WebApiClientFactory.Get<IGitHubApi>("https://api.github.com", false, default, defaultHeaders);
  var requestForSingleUser = new Request<UserResponseModel>("(login: \"ExampleUser\")");
  await webApiClient.SendQueryAsync(requestForSingleUser);
}

Contributions

All contributions are welcome! If you have a problem, please open an issue. And PRs are also appreciated!

Feedback

Are you using this library? We would love to hear from you! Or do you have any questions or suggestions? You are welcome to discuss it on:

About

The Xablu WebApiClient component is a C# HTTP library which aims to simplify consuming of Web API services in .NET projects.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%