Skip to content

Commit fa6eab0

Browse files
authored
Support overriding HTTP client #81
ref DEV-2890
2 parents b20fc4e + c389ec1 commit fa6eab0

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

example/lib/main.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:http/http.dart' as http;
12
import 'dart:async' show StreamSubscription;
23
import 'dart:io';
34
import 'dart:convert';
@@ -22,6 +23,18 @@ ColorScheme colorSchemeFromContext(BuildContext context) {
2223
}
2324
}
2425

26+
class MyHTTPClient extends http.BaseClient {
27+
final http.Client _inner;
28+
29+
MyHTTPClient(this._inner);
30+
31+
@override
32+
Future<http.StreamedResponse> send(http.BaseRequest request) {
33+
request.headers["x-custom-header"] = "42";
34+
return _inner.send(request);
35+
}
36+
}
37+
2538
void main() {
2639
if (Platform.isIOS) {
2740
wechatRedirectURI =
@@ -917,6 +930,7 @@ class _MyAppState extends State<MyApp> {
917930
),
918931
)
919932
: null,
933+
httpClient: MyHTTPClient(http.Client()),
920934
);
921935
_sub?.cancel();
922936
await authgear.configure();

lib/src/container.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ class Authgear implements AuthgearHttpClientDelegate {
280280
this.preAuthenticatedURLEnabled = false,
281281
TokenStorage? tokenStorage,
282282
UIImplementation? uiImplementation,
283+
Client? httpClient,
283284
}) : _tokenStorage = tokenStorage ?? PersistentTokenStorage(),
284285
_uiImplementation = uiImplementation ?? DeviceBrowserUIImplementation(),
285286
_storage = PersistentContainerStorage(),
@@ -288,7 +289,7 @@ class Authgear implements AuthgearHttpClientDelegate {
288289
namespace: name,
289290
sharedStorage: _sharedStorage,
290291
);
291-
final plainHttpClient = Client();
292+
final plainHttpClient = httpClient ?? Client();
292293
final authgearHttpClient = AuthgearHttpClient(this, plainHttpClient);
293294
_apiClient = APIClient(
294295
endpoint: endpoint,

0 commit comments

Comments
 (0)