api_client 2.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 66

api_client #

Build Status

A schema api's client

Set up Spec #

final Spec spec = Spec(
      endpoints: {
        "get_pet": get("{{api_url}}/{{version}}/pet/{{pet_id}}"),
      },
      parameters: {
        "api_url": "https://petstore.swagger.io",
        "version": "v2",
      },
      onSend: (Request request) {
        JsonRequestMiddleware(request);
        request.set("start_time", DateTime.now().millisecondsSinceEpoch);
      },
      onReceive: (Request request, Response response) {
        int startTime = request.get("start_time");
        int endTime = DateTime.now().millisecondsSinceEpoch;
        print(
            "Request ${response.httpResponse.request.url.toString()} is completed in ${endTime - startTime} (ms)");
      });

Call API #

spec.call("get_pet", parameters: {"pet_id": 123})
    .then((response) {
  if (response.statusCode == 200) {
     print(json.decode(utf8.decode(response.bodyBytes)));
  }
});

Function Arguments of call

  • name: (requires) name of spec
  • parameters: (optional) contextual parameters for current call
  • onSend, onReceive, onError: (optional) see Events

Events #

Applicable for initializing Spec and on each call

  • onSend(Request request) executes before request is sent
  • onReceive(Request request, Response response) executes after receiving response
  • onError(Request request, Response response, Exception exception) executes when there is an error

[2.2.0] - Improvement of Spec

  • Make endpoints, parameters, replacer and transporter become private
  • Set name as request's attribute
  • Make Replacer become abstract
  • Add Transporter.factory() and Replacer.factory()

[2.1.2] - Add onError on call

  • Execute onError
  • Update README.md

[2.1.1] - Fix exception handling

  • Add support for Error

[2.1.0] - Improve endpoint specification

  • Use verbs get, post, patch, put, delete
  • Update example
  • Update test cases

[2.0.1] - Improve example

  • Add flutter example
  • Export response.dart

[2.0.0] - New way for middlewares

  • Add onSend, onReceive and onError events
  • Update HttpTransporter

[1.0.2] - Version 1.x

  • Fix bugs

[0.0.1] - First release

  • Initialize first version

example/README.md

Example #

Define Specification #

import 'package:api_client/api_client.dart';

Spec spec = Spec(endpoints: {
  "get_item": get("{{api_url}}/items")
}, parameters: {
   "api_url": "https://api.domain.com"
});

Middlewares #

import 'package:api_client/api_client.dart';
Spec spec = Spec();
spec.onSend((Request request) {
  request.headers[Constants.HEADER_CONTENT_TYPE] =
    Constants.CONTENT_TYPE_JSON_UTF8;
});

Call APIs #

spec.call("get_item").then((response) {
  // do something with response
  // response.statusCode == 200
  // var body = json.decode(utf8.decode(response.bodyBytes));
});

Use this package as a library

1. Depend on it

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


dependencies:
  api_client: ^2.2.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:api_client/api_client.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
35
Health:
Code health derived from static analysis. [more]
97
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
66
Learn more about scoring.

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

  • Dart: 2.7.0
  • pana: 0.13.4

Health issues and suggestions

Document public APIs. (-1 points)

72 out of 72 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/src/event/error_event.dart. (-1.49 points)

Analysis of lib/src/event/error_event.dart reported 3 hints:

line 12 col 7: Don't type annotate initializing formals.

line 12 col 29: Don't type annotate initializing formals.

line 12 col 53: Don't type annotate initializing formals.

Fix lib/src/event/receive_response_event.dart. (-1 points)

Analysis of lib/src/event/receive_response_event.dart reported 2 hints:

line 10 col 24: Don't type annotate initializing formals.

line 10 col 46: Don't type annotate initializing formals.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
emitter ^1.0.0 1.0.4
http ^0.12.0 0.12.0+4
Transitive dependencies
async 2.4.0
charcode 1.1.2
collection 1.14.12
http_parser 3.1.3
meta 1.1.8
path 1.6.4
pedantic 1.9.0
source_span 1.6.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
coveralls ^5.1.0
mockito ^4.0.0
test ^1.3.4