Skip to content

Commit

Permalink
Support Timeout, Debug Label For IsolateHttp
Browse files Browse the repository at this point in the history
  • Loading branch information
thongdn-it committed Sep 12, 2021
1 parent 5313f8e commit 7cbf10a
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 10 deletions.
4 changes: 4 additions & 0 deletions isolate_http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

# [2.1.0] - 12/09/2021

* Support timeout, debug label.

# [2.0.0] - 03/09/2021

* Support null-safety.
Expand Down
8 changes: 8 additions & 0 deletions isolate_http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions isolate_http/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:lints/recommended.yaml
7 changes: 4 additions & 3 deletions isolate_http/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import 'package:isolate_http/isolate_http.dart';

void main(List<String> 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);
Expand Down
33 changes: 29 additions & 4 deletions isolate_http/lib/src/isolate_http.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:http/http.dart';

import 'package:isolate_flutter/isolate_flutter.dart';
Expand All @@ -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<IsolateHttpResponse?> head(String url,
Expand Down Expand Up @@ -71,9 +85,20 @@ class IsolateHttp {

/// Sends an [IsolateHttpRequest] and returns the [IsolateHttpResponse].
Future<IsolateHttpResponse?> 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 ?? {});
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions isolate_http/lib/src/isolate_http_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +28,19 @@ class IsolateHttpRequest {
/// List of files to be uploaded of the request.
final List<HttpFile>? 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,
Expand All @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions isolate_http/lib/src/isolate_http_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>.
IsolateHttpResponse(this.body, this.statusCode, this.headers);

/// Return the body as as Json (dynamic).
Expand Down
9 changes: 8 additions & 1 deletion isolate_http/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:
Expand Down
7 changes: 5 additions & 2 deletions isolate_http/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -13,4 +13,7 @@ dependencies:

isolate_flutter: ^2.0.0
http: ^0.13.0
http_parser: ^4.0.0
http_parser: ^4.0.0

dev_dependencies:
lints: ^1.0.1

0 comments on commit 7cbf10a

Please sign in to comment.