Rest api client

Abstraction for communicating with REST API in flutter projects. Incorporates exception handling and jwt with refresh token authorization. You can also find this package on pub as rest_api_client


  //This must be called once per application lifetime
  await RestApiClient.initFlutter();

  IRestApiClient restApiClient = RestApiClient(
    options: RestApiClientOptions(
      //Defines your base API url eg.
      baseUrl: '',

      //Enable caching of response data
      cacheEnabled: true,
    authOptions: AuthOptions(
      //Define refresh token endpoint for RestApiClient
      //instance to use the first time response status code is 401
      refreshTokenEndpoint: '/auth/token-refresh',

      //Define the name of your api parameter name
      //on RefreshToken endpoint eg. 'refreshToken' or 'value' ...
      refreshTokenParameterName: 'token',

      //This method is called on successfull call to refreshTokenEndpoint
      //Provides a way to get a jwt from response, much like
      //resolveValidationErrorsMap callback
      resolveJwt: (response) =>['result']['accessToken']['token'],

      //Much like resolveJwt, this method is used to resolve
      //refresh token from response
      resolveRefreshToken: (response) =>['result']['refreshToken']['token'],
    loggingOptions: LoggingOptions(
      //Toggle logging of your requests and responses
      //to the console while debugging
      logNetworkTraffic: true,

  //init must be called, preferably right after the instantiation
  await restApiClient.init();

  //Use restApiClient from this point on

If you are using authentication in your app probably it would look something like this

  final response = await
    data: {'username': 'john', 'password': 'Flutter_is_awesome1!'},

  //Extract the values from response
  var jwt =['jwt'];
  var refreshToken =['refreshToken'];

Let's asume that somehow we got jwt and refresh token, you probably pinged your api Authentication endpoint to get these two values.

  jwt = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiZmx1dHRlciI6IkZsdXR0ZXIgaXMgYXdlc29tZSIsImNoYWxsZW5nZSI6IllvdSBtYWRlIGl0LCB5b3UgY3JhY2tlZCB0aGUgY29kZS4gWW91J3JlIGF3ZXNvbWUgdG9vLiIsImlhdCI6MTUxNjIzOTAyMn0.5QJz8hhxYsHxShS4hWKdHzcFH_IsQQZAnWSEcHJkspE';
  refreshToken = 'c91c03ea6c46a86cbc019be3d71d0a1a';

  //set the authorization (JWT)
  restApiClient.authHandler.authenticate(jwt: jwt, refreshToken: refreshToken);

  //Create authorized requests safely

Add parameters to your requests

    queryParameters: {
      'name': 'darts'

Ignore server errors that might happen in the next request

  restApiClient.exceptionHandler.exceptionOptions.showInternalServerErrors = false;

  try {
      queryParameters: {'name': 'darts'},
  } catch (e) {

Ignore all exceptions that might happen in the next request

    data: {'grade': 5, 'comment': 'Throwing dart is not safe but upgrading to Dart 2.12.1 is. #nullsafety'},

    data: {
      'grade': 5,
      'comment': 'On the other hand throwing dartz is fun',