hio 0.3.6

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

hio #

Build Status Pub

Enhanced Http Client for Dart/Flutter.

Getting Started #

This project is a starting point for a Dart package, a library module containing code that can be shared easily across multiple Flutter or Dart projects.

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Add dependency #

dependencies:
  hio: x.x.x  #latest version

Usages #

Simple get #

import 'package:hio/hio.dart';

void getRepos(String username) async {
  try {
    var api = new Api<ApiOpt>()
      ..debugHeader = true
      ..baseUrl = "https://api.github.com/";

    api('users/:user/repos', urlParams: {'user': username})
      ..successCB = (data, resp) {
        print('data: $data');
      }
      ..errorCB = (err, resp) {
        print('ERROR: $err');
      }
      ..go();
  } catch (e) {
    return print(e);
  }
}

Multiple calls, and use global successCB and errorCB

void getSomething(String username) async {
    var api = new Api<ApiOpt>()
      ..debugHeader = true
      ..baseUrl = "https://api.github.com/"
      // using a global callback to parse the json object.
      ..successCB = (data, resp) {
        print('data: $data');
      }
      ..errorCB = (err, resp) {
        print('ERROR: $err');
      };
    
    var c = api('users/:user/repos', method: 'GET', urlParams: {'user': username});
    
    await c.go();
    
    // second call here
    await c.go(urlParams: {'user': 'google'})
      // using special callback for data processing itself
      ..successCB = (data, resp) {
        print('data: $data');
      }
      ..errorCB = (err, resp) {
        print('ERROR: $err');
      };
}

Define your API class (swapi.co) #

class SwApi extends Api<ApiOpt> {
  SwApi() : super() {
    this
      ..debugHeader = true
      ..debugBody = false
      ..baseUrl = 'https://swapi.co/api/'
      ..addOnSendHandler((req, c) {
        return true;
      });
  }

  @override
  String toString() {
    return 'SwApi[]';
  }
}

// and use it:
void run() async {
    var api = SwApi();

    var x = api('people/:id', method: 'GET', urlParams: {'id': 1});
    await x.go();

    await x.go('people/:id', urlParams: {'id': 2});
}

Pre-processing the received data (implements GitHub Api) #


class GithubOpt extends ApiOpt {
  int xRateLimitLimit = 60;
  int xRateLimitRemaining = 59;
  int xRateLimitReset = 1547617535;
  String xGitHubRequestId;
  Map<String, String> links;

  @override
  String toString() {
    return 'GithubApiCall[xRateLimitLimit:$xRateLimitLimit, xRateLimitRemaining:$xRateLimitRemaining, xRateLimitReset: $xRateLimitReset, ${toStringPart()}]';
  }

  GithubOpt();

  GithubOpt.init(
      String method,
      String url,
      Map<String, String> headers,
      Map<String, dynamic> urlParams,
      Map<String, dynamic> queryParams,
      Map<String, dynamic> bodyParams)
      : super.init(method, url, headers, urlParams, queryParams, bodyParams);

//  factory GithubApiCall.create(
//          String method,
//          String url,
//          Map<String, String> headers,
//          Map<String, dynamic> urlParams,
//          Map<String, dynamic> queryParams,
//          Map<String, dynamic> bodyParams) =>
//      GithubApiCall.init(
//          method, url, headers, urlParams, queryParams, bodyParams);
}

class GithubApi extends Api<GithubOpt> {
  GithubApi() : super() {
    this
      ..debugHeader = true
      ..debugBody = false
      ..baseUrl = 'https://api.github.com/'
      ..accept = 'application/vnd.github.v3+json'
      ..addOnSendHandler((req, c) => true)
      ..addOnProcessDataHandler(
          (dynamic data, HttpClientResponse resp, GithubOpt cc) {
        resp.headers.forEach((k, values) {
          // 'cache-control',
          //GithubApiCall cc = c;
          cc.links ??= <String, String>{};
          if (k == 'link') {
            for (var ss1 in values[0].split(',')) {
              print('$ss1');
              var ss2 = ss1.split(';');
              var link = trim(ss2[0].trim(), chars: '<>');
              var rel = ss2[1].trim();
              if (rel.startsWith('rel=')) rel = rel.substring(4);
              rel = trim(rel, chars: '"');
              cc.links[rel] = link;
            }
          } else if (k == 'x-ratelimit-limit') {
            cc.xRateLimitLimit = int.tryParse(values[0]) ?? 0;
          } else if (k == 'x-ratelimit-remaining') {
            cc.xRateLimitRemaining = int.tryParse(values[0]) ?? 0;
          } else if (k == 'x-ratelimit-reset') {
            cc.xRateLimitReset = int.tryParse(values[0]) ?? 0;
          } else if (k == 'x-github-request-id') {
            cc.xGitHubRequestId = values[0];
          }
        });
        return data;
      });
  }

  @override
  String toString() {
    return 'GithubApi[]';
  }

  @override
  ApiBroker<GithubOpt> call(String apiEntry,
      {String method = 'GET',
      Map<String, String> headers,
      Map<String, dynamic> urlParams,
      Map<String, dynamic> queryParams,
      Map<String, dynamic> bodyParams,
      Map<String, dynamic> params = const {}}) {
    var opt = GithubOpt.init(
        method, getUrl(apiEntry), headers, urlParams, queryParams, bodyParams);
    print('opt: $opt, getUrl: ${getUrl(apiEntry)}');
    return create(opt);
  }
}

// and use it:
void run() async {
    var api = GithubApi();
    var x = api('users/:user/repos', urlParams: {'user': 'hedzr'})
      ..successCB = (data, resp, c) {
        print('data received: $data');
        print('links: ${c.links}');
      }
      ..errorCB = (err, resp, c) {
        print('ERROR: $err');
      };

    await x.go();

    // make the second call:
    await x.go('users/:user/repos', method: 'GET', urlParams: {'user': 'google'});
}

[0.0.1] - TODO: Add release date.

  • TODO: Describe initial release.

example/readme.md

Example #

github API v3 demo.

Use this package as a library

1. Depend on it

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


dependencies:
  hio: ^0.3.6

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:hio/hio.dart';
  
Version Uploaded Documentation Archive
0.3.6 Jan 18, 2019 Go to the documentation of hio 0.3.6 Download hio 0.3.6 archive
0.3.5 Jan 18, 2019 Go to the documentation of hio 0.3.5 Download hio 0.3.5 archive
0.3.3 Jan 18, 2019 Go to the documentation of hio 0.3.3 Download hio 0.3.3 archive
0.3.2 Jan 18, 2019 Go to the documentation of hio 0.3.2 Download hio 0.3.2 archive
0.3.1 Jan 17, 2019 Go to the documentation of hio 0.3.1 Download hio 0.3.1 archive
0.2.9 Jan 17, 2019 Go to the documentation of hio 0.2.9 Download hio 0.2.9 archive
0.2.7 Jan 17, 2019 Go to the documentation of hio 0.2.7 Download hio 0.2.7 archive
0.2.5 Jan 17, 2019 Go to the documentation of hio 0.2.5 Download hio 0.2.5 archive
0.2.3 Jan 17, 2019 Go to the documentation of hio 0.2.3 Download hio 0.2.3 archive
0.2.1 Jan 17, 2019 Go to the documentation of hio 0.2.1 Download hio 0.2.1 archive

All 13 versions...

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

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

  • Dart: 2.2.0
  • pana: 0.12.14

Platforms

Detected platforms: Flutter, other

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

Health issues and suggestions

Document public APIs. (-0.11 points)

63 out of 65 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/src/tools.dart. (-1 points)

Analysis of lib/src/tools.dart reported 2 hints:

line 8 col 10: The class 'Future' was not exported from 'dart:core' until version 2.1, but this code is required to be able to run on earlier versions.

line 18 col 5: The class 'Future' was not exported from 'dart:core' until version 2.1, but this code is required to be able to run on earlier versions.

Fix lib/src/api_call.dart. (-0.50 points)

Analysis of lib/src/api_call.dart reported 1 hint:

line 77 col 3: The class 'Future' was not exported from 'dart:core' until version 2.1, but this code is required to be able to run on earlier versions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
Dev dependencies
pedantic ^1.4.0
test ^1.5.0