wyatt_http_client (2.0.1)

Published 2023-08-28 13:53:04 +00:00 by malo in Wyatt-FOSS/wyatt-packages

Installation

dart pub add wyatt_http_client:2.0.1 --hosted-url=

About this package

A Dart client for RESTful APIs with authentication.

HTTP Client

Style: Wyatt Analysis SDK: Dart & Flutter

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(
        const UriPrefixMiddleware(
            protocol: Protocols.http,
            authority: 'localhost:80',
        ),
    )
    ..addMiddleware(const SimpleLoggerMiddleware());

Then if you print the pipeline,

[Req] -> UriPrefix -> SimpleLogger
[Res] -> SimpleLogger

The response doesn't pass through UriPrefix because it's an OnRequestMiddleware 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(
        const UriPrefixMiddleware(
            protocol: Protocols.http,
            authority: 'localhost:80',
        ),
    )
    ..addMiddleware(const BodyToJsonMiddleware())
    ..addMiddleware(
        const 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 on http://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(
        const UriPrefixMiddleware(
            protocol: Protocols.http,
            authority: 'localhost:80',
        ),
    )
    ..addMiddleware(const 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(const SimpleLoggerMiddleware());

Here we just change UnsafeAuthMiddleware by RefreshTokenAuthMiddleware 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 {
    
  const SimpleLoggerMiddleware();
  
  @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;
  }
}
Details
Pub
2023-08-28 13:53:04 +00:00
35
25 KiB
Assets (1)
2.0.1.tar.gz 25 KiB
Versions (2) View all
2.0.1 2023-08-28
2.0.0 2023-04-13