ajanuw_http

Creator: coderz1093

Last updated:

0 purchases

ajanuw_http Image
ajanuw_http Images
Add to Cart

Description:

ajanuw http

A http interceptor in dart #
Install #
dependencies:
ajanuw_http:
copied to clipboard
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);
}
copied to clipboard
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.');
},
);
}
copied to clipboard
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);
}
copied to clipboard
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);
}
copied to clipboard
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),
);
}
copied to clipboard
Fetch request error #
try {
var r = await api.get('');
print(r.body);
} catch (e) {
print( (e as Response).body );
}
copied to clipboard
test #
> pub run test
> flutter pub run test
> pub run test .\test\ajanuw_http_test.dart
copied to clipboard
See also:

Examples

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.