Skip to content
RefitPollyWorkshop for VAA
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore Initial commit Jan 8, 2019
Presentation.pdf
README.md
appveyor.yml Changed sonar name Jan 9, 2019
db.json
start_api.bat Double interface for authorized / unauthorized scenarios Jan 14, 2019

README.md

Build status Sonar Quality Gate Code Coverage

Windows Build history

Workshop Refit & Polly

Jacob Duijzer, January 2018

Refit

Refit: The automatic type-safe REST library for .NET Core, Xamarin and .NET

public interface IRemoteApi
{
	[Get("/posts")]
	Task<IEnumerable<Post>> GetAllPostsAsync();

	[Post("/posts")]
	[Headers("Authorization: Bearer")]
	Task<Post> AddPostAsync([Body]Post post);

	[Delete("/posts/{id}")]
	[Headers("Authorization: Bearer")]
	Task DeletePostByIdAsync(int id);
}

Refit - sample code

var remoteApi = RestService
	.For<IRemoteApi>("http://localhost:3000");


var posts = await remoteApi.GetAllPostsAsync();

var singlePost = await remoteApi.GetPostByIdAsync(1);

Refit - Scenario 1

Simple api calls


Refit - Scenario 2

Logging


Refit - Scenario 3

Authenticated api calls


Polly

Policies:

  • Retry
  • CircuitBreaker
  • Timeout
  • Bulkhead Isolation
  • Cache
  • Fallback
  • PolicyWrap

Polly - sample code


// Retry multiple times, calling an action on each retry
// with the current exception, retry count and context
// provided to Execute()
var _retryPolicy = Policy.Handle<SomeExceptionType>()
	.Retry(3, (exception, retryCount, context) =>
	{
    	// do something to prevent the next exception
	});

await _retryPolicy.ExecuteAsync(
	remoteApi.GetAllPostsAsync()
);

Polly - Scenario 1

Retry


Polly - Scenario 2

Fallback


Polly - Scenario 3

CircuitBreaker


Polly - Scenario 4

Retry with fallback


Polly - Scenario 5

CircuitBreakerWithRetryAndFallBack


HttpClientHandler


Polly - Unit testing


Alternatives - Flurl

C#!
// Flurl will use 1 HttpClient instance per host
var person = await "https://api.com"
    .AppendPathSegment("person")
    .SetQueryParams(new { a = 1, b = 2 })
    .WithOAuthBearerToken("my_oauth_token")
    .PostJsonAsync(new
    {
        first_name = "Claire",
        last_name = "Underwood"
    })
    .ReceiveJson<Person>();

Alteratives - DalSoft.RestClient

C#!
var config = new Config().UseFormUrlEncodedHandler();

dynamic restClient = new RestClient("https://jsonplaceholder.typicode.com", config);
var user = new User { name="foo", email="foo@bar.com" };

//POST name=foo&email=foo@bar.com  https://jsonplaceholder.typicode.com/users/1
result = await client
  .Headers(new { ContentType = "application/x-www-form-urlencoded" })
  .User(1)
  .Post(user);

Setup sample project

https://github.com/jacobduijzer/RefitPollyWorkshop

  • Get the source from GitHub
  • in folder src/api: npm install
  • in the folder src/api: npm run start
  • configure & run the console app

Links


You can’t perform that action at this time.