jaguar_retrofit 2.5.12

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

jaguar_http

An HTTP client API generator inspired by Retrofit for Dart.

TODO

  • UrlEncodedForm
  • Multipart form

Install

dependencies:
  jaguar_retrofit:

dev_dependencies:
  jaguar_retrofit_gen:
  build_runner:

Defining and ApiClient

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

  final SerializerRepo serializers;

  UserApi({this.base, this.serializers});

  @GetReq("/users/:id")
  Future<User> getUserById(String id);

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

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

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

  @GetReq("/users")
  Future<List<User>> all({String name, String email});
}

Generate

pub run build_runner build

Use it

  var api = UserApi(base: route("http://localhost:10000"), serializers: repo);
  User user5 = await api
        .createUser(User(id: '5', name: 'five', email: 'five@five.com'));

Changelog

2.5.1

  • Added PathParam annotation

example/example.dart

library jaguar_http.example;

import 'dart:io';
import 'dart:async';
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;

part 'example.jretro.dart';

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

  final SerializerRepo serializers;

  UserApi({this.base, this.serializers});

  @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);
}

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(
      base: route("http://localhost:10000")
        ..before((route) {
          print("Metadata: ${route.metadataMap}");
        }),
      serializers: 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');
  } 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.5.12

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.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
2.5.9 Nov 2, 2018 Go to the documentation of jaguar_retrofit 2.5.9 Download jaguar_retrofit 2.5.9 archive
2.5.8 Oct 30, 2018 Go to the documentation of jaguar_retrofit 2.5.8 Download jaguar_retrofit 2.5.8 archive
2.5.6 Oct 30, 2018 Go to the documentation of jaguar_retrofit 2.5.6 Download jaguar_retrofit 2.5.6 archive
2.5.5 Oct 2, 2018 Go to the documentation of jaguar_retrofit 2.5.5 Download jaguar_retrofit 2.5.5 archive
2.5.4 Sep 10, 2018 Go to the documentation of jaguar_retrofit 2.5.4 Download jaguar_retrofit 2.5.4 archive
2.5.2 Sep 10, 2018 Go to the documentation of jaguar_retrofit 2.5.2 Download jaguar_retrofit 2.5.2 archive
2.5.1 Sep 10, 2018 Go to the documentation of jaguar_retrofit 2.5.1 Download jaguar_retrofit 2.5.1 archive
2.4.4 Sep 7, 2018 Go to the documentation of jaguar_retrofit 2.4.4 Download jaguar_retrofit 2.4.4 archive

All 15 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
79
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]
86
Learn more about scoring.

We analyzed this package on Nov 14, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.6

Platforms

Detected platforms: Flutter, other

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

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
jaguar_resty ^2.8.18 2.8.18
jaguar_serializer ^2.2.4 2.2.4
meta ^1.1.5 1.1.6
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.4.1
string_scanner 1.0.4
typed_data 1.1.6
Dev dependencies
build_runner any
jaguar ^2.2.20
jaguar_retrofit_gen
jaguar_serializer_cli ^2.2.0
test ^1.3.0