Chopper Client Wrapper

Chopper Client Wrapper is a package that provides an abstract class ChopperClientWrapper to create a Chopper client that can be used to interact with your API service. This class provides a set of functionalities such as creating a Chopper client, interceptors, converters, and more.

Usage

To use the ChopperClientWrapper class, you need to extend it and implement its abstract methods. Here are the methods that need to be implemented:

  • jsonToModelConvertorMap: A map that has a type and its method that converts JSON to its type. For example: LoginResponseModel: LoginResponseModel.fromJson
  • jsonToErrorModelConvertorMap: A map that has a type and its method that converts JSON to its type, used to deserialize error response. For example: ErrorResponseModel: ErrorResponseModel.fromJson
  • baseUrl: Base URL of your API service
  • chopperServices: A list of Chopper services that you create for your application
  • getToken(): A method that should be overridden to return the token that can be passed to the header

Here is an example of how to extend the ChopperClientWrapper class:

import 'package:chopper/chopper.dart'; 
import 'package:chopper\_client\_wrapper/ChopperService/Utils/Convertors/JsonToTypeConverter.dart';
import 'package:chopper\_client\_wrapper/ChopperService/Utils/Interceptors/ApplyHeaderInterceptor.dart'; 
import 'package:chopper\_client\_wrapper/ChopperService/Utils/Interceptors/RequestLogger.dart'; 
import 'package:chopper\_client\_wrapper/ChopperService/Utils/Interceptors/ResponseLogger.dart'; 

class MyChopperClient extends ChopperClientWrapper
{ 
@override Map<Type, JsonConvertorFunction> get jsonToModelConvertorMap => throw UnimplementedError(); 
@override Map<Type, JsonConvertorFunction> get jsonToErrorModelConvertorMap => throw UnimplementedError(); 
@override String get baseUrl => 'https://myapi.com'; 
@override List<ChopperService> get chopperServices => [
// List of Chopper services that you create for your application 
]; 
@override String getToken() { 
// Override this method to get the token that can be passed to the header 
return 'mytoken';
    }
}

#Methods

getChopperService

This method can be used to access any particular Chopper service that you have created and assigned in chopperServices. You need to specify the type of the Chopper service that you want to get. For example:

var myChopperService = MyChopperClient().getChopperService

_createChopperClient()

This private method creates a Chopper client instance using the baseUrl, chopperServices, and interceptors. It also sets up the converter and errorConverter that are used to convert JSON to your model classes.

Interceptors

Chopper Client Wrapper provides three interceptors:

  • RequestLogger(): Logs the request before sending it to the server. It logs the request type, URL, and body.
  • ResponseLogger(): Logs the response that came from the server and also logs the error if there is one.
  • ApplyHeaderInterceptor(): Adds a header before sending it to the server.

Converters

Chopper Client Wrapper provides a JsonToTypeConverter that is used to convert JSON to your model classes. You need to provide a map that has a type and its method that converts JSON