-
-
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.
- Loading branch information
Showing
7 changed files
with
166 additions
and
156 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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
## 3.3.2 | ||
|
||
* More readability for client source codes. | ||
|
||
## 3.3.1 | ||
|
||
* Remove `testing` library annotations. | ||
|
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,73 @@ | ||
import 'dart:math' show Random; | ||
|
||
import 'package:http/http.dart' | ||
hide delete, get, head, patch, post, put, read, readBytes, runWithClient; | ||
import 'package:http/testing.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
import '../disguise_ua/disguise_ua.dart'; | ||
import '../exception/content_type_mismatched.dart'; | ||
import '../fetch/fetch.dart' show MetaFetch; | ||
|
||
part 'mock_client.dart'; | ||
|
||
/// [Client] implementation for OgHref packages. | ||
@internal | ||
final class OgHrefClient extends BaseClient { | ||
/// Default request user agent value. | ||
static const String DEFAULT_USER_AGENT_STRING = "oghref/2"; | ||
|
||
/// Default timeout duration in seconds. | ||
static const int DEFAULT_TIMEOUT = 10; | ||
|
||
/// Allow redirection if necessary. | ||
final bool redirect; | ||
|
||
final Client _c = Client(); | ||
|
||
/// Current user agent preference of following requests. | ||
static String _userAgent = DEFAULT_USER_AGENT_STRING; | ||
|
||
/// Specify new value of user agent if offered. | ||
static set userAgent(String value) { | ||
_userAgent = value; | ||
} | ||
|
||
/// Return current user agent string. | ||
/// | ||
/// If [disguise] enabled, it returns [disguisedUserAgent] instead of | ||
/// user defined. | ||
static String get userAgent { | ||
if (disguise) { | ||
try { | ||
return disguisedUserAgent; | ||
} on UnsupportedError { | ||
// Mostly triggered if running in VM. | ||
} | ||
} | ||
|
||
return _userAgent; | ||
} | ||
|
||
/// Specify timeout of response after specific seconds. | ||
static int timeoutAt = DEFAULT_TIMEOUT; | ||
|
||
/// If implemented under HTML, [userAgent] will be replaced by browser | ||
/// provided one when making request. | ||
static bool disguise = true; | ||
|
||
OgHrefClient(this.redirect); | ||
|
||
@override | ||
Future<StreamedResponse> send(BaseRequest request) { | ||
request | ||
..headers["user-agent"] = userAgent | ||
..followRedirects = redirect; | ||
return _c.send(request).timeout(Duration(seconds: timeoutAt)); | ||
} | ||
|
||
@override | ||
void close() { | ||
_c.close(); | ||
} | ||
} |
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
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