Giter VIP home page Giter VIP logo

chopper's Introduction

Build Status codecov

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

Usage

Generator

Add the generator to your dev dependencies

dependencies:
  chopper: ^[latest version]

dev_dependencies:
  build_runner: ^[latest version]
  chopper_generator: ^[latest version]

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");

if (response.isSuccessful) {
  final body = response.body;
} else {
  final error = response.error;
}

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 {
       logger.log(request.body);
       return request;
     }
   ]
);

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 {
       logger.log(response.body);
       return response;
     }
   ]
);

Interceptors should not change the body of the request or response, use converters instead

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

chopper's People

Contributors

charafau avatar edwardaux avatar kavantix avatar lejard-h avatar matmaul avatar rinlv avatar xiprox avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.