http_xtra 0.1.1 copy "http_xtra: ^0.1.1" to clipboard
http_xtra: ^0.1.1 copied to clipboard

Http encapsulation to parse easily

HttpXtra #

HttpXtra is a package which aims to facilitate the use of the http package

Features #

HttpXtra parse http response in an easy way.

Usage #

import 'package:http_xtra/http_xtra.dart';

abstract class Api {
  static final HttpXtra client = HttpXtra(
    baseUrl: "https://my-base-url.com",
    onRefresh: (client, refreshToken) {
      if (refreshToken == null) return Future(() => null);
      return client.post<JsonBody, JsonBody>(
        "/refresh",
        body: {
          "refreshToken": refreshToken,
        },
      ).then(_setAuthorization);
    },
  );

  static Future<bool> isUp() => client.get<bool, String>(
        "/health",
        parser: (value) => value == "Up",
      );

  static Future<void> login(String email, String password) =>
      client.post<JsonBody, JsonBody>(
        "/login",
        body: {
          "email": email,
          "password": password,
        },
      ).then(_setAuthorization);

  static void _setAuthorization(JsonBody tokens) {
    client.setAuthorization(tokens['accessToken'],
        refreshToken: tokens['refreshToken']);
  }

  static Future<JsonBody> me() => client.get("/me");
}

void main() async {
  final bool isUp = await Api.isUp();

  if (isUp == false) return;

  await Api.login("my-email", "my-password");

  final Map<String, dynamic> me = await Api.me();

  print(me);
}
0
likes
140
points
162
downloads

Publisher

unverified uploader

Weekly Downloads

Http encapsulation to parse easily

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

http

More

Packages that depend on http_xtra