Nothing Special   »   [go: up one dir, main page]

Skip to content
forked from lejard-h/chopper

Chopper is an http client generator using source_gen and inspired from Retrofit.

Notifications You must be signed in to change notification settings

zgunz42/chopper

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Coverage Status

Chopper is an http client generator using source_gen and inspired by Retrofit.

Usage

Generator

Add the generator to your dev dependencies

dependencies:
  chopper: ^2.0.0

dev_dependencies:
  build_runner: ^1.0.0
  chopper_generator: ^2.0.0

Define and Generate your API

// my_service.dart

import "dart:async";
import 'package:chopper/chopper.dart';

part "my_service.chopper.dart";

@ChopperApi(baseUrl: "/resources")
abstract class MyService extends ChopperService {
  static MyService create([ChopperClient client]) => _$MyService(client);

  @Get(url: "{id}")
  Future<Response> getResource(@Path() String id);

  @Get(headers: const {"foo": "bar"})
  Future<Response<Map>> getMapResource(@Query() String id);

  @Post(url: 'multi')
  @multipart
  Future<Response> postResources(
    @Part('1') Map a,
    @Part('2') Map b,
    @Part('3') String c,
  );

  @Post(url: 'file')
  @multipart
  Future<Response> postFile(
    @FileField('file') List<int> bytes,
  );
}

then run the generator

pub run build_runner build

#flutter
flutter packages pub run build_runner build

Use it

final chopper = ChopperClient(
    baseUrl: "http://localhost:8000",
    services: [
      // the generated service
      MyService.create()
    ],
    converter: JsonConverter(),
  );

final myService = MyService.create(chopper);

final response = await myService.getMapResource("1");

chopper.close();

Or create a Chopper client and inject your generated api.

import 'package:chopper/chopper.dart';

final chopper = new ChopperClient(
    baseUrl: "http://localhost:8000",
    services: [
      // the generated service
      MyService.create()
    ],
    converter: JsonConverter(),
);

final myService = chopper.service<MyService>(MyService);

Interceptors

Request

implement RequestInterceptor class or define function with following signature FutureOr<Request> RequestInterceptorFunc(Request request)

Request interceptor are called just before sending request

final chopper = new ChopperClient(
   interceptors: [
     (request) async => request.replace(body: {}),
   ]
);

Response

implement ResponseInterceptor class or define function with following signature FutureOr<Response> ResponseInterceptorFunc(Response response)

Called after successfull or failed request

final chopper = new ChopperClient(
   interceptors: [
     (response) async => response.replace(body: {}),
   ]
);

Converter

Converter is used to transform body, for example transforming a Dart object to a Map<String, dynamic>

Both converter and errorConverter are called before request and response intercptors.

errorConverter is called only on error response (statusCode < 200 || statusCode >= 300)

final chopper = new ChopperClient(
   converter: MyConverter(),
   errorConverter: MyErrorConverter()
);

More example

About

Chopper is an http client generator using source_gen and inspired from Retrofit.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Dart 97.1%
  • Shell 2.4%
  • HTML 0.5%