ajanuw_http 0.5.1

Dart native js
Flutter Android iOS web

http interceptor in dart

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(
    Uri.parse('/'),
    AjanuwHttpConfig(params: {'name': 'Ajanuw'}),
  );
  print(r.body);
}

Get File #

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

var api = AjanuwHttp();
var url = 'https://i.loli.net/2020/01/14/w1dcNtf4SECG6yX.jpg';

void main() async {
  var r = await api.getStream(
    url,
    AjanuwHttpConfig(
      onDownloadProgress: (bytes, total) {
        print((bytes / total * 100).toInt().toString() + '%');
      },
    ),
  );
  var f$ = File('./test.jpg').openWrite();
  r.stream.listen(
    f$.add,
    onDone: () {
      f$.close();
      print('done.');
    },
  );
}

send file #

import 'dart:io';

import 'package:ajanuw_http/ajanuw_http.dart';

void main() async {
  var api = AjanuwHttp()..config.baseURL = 'http://localhost:3000/api';
  var r = await api.post(
    '/upload',
    AjanuwHttpConfig(
      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 {
    config.headers ??= {};

    if (config.method.toLowerCase() == 'post' && config.body is Map) {
      (config.body as Map)['x-key'] = 'key';
    }

    config.headers.addAll({'x-senduser': 'ajanuw'});
    return config;
  }

  @override
  Future<BaseResponse> 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('/', 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<Response>(() {
    return api.get('/retry').asStream().map((r) {
      if (r.statusCode != 200) return throw Stream.error(r);
      return r;
    });
  }, 5)
      .listen(
    (r) => print(r.body),
    onError: (er) => print(er),
  );
}

Fetch request error #

try {
  var r = await api.get('');
  print(r.body);
} catch (e) {
  print( (e as Response).body );
}

test #

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

See also: