-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: redirect policy and request streaming
- Loading branch information
Showing
7 changed files
with
229 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import 'package:fetch_client/fetch_client.dart'; | ||
import 'package:http/http.dart'; | ||
|
||
|
||
void main() async { | ||
final client = FetchClient( | ||
mode: RequestMode.cors, | ||
redirectPolicy: RedirectPolicy.probeHead, // or RedirectPolicy.probe | ||
); | ||
final uri = Uri.https('jsonplaceholder.typicode.com', 'guide'); | ||
final response = await client.send( | ||
Request('GET', uri)..followRedirects = false, | ||
); | ||
|
||
print(response.headers['location']); | ||
} |
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,48 @@ | ||
import 'dart:convert'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:fetch_client/fetch_client.dart'; | ||
import 'package:http/http.dart'; | ||
|
||
|
||
void main(List<String> args) async { | ||
final client = FetchClient( | ||
mode: RequestMode.cors, | ||
streamRequests: true, | ||
); | ||
|
||
final uri = Uri.https('api.restful-api.dev', 'objects'); | ||
|
||
final stream = (() async* { | ||
yield Uint8List.fromList( | ||
''' | ||
{ | ||
"name": "My cool data", | ||
"data": { | ||
"data_part_1": "part_1", | ||
'''.codeUnits, | ||
); | ||
await Future<void>.delayed(const Duration(seconds: 1)); | ||
yield Uint8List.fromList( | ||
''' | ||
"data_part_2": "part_2" | ||
} | ||
} | ||
'''.codeUnits, | ||
); | ||
})(); | ||
|
||
final request = StreamedRequest('POST', uri)..headers.addAll({ | ||
'content-type': 'application/json', | ||
}); | ||
|
||
stream.listen( | ||
request.sink.add, | ||
onDone: request.sink.close, | ||
onError: request.sink.addError, | ||
); | ||
|
||
final response = await client.send(request); | ||
|
||
print(await utf8.decodeStream(response.stream)); | ||
} |
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,28 @@ | ||
import 'package:fetch_api/fetch_api.dart' show RequestRedirect, ResponseInstanceMembers; | ||
|
||
import 'fetch_client.dart'; | ||
import 'fetch_response.dart'; | ||
|
||
|
||
/// How [FetchClient] should handle redirects. | ||
enum RedirectPolicy { | ||
/// Default policy - always follow redirects. | ||
/// If redirect is occurred the only way to know about it is via | ||
/// [FetchResponse.redirected] and [FetchResponse.url]. | ||
alwaysFollow, | ||
/// Probe via HTTP `GET` request. | ||
/// | ||
/// Is this mode request is made with [RequestRedirect.manual], with | ||
/// no redirects, normal response is returned as usual. | ||
/// | ||
/// If redirect is occurred additional `GET` request will be send and canceled | ||
/// before body will be available. Returning response with only headers and | ||
/// artificial `Location` header crafted from [ResponseInstanceMembers.url]. | ||
/// | ||
/// Note that such response will always be crafted as `302 Found` and there's | ||
/// no way to get intermediate redirects, so you will get target redirect as | ||
/// if the original server returned it. | ||
probe, | ||
/// Same as [probe] but using `HEAD` method and therefore no cancel is needed. | ||
probeHead; | ||
} |
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