jaguar_retrofit 2.8.6

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

jaguar_retrofit #

An HTTP client API generator inspired by Retrofit for Dart.

Install #

dependencies:
  jaguar_retrofit:

dev_dependencies:
  jaguar_retrofit_gen:
  build_runner:

Defining and ApiClient #

/// Example showing how to define an [ApiClient]
/// Example showing how to define an [ApiClient]
@GenApiClient(path: "user")
class UserApi extends ApiClient with _$UserApiClient {
  final resty.Route base;

  UserApi(this.base);

  @GetReq(path: ":id")
  Future<User> getUserById(@PathParam() String id);

  @GetReq()
  Future<List<User>> all();

  @PostReq()
  Future<User> createUser(@AsJson() User user);

  @PutReq(path: ":id")
  Future<User> updateUser(String id, @AsJson() User user);

  @DeleteReq(path: ":id")
  Future<void> deleteUser(String id);

  @PostReq(path: "/login")
  Future<void> login(@AsForm() Login login);

  @PatchReq(path: "/avatar")
  Future<void> avatar(@AsBody() List<int> data);

  @PostReq()
  Future<User> serialize(@Serialized(MimeTypes.json) User data);
}

Generate #

pub run build_runner build

Use it #

  final repo = JsonRepo()..add(UserSerializer());
  final api = UserApi(base: route("http://localhost:10000"))..jsonConverter = repo;;
  User user = await api
        .createUser(User(id: '5', name: 'five', email: 'five@five.com'));

Changelog #

2.6.5 #

  • Added AsBodyString annotation

2.5.1 #

  • Added PathParam annotation

example/example.dart

library jaguar_http.example;

import 'dart:async';
import 'dart:io' show exit;
import 'package:http/io_client.dart';
import 'package:jaguar_retrofit/jaguar_retrofit.dart';
import 'package:jaguar_serializer/jaguar_serializer.dart';
import 'models/user.dart';
import 'package:jaguar_resty/jaguar_resty.dart' as resty;
import 'package:jaguar_resty/jaguar_resty.dart';
import 'package:jaguar/jaguar.dart' as jaguar;
import 'package:jaguar_mimetype/jaguar_mimetype.dart';

part 'example.jretro.dart';

/// Example showing how to define an [ApiClient]
@GenApiClient(path: "user")
class UserApi extends ApiClient with _$UserApiClient {
  final resty.Route base;

  UserApi(this.base);

  @GetReq(path: ":id")
  Future<User> getUserById(@PathParam() String id);

  @GetReq()
  Future<List<User>> all();

  @PostReq()
  Future<User> createUser(@AsJson() User user);

  @PutReq(path: ":id")
  Future<User> updateUser(@PathParam() String id, @AsJson() User user);

  @DeleteReq(path: ":id")
  Future<void> deleteUser(@PathParam() String id);

  @PostReq(path: "/login")
  Future<void> login(@AsForm() Login login);

  @PatchReq(path: "/avatar")
  Future<void> avatar(@AsBody() List<int> data);

  @PostReq()
  Future<User> serialize(@Serialized(MimeTypes.json) User data);
}

final repo = JsonRepo()..add(UserSerializer())..add(LoginSerializer());

void server() async {
  final users = <String, User>{};

  final server = jaguar.Jaguar(port: 10000);
  server.getJson('/user/:id', (c) => users[c.pathParams['id']]);
  server.getJson('/user', (c) => users.values.toList());
  server.postJson('/user', (c) async {
    User user = await c.bodyAsJson(convert: User.fromMap);
    users[user.id] = user;
    return user;
  });
  server.putJson('/user/:id', (c) async {
    User user = await c.bodyAsJson(convert: User.fromMap);
    users[user.id] = user;
    return user;
  });
  server.deleteJson('/user/:id', (c) => users.remove(c.pathParams['id']));
  server.postJson('/user/login', (c) async {
    Map<String, String> body = await c.bodyAsUrlEncodedForm();
    if (body['username'] == "teja" && body["password"] == "pass") {
      c.response = jaguar.Response("Success!");
    } else {
      c.response = jaguar.Response("Failed!", statusCode: 401);
    }
  });
  server.log.onRecord.listen(print);
  await server.serve(logRequests: true);
}

void client() async {
  globalClient = IOClient();
  var api = UserApi(route("http://localhost:10000"))..jsonConverter = repo;

  try {
    await api.login(Login(username: 'teja', password: 'pass'));

    // TODO await api.loginMultipart(Login(username: 'teja', password: 'pass'));

    User user5 = await api
        .createUser(User(id: '5', name: 'five', email: 'five@five.com'));
    print('Created $user5');
    User user10 =
        await api.createUser(User(id: '10', name: 'ten', email: 'ten@ten.com'));
    print('Created $user10');
    user5 = await api.getUserById("5");
    print('Fetched $user5');
    List<User> users = await api.all();
    print('Fetched all users $users');
    user5 = await api.updateUser(
        '5', User(id: '5', name: 'Five', email: 'five@five.com'));
    print('Updated $user5');
    await api.deleteUser('5');
    users = await api.all();
    print('Deleted user $users');
    User user11 = await api
        .serialize(User(id: '11', name: 'eleven', email: 'eleven@eleven.com'));
    print('Created $user11');
  } on resty.Response catch (e) {
    print(e.body);
  }
}

main() async {
  await server();
  await client();
  exit(0);
}

Use this package as a library

1. Depend on it

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


dependencies:
  jaguar_retrofit: ^2.8.6

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:jaguar_retrofit/jaguar_retrofit.dart';
  
Version Uploaded Documentation Archive
2.8.6 Jan 22, 2019 Go to the documentation of jaguar_retrofit 2.8.6 Download jaguar_retrofit 2.8.6 archive
2.8.5 Jan 14, 2019 Go to the documentation of jaguar_retrofit 2.8.5 Download jaguar_retrofit 2.8.5 archive
2.8.4 Jan 14, 2019 Go to the documentation of jaguar_retrofit 2.8.4 Download jaguar_retrofit 2.8.4 archive
2.8.2 Jan 2, 2019 Go to the documentation of jaguar_retrofit 2.8.2 Download jaguar_retrofit 2.8.2 archive
2.8.1 Jan 1, 2019 Go to the documentation of jaguar_retrofit 2.8.1 Download jaguar_retrofit 2.8.1 archive
2.6.5 Dec 28, 2018 Go to the documentation of jaguar_retrofit 2.6.5 Download jaguar_retrofit 2.6.5 archive
2.6.4 Dec 27, 2018 Go to the documentation of jaguar_retrofit 2.6.4 Download jaguar_retrofit 2.6.4 archive
2.6.2 Dec 27, 2018 Go to the documentation of jaguar_retrofit 2.6.2 Download jaguar_retrofit 2.6.2 archive
2.6.1 Dec 10, 2018 Go to the documentation of jaguar_retrofit 2.6.1 Download jaguar_retrofit 2.6.1 archive
2.5.12 Nov 13, 2018 Go to the documentation of jaguar_retrofit 2.5.12 Download jaguar_retrofit 2.5.12 archive

All 24 versions...

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

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

  • Dart: 2.3.2
  • pana: 0.12.18

Platforms

Detected platforms: Flutter, other

Primary library: package:jaguar_retrofit/jaguar_retrofit.dart with components: io.

Health suggestions

Fix lib/annotations/requests.dart. (-2.96 points)

Analysis of lib/annotations/requests.dart reported 6 hints, including:

line 11 col 46: Use = to separate a named parameter from its default value.

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

line 23 col 46: Use = to separate a named parameter from its default value.

line 29 col 49: Use = to separate a named parameter from its default value.

line 35 col 47: Use = to separate a named parameter from its default value.

Fix lib/annotations/params.dart. (-1 points)

Analysis of lib/annotations/params.dart reported 2 hints:

line 3 col 8: Unused import: 'package:jaguar_resty/jaguar_resty.dart'.

line 93 col 36: Use = to separate a named parameter from its default value.

Fix lib/client/client.dart. (-1 points)

Analysis of lib/client/client.dart reported 2 hints:

line 23 col 7: DO use curly braces for all flow control structures.

line 35 col 7: DO use curly braces for all flow control structures.

Fix lib/annotations/annotations.dart. (-0.50 points)

Analysis of lib/annotations/annotations.dart reported 1 hint:

line 9 col 47: Use = to separate a named parameter from its default value.

Maintenance suggestions

The package description is too short. (-9 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.65 <3.0.0
http ^0.12.0 0.12.0+2
jaguar_mimetype ^1.0.0 1.0.1
jaguar_resty ^2.10.10 2.10.12
jaguar_serializer ^2.2.4 2.2.12
meta ^1.1.5 1.1.7
Transitive dependencies
async 2.2.0
auth_header 2.1.2
charcode 1.1.2
client_cookie 2.2.1
collection 1.14.11
http_parser 3.1.3
path 1.6.2
pedantic 1.7.0
source_span 1.5.5
string_scanner 1.0.4
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
build_runner any
jaguar ^2.4.1
jaguar_retrofit_gen
jaguar_serializer_cli ^2.2.0
test ^1.3.0