-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from koji-1009/feat/http_client
feat: Remove http package
- Loading branch information
Showing
16 changed files
with
281 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import 'package:http/http.dart' as http; | ||
import 'package:taro/taro.dart'; | ||
|
||
/// [HttpHttp] is a class that performs GET requests using the http package | ||
class HttpHttp implements TaroHttpClient { | ||
/// Creates a [HttpClient]. | ||
const HttpHttp({ | ||
this.timeout = const Duration( | ||
seconds: 180, | ||
), | ||
}); | ||
|
||
final Duration timeout; | ||
|
||
@override | ||
Future<TaroHttpResponse> get({ | ||
required Uri uri, | ||
required Map<String, String> headers, | ||
}) async { | ||
final response = await http | ||
.get( | ||
uri, | ||
headers: headers, | ||
) | ||
.timeout(timeout); | ||
return ( | ||
statusCode: response.statusCode, | ||
bodyBytes: response.bodyBytes, | ||
reasonPhrase: response.reasonPhrase, | ||
contentLength: response.contentLength, | ||
headers: response.headers, | ||
isRedirect: response.isRedirect, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ dependencies: | |
sdk: flutter | ||
|
||
cupertino_icons: ^1.0.0 | ||
http: ^1.0.0 | ||
dio: ^5.0.0 | ||
|
||
taro: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import 'dart:async'; | ||
import 'dart:io'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:taro/src/taro_loader_network.dart'; | ||
|
||
final _httpClient = HttpClient()..autoUncompress = false; | ||
|
||
/// Load image data as [TaroHttpResponse] from the network. | ||
Future<TaroHttpResponse> get({ | ||
required Uri uri, | ||
required Map<String, String> headers, | ||
}) async { | ||
final request = await _httpClient.getUrl(uri); | ||
for (final entry in headers.entries) { | ||
request.headers.add(entry.key, entry.value); | ||
} | ||
final response = await request.close(); | ||
|
||
final responseBytes = await consolidateHttpClientResponseBytes(response); | ||
final responseHeaders = <String, String>{}; | ||
response.headers.forEach((key, values) { | ||
responseHeaders[key] = values.join(','); | ||
}); | ||
|
||
return ( | ||
statusCode: response.statusCode, | ||
bodyBytes: responseBytes, | ||
reasonPhrase: response.reasonPhrase, | ||
contentLength: response.contentLength, | ||
headers: responseHeaders, | ||
isRedirect: response.isRedirect, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export 'unsupported.dart' | ||
if (dart.library.ffi) 'native.dart' | ||
if (dart.library.html) 'web.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:taro/src/taro_loader_network.dart'; | ||
|
||
/// Load image data as [TaroHttpResponse] from the network. | ||
Future<TaroHttpResponse> get({ | ||
required Uri uri, | ||
required Map<String, String> headers, | ||
}) async => | ||
throw UnimplementedError(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import 'dart:async'; | ||
import 'dart:js_interop'; | ||
|
||
import 'package:taro/src/network/web_fetch.dart'; | ||
import 'package:taro/src/taro_loader_network.dart'; | ||
|
||
/// Load image data as [TaroHttpResponse] from the network. | ||
Future<TaroHttpResponse> get({ | ||
required Uri uri, | ||
required Map<String, String> headers, | ||
}) async { | ||
final requestHeaders = Headers(); | ||
for (final entry in headers.entries) { | ||
requestHeaders.append(entry.key, entry.value); | ||
} | ||
final response = await fetch( | ||
uri.toString(), | ||
requestHeaders, | ||
).toDart; | ||
|
||
final responseBuffer = await response.arrayBuffer().toDart; | ||
final responseBytes = responseBuffer.toDart.asUint8List(); | ||
final responseHeaders = <String, String>{}; | ||
response.headers.forEach((String value, String key, JSObject object) { | ||
responseHeaders[key] = value; | ||
}.toJS); | ||
|
||
return ( | ||
statusCode: response.status, | ||
bodyBytes: responseBytes, | ||
reasonPhrase: response.statusText, | ||
contentLength: responseBytes.length, | ||
headers: responseHeaders, | ||
isRedirect: response.redirected, | ||
); | ||
} |
Oops, something went wrong.