ajanuw_http 0.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 60

A http interceptor in dart #

Install #

dependencies:
  ajanuw_http:

Get #

import 'package:ajanuw_http/ajanuw_http.dart';

void main() async {
  var api = AjanuwHttp()..config.baseURL = 'http://localhost:3000/api/';
  var r = await api.get('/cats');
  print(r.body);
}

Post #

import 'package:ajanuw_http/ajanuw_http.dart';

void main() async {
  var api = AjanuwHttp()..config.baseURL = 'http://localhost:3000/api/';
  var r2 = await api.post('/cats');
  print(r2.body);
}

send file #

import 'dart:io';

import 'package:ajanuw_http/ajanuw_http.dart';
import 'package:http/http.dart' show MultipartFile;
import 'package:http_parser/http_parser.dart' show MediaType;

void main() async {
import 'dart:io';

import 'package:ajanuw_http/ajanuw_http.dart';
import 'package:http/http.dart' show MultipartFile;
import 'package:http_parser/http_parser.dart' show MediaType;

void main() async {
  var api = AjanuwHttp()..config.baseURL = 'http://localhost:3000/api';

  var r = await api.post(
    '/upload',
    AjanuwHttpConfig(
      params: {'name': 'ajanuw'},
      body: {'data': '111'},
      files: [
        await MultipartFile.fromPath('file', './a.jpg'),
        MultipartFile.fromBytes(
          'file',
          await File('./a.jpg').readAsBytes(),
          contentType: MediaType('image', 'jpeg'),
          filename: 'a.jpg',
        ),
        MultipartFile.fromBytes(
          'file',
          await api
              .readBytes('https://i.loli.net/2019/10/01/CVBu2tNMqzOfXHr.png'),
          contentType: MediaType('image', 'png'),
          filename: 'CVBu2tNMqzOfXHr.png',
        ),
      ],
    ),
  );
  print(r.body);
}

Use interceptor #

import 'package:ajanuw_http/ajanuw_http.dart';
import 'package:http/http.dart';

class HeaderInterceptor extends AjanuwHttpInterceptors {
  @override
  Future<AjanuwHttpConfig> request(AjanuwHttpConfig config) async {
    if (config.method.toLowerCase() == 'post' && config.body is Map) {
      (config.body as Map)['x-key'] = '拦截器数据';
    }
    return config;
  }

  @override
  Future<Response> response(BaseResponse response) async {
    return response;
  }
}

void main() async {
  var api = AjanuwHttp()
    ..config.baseURL = 'http://localhost:3000/api/'
    ..interceptors.add(HeaderInterceptor());

  var r = await api.post('/cats', AjanuwHttpConfig(body: {'name': 'ajanuw'}));

  print(r.body);
}

Use rxdart for error retry #

import 'package:ajanuw_http/ajanuw_http.dart';
import 'package:rxdart/rxdart.dart';

void main() async {
  var api = AjanuwHttp()
    ..config.baseURL = 'http://localhost:3000/api/';

  Rx.retry(() {
    return Stream.fromFuture(api.get('/cats')).map((r) {
      print(r.statusCode);
      if (r.statusCode != 200) {
        throw Stream.error('send a err');
      }
      return r;
    });
  }, 3)
      .listen(
    (r) {
      print(r.body);
    },
    onError: (er) {
      // If all three fail
      print('Error: $er');
    },
  );
}

If you execute the above example, you may see the following result:

λ dart ./example/ajanuw_http_example.dart
403
403
200
Hello World!

test #

> pub run test
> pub run test .\test\ajanuw_http_test.dart

0.2.0 2020-4-1 #

  • 重大变更

0.1.1 2020-2-22 #

  • 添加错误重试example

0.1.0 #

  • A http interceptor in dart

example/ajanuw_http_example.dart

import 'package:ajanuw_http/ajanuw_http.dart';
import 'package:rxdart/rxdart.dart';

void main() async {
  var api = AjanuwHttp()
    ..config.baseURL = 'http://localhost:3000/api/';

  Rx.retry(() {
    return Stream.fromFuture(api.get('/cats')).map((r) {
      print(r.statusCode);
      if (r.statusCode != 200) {
        throw Stream.error('send a err');
      }
      return r;
    });
  }, 3)
      .listen(
    (r) {
      print(r.body);
    },
    onError: (er) {
      // If all three fail
      print('Error: $er');
    },
  );
}

Use this package as a library

1. Depend on it

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


dependencies:
  ajanuw_http: ^0.2.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:ajanuw_http/ajanuw_http.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
28
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]
60
Learn more about scoring.

We analyzed this package on Jul 10, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Health suggestions

Format lib/ajanuw_http.dart.

Run dartfmt to format lib/ajanuw_http.dart.

Maintenance suggestions

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
http ^0.12.0 0.12.1
Transitive dependencies
charcode 1.1.3
collection 1.14.13 1.15.0-nullsafety
http_parser 3.1.4
meta 1.2.2 1.3.0-nullsafety
path 1.7.0
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.2.0 1.3.0-nullsafety
Dev dependencies
pedantic ^1.8.0 1.9.2
rxdart any
test ^1.6.0