chopper 2.1.0

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

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 #

Issue Tracker #

Changelog #

2.0.0 #

  • Fix type safety
  • Fix json converter
  • Handle BuiltList

2.0.0 #

  • Request is now containing baseUrl

  • Can call Request.toHttpRequest() direclty to get the http.BaseRequest will receive

  • If a full url is specified in the path (ex: @Get(path: 'https://...')), it won't be concaten with the baseUrl of the ChopperClient and the ChopperAPI

  • Add CurlInterceptor thanks @edwardaux

  • Add HttpLoggingInterceptor

  • Add FactoryConverter annotation @FactoryConverter(request: convertRequest, response: convertResponse)

  • BreakingChange

    • Method.url renamed to path
    • Converter.encode and Converter.decode removed, implement Converter.convertResponse and Converter.convertRequest` instead
    • ChopperClient.jsonApi deprecated, use a JsonConverter instead
    • ChopperClient.formUrlEncodedApi, use FormUrlEncodedConverter instead
    • remove JsonEncoded annotation, use FactoryConverter instead

1.1.0 #

  • BreakingChange Removed name parameter on ChopperApi New way to instanciate a service

      @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 'package:chopper/src/interceptor.dart';
import 'definition.dart';

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

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

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

  final list = await myService.getListResources();
  print(list.body);

  chopper.dispose();
}

Use this package as a library

1. Depend on it

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


dependencies:
  chopper: ^2.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
2.1.0 Feb 16, 2019 Go to the documentation of chopper 2.1.0 Download chopper 2.1.0 archive
2.0.0 Feb 12, 2019 Go to the documentation of chopper 2.0.0 Download chopper 2.0.0 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.2.0-dev3 Jan 15, 2019 Go to the documentation of chopper 1.2.0-dev3 Download chopper 1.2.0-dev3 archive
1.2.0-dev2 Dec 21, 2018 Go to the documentation of chopper 1.2.0-dev2 Download chopper 1.2.0-dev2 archive

All 14 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
82
Health:
Code health derived from static analysis. [more]
90
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
88
Learn more about scoring.

We analyzed this package on Feb 16, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.13

Platforms

Detected platforms: Flutter, web, other

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

Health suggestions

Fix lib/src/annotations.dart. (-6.31 points)

Analysis of lib/src/annotations.dart reported 13 hints, including:

line 14 col 17: Use = to separate a named parameter from its default value.

line 70 col 39: Use = to separate a named parameter from its default value.

line 70 col 58: Use = to separate a named parameter from its default value.

line 77 col 25: Use = to separate a named parameter from its default value.

line 77 col 59: Use = to separate a named parameter from its default value.

Fix lib/src/request.dart. (-1.99 points)

Analysis of lib/src/request.dart reported 4 hints:

line 24 col 20: Use = to separate a named parameter from its default value.

line 25 col 17: Use = to separate a named parameter from its default value.

line 26 col 19: Use = to separate a named parameter from its default value.

line 27 col 15: Use = to separate a named parameter from its default value.

Fix lib/src/base.dart. (-1.49 points)

Analysis of lib/src/base.dart reported 3 hints:

line 38 col 17: Use = to separate a named parameter from its default value.

line 40 col 26: Use = to separate a named parameter from its default value.

line 43 col 38: Use = to separate a named parameter from its default value.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
http ^0.11.0 0.11.3+17 0.12.0+1
logging ^0.11.3 0.11.3+2
meta ^1.1.2 1.1.7
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.5.4
string_scanner 1.0.4
term_glyph 1.1.0
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