chopper 1.1.0

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 90

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: ^1.0.0

dev_dependencies:
  build_runner: ^0.8.0
  chopper_generator: ^1.0.0

Define and Generate your API

// my_service.dart

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

part "my_service.chopper.dart";

@ChopperApi("MyService", baseUrl: "/resources")
abstract class MyServiceDefinition {
  @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 = new ChopperClient(
    baseUrl: "http://localhost:8000",
    jsonApi: true,
);

final myService = MyService.withClient(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()
    ],
    jsonApi: true,
);

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

Issue Tracker

Changelog

1.1.0

  • BreakingChange Removed name parameter on ChopperApi New way to instanciate a service dart @ChopperApi() abstract class MyService extends ChopperService { static MyService create([ChopperClient client]) => _$MyService(client); }

1.0.0

  • Multipart request
  • form url encoded
  • add jsonAPI and formUrlEncodedApi boolean to ChopperClient
  • json and formUrlEncoding are now builtin
  • onError, onResponse, onRequest stream
  • error converter
  • add withClient constructor

0.1.1

  • Remove trimSlashes

0.1.0

  • update dart sdk

0.0.2

  • the generated extension is now *.chopper.dart

  • rename ServiceDefinition to ChopperApi

  • rename ChopperClient.services field to ChopperClient.apis

0.0.1

  • Initial version, created by Stagehand

example/main.dart

import 'package:chopper/chopper.dart';
import 'definition.dart';

main() async {
  final chopper = ChopperClient(
    baseUrl: "http://localhost:8000",
    services: [
      // the generated service
      MyService.create()
    ],
    jsonApi: true,
  );

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

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

  chopper.close();
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  chopper: ^1.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:chopper/chopper.dart';
  
Version Uploaded Documentation Archive
1.1.0 Dec 9, 2018 Go to the documentation of chopper 1.1.0 Download chopper 1.1.0 archive
1.0.0 Sep 29, 2018 Go to the documentation of chopper 1.0.0 Download chopper 1.0.0 archive
0.1.0 Aug 1, 2018 Go to the documentation of chopper 0.1.0 Download chopper 0.1.0 archive
0.0.3 Apr 24, 2018 Go to the documentation of chopper 0.0.3 Download chopper 0.0.3 archive
0.0.2 Apr 8, 2018 Go to the documentation of chopper 0.0.2 Download chopper 0.0.2 archive
0.0.1 Apr 7, 2018 Go to the documentation of chopper 0.0.1 Download chopper 0.0.1 archive
1.0.0-dev3 Sep 25, 2018 Go to the documentation of chopper 1.0.0-dev3 Download chopper 1.0.0-dev3 archive
1.0.0-dev2 Sep 16, 2018 Go to the documentation of chopper 1.0.0-dev2 Download chopper 1.0.0-dev2 archive
1.0.0-dev1 Sep 14, 2018 Go to the documentation of chopper 1.0.0-dev1 Download chopper 1.0.0-dev1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
79
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
90
Learn more about scoring.

We analyzed this package on Dec 9, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:chopper/chopper.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <3.0.0
http ^0.11.0 0.11.3+17 0.12.0
meta ^1.1.2 1.1.6
Transitive dependencies
async 2.0.8
charcode 1.1.2
collection 1.14.11
http_parser 3.1.3
path 1.6.2
source_span 1.4.1
string_scanner 1.0.4
typed_data 1.1.6
Dev dependencies
build_runner ^1.0.0
build_test any
chopper_generator
coverage ^0.12.2
test ^1.3.0