All Projects → jgiacomini → Tiny.restclient

jgiacomini / Tiny.restclient

Licence: mit
Simpliest Fluent REST client for .NET

Programming Languages

csharp
926 projects

Projects that are alternatives of or similar to Tiny.restclient

Restclient.net
.NET REST Client Framework for all platforms
Stars: ✭ 143 (-9.49%)
Mutual labels:  rest-client, xamarin, uwp
Linqtotwitter
LINQ Provider for the Twitter API (C# Twitter Library)
Stars: ✭ 401 (+153.8%)
Mutual labels:  dotnet-core, xamarin, uwp
Xamarin.animations
Animate your views with simple shareable animation declarations.
Stars: ✭ 103 (-34.81%)
Mutual labels:  xamarin, uwp
Microcharts
This project is just simple drawing on top of the awesome SkiaSharp library. The purpose is not to have an heavily customizable charting library. If you want so, simply fork the code, since all of this is fairly simple. Their is no interaction, nor animation at the moment.
Stars: ✭ 1,653 (+946.2%)
Mutual labels:  xamarin, uwp
Xam.plugin.webview
Xamarin Plugin for a HybridWebView in PCL projects.
Stars: ✭ 132 (-16.46%)
Mutual labels:  xamarin, uwp
Xamarines
🕹️📱Cross-Platform Nintendo Emulator using Xamarin and .Net Standard!
Stars: ✭ 153 (-3.16%)
Mutual labels:  xamarin, uwp
Essentials
Essential cross platform APIs for your mobile apps.
Stars: ✭ 1,344 (+750.63%)
Mutual labels:  xamarin, uwp
Arcgis Toolkit Dotnet
Toolkit for ArcGIS Runtime SDK for .NET
Stars: ✭ 125 (-20.89%)
Mutual labels:  xamarin, uwp
Httptransfertasks
Cross Platform HTTP Transfers for downloading and uploading (supports background operations)
Stars: ✭ 87 (-44.94%)
Mutual labels:  xamarin, uwp
Mvvmvalidation
Lightweight library that helps reduce boilerplate when implementing validation in XAML MVVM applications
Stars: ✭ 141 (-10.76%)
Mutual labels:  xamarin, uwp
Plugin.audiorecorder
Audio Recorder plugin for Xamarin and Windows
Stars: ✭ 140 (-11.39%)
Mutual labels:  xamarin, uwp
Httptracer
Stars: ✭ 142 (-10.13%)
Mutual labels:  httpclient, xamarin
Reactivemvvm
Cross-platform ReactiveUI sample app built for a talk at MSK .NET conf.
Stars: ✭ 94 (-40.51%)
Mutual labels:  xamarin, uwp
Active Directory B2c Xamarin Native
This is a simple Xamarin Forms app showcasing how to use MSAL to authenticate users via Azure Active Directory B2C, and access a Web API with the resulting tokens.
Stars: ✭ 91 (-42.41%)
Mutual labels:  xamarin, uwp
Megaapiclient
MegaApiClient is a C# .Net library to access http://mega.co.nz / http://mega.nz cloud storage and file hosting service.
Stars: ✭ 151 (-4.43%)
Mutual labels:  xamarin, uwp
Ffimageloading
Image loading, caching & transforming library for Xamarin and Windows
Stars: ✭ 1,288 (+715.19%)
Mutual labels:  xamarin, uwp
Htmllabelplugin
Use this Xamarin.Forms plugin to display HTML content into a label.
Stars: ✭ 119 (-24.68%)
Mutual labels:  xamarin, uwp
Aspnetcore Practice
ASP.NET Core 專案練習集合,ASP.NET Core Practice Projects
Stars: ✭ 80 (-49.37%)
Mutual labels:  httpclient, dotnet-core
Xamarin Forms Gtk Movies Sample
The Movie DB Xamarin.Forms Sample
Stars: ✭ 83 (-47.47%)
Mutual labels:  xamarin, uwp
Ble.net
Cross-platform Bluetooth Low Energy (BLE) library for Android, iOS, and UWP
Stars: ✭ 137 (-13.29%)
Mutual labels:  xamarin, uwp

NuGet Build status Gitter chat StackOverflow

Please visit the main site.

Tiny.RestClient facilitates the dialog between your API and your application. It hides all the complexity of communication, deserialisation ...

Platform Support

The support of .NET Standard 1.1 to 2.0 allows you to use it with :

  • .Net Framework 4.5+
  • Xamarin iOS, Xamarin Android
  • .Net Core
  • UWP
  • Windows Phone 8.1
  • Windows 8.1

Features

  • Modern async http client for REST API.
  • Support of verbs : GET, POST , PUT, DELETE, PATCH and custom http verbs
  • Support of ETag
  • Support of multi-part form data
  • Support of cancellation token on each requests
  • Support of : download file and Upload file
  • Automatic XML and JSON serialization / deserialization
  • Support of custom serialisation / deserialisation
  • Support of camelCase, snakeCase kebabCase for json serialization
  • Support of compression and decompression (gzip and deflate)
  • Typed exceptions which are easier to interpret
  • Define timeout globally or per request
  • Timeout exception thrown if the request is in timeout (by default HttpClient sends OperationCancelledException, so we can't distinguish between user cancellation and timeout)
  • Provide an easy way to log : all sending of request, failed to get response, and the time get response.
  • Support of export requests to postman collection
  • Support of display cURL requests in debug output
  • Support of Basic Authentification
  • Support of OAuth2 Authentification

Basic usage

Create the client

using Tiny.RestClient;

var client = new TinyRestClient(new HttpClient(), "http://MyAPI.com/api");

Headers

Default header for all requests

// Add default header for each calls
client.Settings.DefaultHeaders.Add("CustomHeader", "Header");
// Add Auth2.0 token
client.Settings.DefaultHeaders.AddBearer("token");
// Add default basic authentication header
client.Settings.DefaultHeaders.AddBasicAuthentication("username", "password");

Add header for current request

// Add header for this request only
client.GetRequest("City/All").
      AddHeader("CustomHeader", "Header").
      ExecuteAsync();
// Add header for this request only
client.GetRequest("City/All").
      WithOAuthBearer("MYTOKEN").
      ExecuteAsync();
// Add basic authentication for this request only
client.GetRequest("City/All").
      WithBasicAuthentication("username", "password").
      ExecuteAsync();

Calculate headers before send the requests

Before send requests to server we can add calculate dynamically the headers to add to resquest like below :

client.Settings.CalculateHeadersHandler = async () =>
{
   var token = await GetACustomTokenAsync();

   var headers = new Headers
   {
       { "CustomToken", token },
   };
   return headers;
};

Read headers of response

await client.GetRequest("City/GetAll").
             FillResponseHeaders(out headersOfResponse Headers).
             ExecuteAsync();
foreach(var header in headersOfResponse)
{
    Debug.WriteLine($"{current.Key}");
    foreach (var item in current.Value)
    {
        Debug.WriteLine(item);
    }
}

Basic GET http requests

var cities = client.GetRequest("City/All").ExecuteAsync<List<City>>();
// GET http://MyAPI.com/api/City/All an deserialize automaticaly the content

// Add a query parameter
var cities = client.
    GetRequest("City").
    AddQueryParameter("id", 2).
    AddQueryParameter("country", "France").
    ExecuteAsync<City>> ();

// GET http://MyAPI.com/api/City?id=2&country=France deserialize automaticaly the content

Basic POST http requests

// POST
 var city = new City() { Name = "Paris" , Country = "France"};

// With content
var response = await client.PostRequest("City", city).
                ExecuteAsync<bool>();
// POST http://MyAPI.com/api/City with city as content

// With form url encoded data
var response = await client.
                PostRequest("City/Add").
                AddFormParameter("country", "France").
                AddFormParameter("name", "Paris").
                ExecuteAsync<Response>();
// POST http://MyAPI.com/api/City/Add with from url encoded content


var fileInfo = new FileInfo("myTextFile.txt");
var response = await client.
                PostRequest("City/Image/Add").
                AddFileContent(fileInfo, "text/plain").
                ExecuteAsync<Response>();
// POST text file at http://MyAPI.com/api/City/Add 

Custom Http Verb requests

 await client.
       NewRequest(new System.Net.Http.HttpMethod("HEAD"), "City/All").
       ExecuteAsync<List<City>>();

Define timeout

Define a global timeout for all client. (By default it's setted to 100 secondes)

client.Settings.DefaultTimeout = TimeSpan.FromSeconds(100);

Define the timeout for one request

request.WithTimeout(TimeSpan.FromSeconds(100));

Allow non http 2xx responses

Globaly

Allow any status codes :

client.Settings.HttpStatusCodeAllowed.AllowAnyStatus = true;

Allow only a range of http status codes :

client.Settings.HttpStatusCodeAllowed.Add(new HttpStatusRange(400, 420));

or

client.Settings.HttpStatusCodeAllowed.Add(new HttpStatusRange(System.Net.HttpStatusCode.BadRequest, System.Net.HttpStatusCode.BadGateway));

By request

Allow all status code :

request.AllowAllHttpStatusCode().ExecuteAsync();

Allow only a range of http status codes :

request.AllowRangeHttpStatusCode(400, 420).ExecuteAsync();

Allow only on stats code of http status codes :

request.AllowSpecificHttpStatusCode(409).ExecuteAsync();

Download file

string filePath = "c:\map.pdf";
FileInfo fileInfo = await client.
                GetRequest("City/map.pdf").
                DownloadFileAsync("c:\map.pdf");
// GET http://MyAPI.com/api/City/map.pdf 

Get raw HttpResponseMessage

var response = await client.
                PostRequest("City/Add").
                AddFormParameter("country", "France").
                AddFormParameter("name", "Paris").
                ExecuteAsHttpResponseMessageAsync();
// POST http://MyAPI.com/api/City/Add with from url encoded content

Get raw string result

string response = await client.
                GetRequest("City/All").
                ExecuteAsStringAsync();
// GET http://MyAPI.com/api/City/All with from url encoded content

Multi-part form data

// With 2 json content
var city1 = new City() { Name = "Paris" , Country = "France"};
var city2 = new City() { Name = "Ajaccio" , Country = "France"};
var response = await client.NewRequest(HttpVerb.Post, "City").
await client.PostRequest("MultiPart/Test").
              AsMultiPartFromDataRequest().
              AddContent<City>(city1, "city1", "city1.json").
              AddContent<City>(city2, "city2", "city2.json").
              ExecuteAsync();


// With 2 byte array content
byte[] byteArray1 = ...
byte[] byteArray2 = ...           
              
await client.PostRequest("MultiPart/Test").
              AsMultiPartFromDataRequest().
              AddByteArray(byteArray1, "request", "request2.bin").
              AddByteArray(byteArray2, "request", "request2.bin")
              ExecuteAsync();
  

// With 2 streams content        
Stream1 stream1 = ...
Stream stream2 = ...         
await client.PostRequest("MultiPart/Test").
              AsMultiPartFromDataRequest().
              AddStream(stream1, "request", "request2.bin").
              AddStream(stream2, "request", "request2.bin")
              ExecuteAsync();
              
              
// With 2 files content           

var fileInfo1 = new FileInfo("myTextFile1.txt");
var fileInfo2 = new FileInfo("myTextFile2.txt");

var response = await client.
                PostRequest("City/Image/Add").
                AsMultiPartFromDataRequest().
                AddFileContent(fileInfo1, "text/plain").
                AddFileContent(fileInfo2, "text/plain").
                ExecuteAsync<Response>();
                
                
// With 2 strings content   
var response = await client.
                PostRequest("City/Image/Text").
                AsMultiPartFromDataRequest().
                AddString("string1", "text/plain").
                AddString("string2", "text/plain").
                ExecuteAsync<Response>();

// With mixed content                  
await client.PostRequest("Files/Add").
              AsMultiPartFromDataRequest().
              AddContent<City>(city1, "city1", "city1.json").
              AddByteArray(byteArray1, "request", "request2.bin").
              AddStream(stream2, "request", "request2.bin")
              AddString("string1", "text", "request.txt")
              ExecuteAsync();

String, Streams and bytes array

You can use as content : strings, streams or byte arrays. If you use these methods no serializer will be used.

String

// Read string response
 Stream stream = await client.
              GetRequest("text").
              ExecuteAsStringAsync();
              
// Post String as content
await client.PostRequest("poetry/text").
            AddStringContent(stream).
            ExecuteAsync();

Streams

// Read stream response
 Stream stream = await client.
              GetRequest("File").
              ExecuteAsStreamAsync();
// Post Stream as content
await client.PostRequest("File/Add").
            AddStreamContent(stream).
            ExecuteAsync();

Byte array

// Read byte array response         
byte[] byteArray = await client.
              GetRequest("File").
              ExecuteAsByteArrayAsync();

// Read byte array as content
await client.
            PostRequest("File/Add").
            AddByteArrayContent(byteArray).
            ExecuteAsync();

Error handling

All requests can throw 5 exceptions :

  • ConnectionException : thrown when the request can't reach the server
  • HttpException : thrown when the server has invalid error code
  • SerializeException : thrown when the serializer can't serialize the content
  • DeserializeException : thrown when the deserializer can't deserialize the response
  • TimeoutException : thrown when the request take too much time to be executed

Catch a specific error code

string cityName = "Paris";
try
{ 
   var response = await client.
     GetRequest("City").
     AddQueryParameter("Name", cityName).
     ExecuteAsync<City>();
}
catch (HttpException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
{
   throw new CityNotFoundException(cityName);
}
catch (HttpException ex) when (ex.StatusCode == System.Net.HttpStatusCode.InternalServerError)
{
   throw new ServerErrorException($"{ex.Message} {ex.ReasonPhrase}");
}

Enclapsulate HttpExceptions

We can setup a global handler to provide a logic to encapsulate HttpException automatically.

For example I can choose to translate all HttpException with StatusCode NotFound in a NotFoundCustomException.

client.Settings.EncapsulateHttpExceptionHandler = (ex) =>
{
   if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
   {
      return new NotFoundCustomException();
   }

   return ex;
};

Now if I call an API wich respond with status code NotFound it will throw automaticaly my custom exception.

// Call an API wich throw NotFound error
await client.GetRequest("APIWhichNotExists").ExecuteAsync();

ETag

The lib supports the Entity tag but it's not enabled by default.

Define an ETagContainer globally

An implementation of IETagContainer is provided. It stores all data in multiples files.

To enable it :

client.Settings.ETagContainer = new ETagFileContainer(@"C:\ETagFolder");

Define an ETagContainer for one request

You can also define the ETagContainer only on specific request.

request.WithETagContainer(eTagContainer);

Formatters

By default :

  • the Json is used as default Formatter.
  • Xml Formatter is added in Formatters

Each formatter has a list of supported media types. It allows TinyRestClient to detect which formatter will be used. If no formatter is found it uses the default formatter.

Add a new formatter

Add a new custom formatter as default formatter.

bool isDefaultFormatter = true;
var customFormatter = new CustomFormatter();
client.Settings.Formatters.Add(customFormatter, isDefaultFormatter);

Remove a formatter

var lastFormatter = client.Settings.Formatters.First(f => f is XmlSerializer);
client.Settings.Formatters.Remove(lastFormatter);

Json custom formatting

You can enable 3 types of formatting on JsonFormatter :

  • CamelCase (PropertyName => propertyName)
  • SnakeCase (PropertyName => property_name)
  • KebabCase (aslo known as SpinalCase) (PropertyName => property-name).
// Enable KebabCase
  client.Settings.Formatters.OfType<JsonFormatter>().First().UseKebabCase();
// Enable CamelCase
  client.Settings.Formatters.OfType<JsonFormatter>().First().UseCamelCase();
// Enable SnakeCase
  client.Settings.Formatters.OfType<JsonFormatter>().First().UseSkakeCase();

Define a specific serialize for one request

IFormatter serializer = new XmlFormatter();
 var response = await client.
     PostRequest("City", city, serializer).
     ExecuteAsync();

Define a specific deserializer for one request

IFormatter deserializer = new XmlFormatter();

 var response = await client.
     GetRequest("City").
     AddQueryParameter("Name", cityName).
     ExecuteAsync<City>(deserializer);

Custom formatter

You can create your own serializer/deserializer by implementing IFormatter

For example the implementation of XmlFormatter is really simple :

public class XmlFormatter : IFormatter
{

   public string DefaultMediaType => "application/xml";

   public IEnumerable<string> SupportedMediaTypes
   {
      get
      {
         yield return "application/xml";
         yield return "text/xml";
      }
   }

   public T Deserialize<T>(Stream stream, Encoding encoding)
   {
      using (var reader = new StreamReader(stream, encoding))
      {
         var serializer = new XmlSerializer(typeof(T));
         return (T)serializer.Deserialize(reader);
      }
   }

   public string Serialize<T>(T data, Encoding encoding)
   {
         if (data == default)
         {
             return null;
         }

         var serializer = new XmlSerializer(data.GetType());
         using (var stringWriter = new DynamicEncodingStringWriter(encoding))
         {
            serializer.Serialize(stringWriter, data);
            return stringWriter.ToString();
         }
      }
   }

Listeners

You can easily add a listener to listen all the sent requests / responses received and all exceptions.

Two listeners are provided by the lib :

  • A debug listener : which logs all requests in debug console
  • A postman listener : which allows you to export all your requests as a postman collection

Debug Listener

To add a Debug listener you have to call AddDebug on Listeners property

client.Settings.Listeners.AddDebug();

cURL Listener

To add a cURL listener you have to call AddCurl on Listeners property

client.Settings.Listeners.AddCurl();

It produce this type of output in debug window for each ExecuteAsync called :

curl -X POST "http://localhost:4242/api/PostTest/complex"-H "Accept: application/json" -H "Content-Type: application/json" -d "{\"Id\":42,\"Data\":\"DATA\"}"

Postman Listener

To add a postman listener you have to call AddPostman on Listeners property

PostmanListerner postmanListener = client.Settings.Listeners.AddPostman("nameOfCollection");

When you want to save the postman collection you have to call SaveAsync

await postmanListener.SaveAsync(new FileInfo("postmanCollection.json");

If you only want the Json of collection you can call the method GetCollectionJson

listener.GetCollectionJson();

Custom Listener

You can also create you own listener by implementing IListener.

IListener myCustomListerner = ..
client.Settings.Listeners.Add(myCustomListerner);

Compression and Decompression

By default, the client supports the decompression of Gzip and deflate.

If the server respond with the header ContentEncoding "gzip" or "deflate" the client will decompress it automaticly.

Compression

For each request which posts a content you can specified the compression algorithm like below

var response = await client.
                PostRequest("Gzip/complex", postRequest, compression: client.Settings.Compressions["gzip"]).
                ExecuteAsync<Response>();

Warning : the server must be able to decompress your content.

Decompression

Even if it's supported the client didn't send Accept-Encoding header automaticaly.

You can add it for gzip all request like below :

var compression = client.Settings.Compressions["gzip"];
compression.AddAcceptEncodingHeader = true;

You can add it for deflate all requests like below :

var compression = client.Settings.Compressions["deflate"];
compression.AddAcceptEncodingHeader = true;

If the server can compress response, it will respond with compressed content.

Custom ICompression

You can add your own compression / decompression algorithm :

client.Settings.Add(new CustomCompression());

Your class must implement the interface ICompression.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].