Skip to content

Instantly share code, notes, and snippets.

@johngorithm
Last active August 22, 2021 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johngorithm/c2ae6b7508dc6530f59318151febf27c to your computer and use it in GitHub Desktop.
Save johngorithm/c2ae6b7508dc6530f59318151febf27c to your computer and use it in GitHub Desktop.
This Rapper essentially makes it easy to test out code using the http dart library. It is plainly an abstraction to enhance testability
import 'dart:typed_data';
import 'package:http/http.dart' as http;
class HttpService {
Duration timeOutLimit = Duration(seconds: 15);
Future<http.Response> delete(Uri url, {Map<String, String>? headers, Object? body, Encoding? encoding}) {
return http.delete(url, headers: headers, body: body, encoding: encoding).timeout(timeOutLimit);
}
Future<http.Response> get(Uri url, {Map<String, String>? headers}) {
return http.get(url, headers: headers).timeout(timeOutLimit);
}
Future<http.Response> patch(Uri url, {Map<String, String>? headers, Object? body, Encoding? encoding}) {
return http.patch(url, headers: headers, body: body, encoding: encoding).timeout(timeOutLimit);
}
Future<http.Response> post(Uri url, {Map<String, String>? headers, Object? body, Encoding? encoding}) {
return http.post(url, headers: headers, body: body, encoding: encoding).timeout(timeOutLimit);
}
Future<http.Response> put(Uri url, {Map<String, String>? headers, Object? body, Encoding? encoding}) {
return http.put(url, headers: headers, body: body, encoding: encoding).timeout(timeOutLimit);
}
Future<Uint8List> readBytes(Uri url, {Map<String, String>? headers}) {
return http.readBytes(url, headers: headers).timeout(timeOutLimit);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment