authenticated_http_client 0.2.6 copy "authenticated_http_client: ^0.2.6" to clipboard
authenticated_http_client: ^0.2.6 copied to clipboard

An advanced authenticated HTTP client introduces `factory` feature that generates request futures from API definitions in WYSIWYG style, with extra support for `mock`, `silent`.

example/example.dart

import 'package:authenticated_http_client/authenticated_http_client.dart';
import 'package:authenticated_http_client/http_headers_interceptor.dart';
import 'package:authenticated_http_client/router_helper.dart';

class Constants {
  static String appVersion = "1.0.1";
  static String systemCode = "f08511c8-3b1c-4008-abba-045787f0b6c0";
  static String udid = "357292741221214";
}

class CustomHttpHeadersInterceptor extends HttpHeadersInterceptor {
  @override
  Map<String, String> headersInterceptor(Map<String, String> headers) {
    headers["udid"] = Constants.udid;
    headers["version"] = Constants.appVersion;
    headers["system-code"] = Constants.systemCode;
    return headers;
  }
}

void main() {
  RouterHelper.init(
      unAuthCode: "101|103",
      jump2LoginCallback: () {/* navigate to login route */});

  AuthenticatedHttpClient.getInstance().init("https://api.company.com",
      customHttpHeadersInterceptor: CustomHttpHeadersInterceptor());

  var apiService = AuthenticatedHttpClient.getInstance().factory({
    "login": "POST /api/sign-in",
    "requestName": "POST /api/submit/plan",
    "requestNameWithParams":
        "GET /api/plan/:id/details", // or "GET /api/plan/{id}/details"
    "mockRequest":
        "MOCK POST /api/task/config", // mock from _post_api_task_config.json under mockDirectory /lib/mock
    "silentRequest":
        "SILENT GET /api/message/check/unread", // silent request won't jump when response met unauthorized or under maintenance
    "download": "DOWN https://assets.xxx.com/path/of/file",
    "uploadFile": "UP /api/file"
  });

  apiService
      .login({"username": "demo", "passwords": "test123"}).then((response) {
    // response here in format {code, message, data}
    final {"auth_token": authToken, "expired_at": expiredAt} = response["data"];
    AuthenticatedHttpClient.getInstance().setAuthToken(authToken);
  });
  apiService.requestName().then((response) {/* success */}).catchError(
      (e, stackTrace) {/* fail */}).whenComplete(() {/* finally */});
  apiService.requestNameWithParams({"id": 9527}).then((response) {
    /* success */
  }).catchError((e, stackTrace) {/* fail */}).whenComplete(() {/* finally */});

  void onReceiveProgress(int received, int total) {
    print("Downloading Progress $received/$total");
  }

  String savePath = "/local/path/for/download";
  // if Authorization is not required, set authenticate: false
  apiService
      .download(null,
          savePath: savePath,
          authenticate: false,
          onReceiveProgress: onReceiveProgress)
      .then((bytes) {/* success */}).catchError((e, stackTrace) {
    /* fail */
  }).whenComplete(() {/* finally */});

  String localPath = "/local/path/for/upload";
  apiService.uploadFile({"file": localPath}, throttling: true).then((response) {
    /* success */
  }).catchError((e, stackTrace) {/* fail */}).whenComplete(() {/* finally */});

  /// AuthenticatedHttpClient.all is a static function,similar to Promise.all,
  /// which introduces a delay feature to prevent potential server concurrency issues
  List<Future> futures = [
    apiService.requestName(),
    apiService.requestNameWithParams({"id": 9528})
  ];
  AuthenticatedHttpClient.all(futures, delayMillis: 350).then((results) {
    print(results['0']); // response of No.1 request
    print(results['1']); // response of No.2 request
  });
}
1
likes
150
points
169
downloads

Publisher

unverified uploader

Weekly Downloads

An advanced authenticated HTTP client introduces `factory` feature that generates request futures from API definitions in WYSIWYG style, with extra support for `mock`, `silent`.

Repository (GitHub)
View/report issues

Topics

#auth-http-client #http-interceptor #http-client #wysiwyg #mock-response

Documentation

API reference

License

MIT (license)

Dependencies

flutter, http, http_interceptor, shared_preferences, universal_io

More

Packages that depend on authenticated_http_client