jaguar_retrofit 2.8.5

  • 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.5

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:jaguar_retrofit/jaguar_retrofit.dart';
  
Version Uploaded Documentation 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
2.5.10 Nov 12, 2018 Go to the documentation of jaguar_retrofit 2.5.10 Download jaguar_retrofit 2.5.10 archive

All 23 versions...

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

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

  • Dart: 2.1.0
  • pana: 0.12.10

Platforms

Detected platforms: Flutter, other

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

Health suggestions

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

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

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

Maintenance suggestions

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.65 <3.0.0
http ^0.12.0 0.12.0+1
jaguar_mimetype ^1.0.0 1.0.1
jaguar_resty ^2.10.8 2.10.8
jaguar_serializer ^2.2.4 2.2.6
meta ^1.1.5 1.1.7
Transitive dependencies
async 2.0.8
auth_header 2.1.2
charcode 1.1.2
client_cookie 2.1.6
collection 1.14.11
http_parser 3.1.3
path 1.6.2
source_span 1.5.1
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