diff --git a/isolate_http/CHANGELOG.md b/isolate_http/CHANGELOG.md index 06f3f37..27d5688 100644 --- a/isolate_http/CHANGELOG.md +++ b/isolate_http/CHANGELOG.md @@ -1,4 +1,8 @@ +# [2.1.0] - 12/09/2021 + +* Support timeout, debug label. + # [2.0.0] - 03/09/2021 * Support null-safety. diff --git a/isolate_http/README.md b/isolate_http/README.md index cfd2218..7198b5d 100644 --- a/isolate_http/README.md +++ b/isolate_http/README.md @@ -34,6 +34,14 @@ final _response = await IsolateHttp().delete('https://example.com/product/1', print(_response); ``` +*** You can set a timeout and debug label for your request when creating an IsolateHttp like: + +```dart +IsolateHttp(timeout: Duration(seconds: 30), debugLabel: 'get_products') +``` + +If timeout, its returns you an IsolateHttpResponse with status code 408 (Request Timeout). + ## Author IsolateHttp is developed by Thong Dang. You can contact me at thongdn.it@gmail.com diff --git a/isolate_http/analysis_options.yaml b/isolate_http/analysis_options.yaml new file mode 100644 index 0000000..ea2c9e9 --- /dev/null +++ b/isolate_http/analysis_options.yaml @@ -0,0 +1 @@ +include: package:lints/recommended.yaml \ No newline at end of file diff --git a/isolate_http/example/main.dart b/isolate_http/example/main.dart index 56cc3f4..036d403 100644 --- a/isolate_http/example/main.dart +++ b/isolate_http/example/main.dart @@ -2,9 +2,10 @@ import 'package:isolate_http/isolate_http.dart'; void main(List args) async { // https://developers.google.com/books - final _response = await IsolateHttp().get( - 'https://www.googleapis.com/auth/books/v1/volumes', - query: {'q': 'flutter'}); + final _response = await IsolateHttp( + timeout: Duration(seconds: 60), debugLabel: 'search_book') + .get('https://www.googleapis.com/auth/books/v1/volumes', + query: {'q': 'flutter'}); if (_response?.statusCode == 200) { final _bodyJson = _response?.bodyJson; print(_bodyJson); diff --git a/isolate_http/lib/src/isolate_http.dart b/isolate_http/lib/src/isolate_http.dart index cc2efd5..6a91d3c 100644 --- a/isolate_http/lib/src/isolate_http.dart +++ b/isolate_http/lib/src/isolate_http.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:http/http.dart'; import 'package:isolate_flutter/isolate_flutter.dart'; @@ -11,7 +13,19 @@ import 'package:isolate_http/src/isolate_http_response.dart'; /// /// [head], [get], [post], [put], [delete], [send] class IsolateHttp { - const IsolateHttp(); + final Duration? _timeout; + final String? _debugLabel; + + /// Create an IsolateHttp + /// + /// [timeout] time limit for an request. + /// If this request does not complete before [timeout] has passed, + /// Its returns you an IsolateHttpResponse with status code 408 (Request Timeout). + /// + /// [debugLabel] this name in debuggers and logging for IsolateHttp. + IsolateHttp({Duration? timeout, String? debugLabel}) + : _timeout = timeout, + _debugLabel = debugLabel; /// Sends an HTTP HEAD request with the given headers to the given URL. Future head(String url, @@ -71,9 +85,20 @@ class IsolateHttp { /// Sends an [IsolateHttpRequest] and returns the [IsolateHttpResponse]. Future send(IsolateHttpRequest request) async { - final _isolateHttpResponse = - await IsolateFlutter.createAndStart(_call, request); - return _isolateHttpResponse; + try { + final _isolateRequest = IsolateFlutter.createAndStart(_call, request, + debugLabel: _debugLabel); + + if (_timeout == null) { + return await _isolateRequest; + } else { + return await _isolateRequest.timeout(_timeout!); + } + } on TimeoutException catch (e) { + return IsolateHttpResponse(e.toString(), 408, request.headers ?? {}); + } on Exception catch (e) { + return IsolateHttpResponse(e.toString(), 520, request.headers ?? {}); + } } } diff --git a/isolate_http/lib/src/isolate_http_request.dart b/isolate_http/lib/src/isolate_http_request.dart index 58dcc6a..7feb855 100644 --- a/isolate_http/lib/src/isolate_http_request.dart +++ b/isolate_http/lib/src/isolate_http_request.dart @@ -5,6 +5,7 @@ import 'package:http/http.dart'; import 'package:isolate_http/src/http_file.dart'; import 'package:isolate_http/src/http_method.dart'; +/// The request using for Isolate Http. class IsolateHttpRequest { /// The url to which the request will be sent. final String url; @@ -27,6 +28,19 @@ class IsolateHttpRequest { /// List of files to be uploaded of the request. final List? files; + /// The request using for Isolate Http. + /// + /// [url] The url to which the request will be sent. + /// + /// [method] The [HttpMethod] of the request. + /// + /// [query] List [queryParameters] in [http] + /// + /// [headers] The headers of the request. + /// + /// [body] The body of the request. + /// + /// [files] List of files (HttpFile) to be uploaded of the request. IsolateHttpRequest( this.url, { this.method = HttpMethod.get, @@ -36,6 +50,7 @@ class IsolateHttpRequest { this.files, }); + /// Convert [url] and [query] to full link request (Uri) Uri? get uri { String _requestUrl = url; if (query?.isNotEmpty == true) { diff --git a/isolate_http/lib/src/isolate_http_response.dart b/isolate_http/lib/src/isolate_http_response.dart index 7a82b50..83111b2 100644 --- a/isolate_http/lib/src/isolate_http_response.dart +++ b/isolate_http/lib/src/isolate_http_response.dart @@ -11,6 +11,13 @@ class IsolateHttpResponse { /// The status code of the response as int. final int statusCode; + /// The response using for Isolate Http. + /// + /// [body] The body of the response as a String. + /// + /// [statusCode] The status code of the response as int. + /// + /// [headers] The headers of the response as a Map. IsolateHttpResponse(this.body, this.statusCode, this.headers); /// Return the body as as Json (dynamic). diff --git a/isolate_http/pubspec.lock b/isolate_http/pubspec.lock index d8f89d7..eaeb4f8 100644 --- a/isolate_http/pubspec.lock +++ b/isolate_http/pubspec.lock @@ -42,7 +42,7 @@ packages: source: hosted version: "0.13.3" http_parser: - dependency: transitive + dependency: "direct main" description: name: http_parser url: "https://pub.dartlang.org" @@ -55,6 +55,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.0" + lints: + dependency: "direct dev" + description: + name: lints + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" meta: dependency: transitive description: diff --git a/isolate_http/pubspec.yaml b/isolate_http/pubspec.yaml index 0f21feb..30e9517 100644 --- a/isolate_http/pubspec.yaml +++ b/isolate_http/pubspec.yaml @@ -1,6 +1,6 @@ name: isolate_http description: IsolateHttp provides a way to launch 'http' library in Isolate with IsolatesFlutter. -version: 2.0.0 +version: 2.1.0 homepage: https://github.com/thongdn-it/isolate_flutter/tree/master/isolate_http repository: https://github.com/thongdn-it/isolate_flutter/tree/master/isolate_http @@ -13,4 +13,7 @@ dependencies: isolate_flutter: ^2.0.0 http: ^0.13.0 - http_parser: ^4.0.0 \ No newline at end of file + http_parser: ^4.0.0 + +dev_dependencies: + lints: ^1.0.1 \ No newline at end of file