Dart - HTTP Client
HTTP Client for Dart with Middlewares !
Getting started
Simply add wyatt_http_client in pubspec.yaml, then
import 'package:wyatt_http_client/wyatt_http_client.dart';
Usage
Firstly you have to understand Middleware and Pipeline concepts.
In wyatt_http_client
a middleware is an object where requests and responses
pass through. And a pipeline is basicaly a list of middlewares.
In a pipeline with middlewares A and B, if request pass through A, then B, the response will pass through B then A.
You can
print(pipeline)
to get full process order of a pipeline.
For example, if you want to log every request, and simplify an url you can use provided SimpleLogger
and UriPrefix
.
// Create the Pipeline
final Pipeline pipeline = Pipeline()
.addMiddleware(
UriPrefixMiddleware(
protocol: Protocols.http,
authority: 'localhost:80',
),
)
.addMiddleware(SimpleLoggerMiddleware());
Then if you print the pipeline,
[Req] -> UriPrefix -> SimpleLogger
[Res] -> SimpleLogger
The
response
doesn't pass throughUriPrefix
because it's anOnRequestMiddleware
only.
And you can create a client.
final client = MiddlewareClient(pipeline: pipeline);
At this point you can use client
like every Client from package:http/http.dart
.
Recipes
Rest API with URL Authentication
Let's build a client for a REST API where the (bad) authentication is through the URL. We need some middlewares:
- SimpleLogger, to log every request and response (useful for debug).
- BodyToJson, to automaticaly transform Map object to JSON.
- UriPrefix, to simplify the build of an API Object (save protocol and API prefix).
- UnsafeAuth, to use url based authentication.
Let's start by creating the Pipeline:
final Pipeline pipeline = Pipeline()
.addMiddleware(
UriPrefixMiddleware(
protocol: Protocols.http,
authority: 'localhost:80',
),
)
.addMiddleware(BodyToJsonMiddleware())
.addMiddleware(
UnsafeAuthMiddleware(
username: 'wyatt',
password: 'motdepasse',
),
)
.addMiddleware(SimpleLoggerMiddleware());
Then simply create a client and make a call.
final client = MiddlewareClient(pipeline: pipeline);
await client.get(Uri.parse('/protected'));
Here it make a
GET
call onhttp://localhost:80/protected?username=wyatt&password=motdepasse
And voilà.
Rest API with Oauth2
So now we want a real authentication.
final Pipeline pipeline = Pipeline()
.addMiddleware(
UriPrefixMiddleware(
protocol: Protocols.http,
authority: 'localhost:80',
),
)
.addMiddleware(BodyToJsonMiddleware())
.addMiddleware(
RefreshTokenAuthMiddleware(
authorizationEndpoint: '/auth/sign-in',
tokenEndpoint: '/auth/refresh',
accessTokenParser: (body) => body['access_token']! as String,
refreshTokenParser: (body) => body['refresh_token']! as String,
unauthorized: HttpStatus.forbidden,
),
)
.addMiddleware(SimpleLoggerMiddleware());
Here we just change
UnsafeAuthMiddleware
byRefreshTokenAuthMiddleware
and the whole app while adapt to a new authentication system.
Create a new Middleware
You can create your own middleware by implementing Middleware
class, and use mixins to add OnRequest
and/or OnResponse
methods.
class SimpleLoggerMiddleware
with OnRequestMiddleware, OnResponseMiddleware
implements Middleware {
@override
String getName() => 'SimpleLogger';
@override
Future<MiddlewareRequest> onRequest(
MiddlewareContext context,
MiddlewareRequest request,
) async {
print('${getName()}::OnRequest');
return request;
}
@override
Future<MiddlewareResponse> onResponse(
MiddlewareContext context,
MiddlewareResponse response,
) async {
print('${getName()}::OnResponse');
return response;
}
}