Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
chore: revert
Browse files Browse the repository at this point in the history
  • Loading branch information
omartinma committed Nov 21, 2023
1 parent 2c21e27 commit 96dad91
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/main_development.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ void main() {
bootstrap(
() {
final apiClient = ApiClient(
baseUrl: 'http://localhost:8080',
baseUrl: 'http://development',
idTokenStream: const Stream.empty(),
refreshIdToken: () async => Future.value(),
);

return App(
Expand Down
2 changes: 2 additions & 0 deletions lib/main_production.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ void main() {
() {
final apiClient = ApiClient(
baseUrl: 'http://production',
idTokenStream: const Stream.empty(),
refreshIdToken: () async => Future.value(),
);

return App(
Expand Down
4 changes: 3 additions & 1 deletion packages/api_client/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import 'package:api_client/api_client.dart';

Future<void> main() async {
final apiClient = ApiClient(
baseUrl: '',
baseUrl: 'http://development',
idTokenStream: const Stream.empty(),
refreshIdToken: () async => Future.value(),
);
final questionsResource = apiClient.questionsResource;
final answer = await questionsResource.getVertexResponse('random');
Expand Down
33 changes: 32 additions & 1 deletion packages/api_client/lib/src/api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class ApiClient {
/// {@macro api_client}
ApiClient({
required String baseUrl,
required Stream<String?> idTokenStream,
required Future<String?> Function() refreshIdToken,
PostCall postCall = http.post,
PutCall putCall = http.put,
PatchCall patchCall = http.patch,
Expand All @@ -67,13 +69,26 @@ class ApiClient {
_post = postCall,
_put = putCall,
_patch = patchCall,
_get = getCall;
_get = getCall,
_refreshIdToken = refreshIdToken {
_idTokenSubscription = idTokenStream.listen((idToken) {
_idToken = idToken;
});
}

final Uri _base;
final PostCall _post;
final PostCall _put;
final PatchCall _patch;
final GetCall _get;
final Future<String?> Function() _refreshIdToken;

late final StreamSubscription<String?> _idTokenSubscription;
String? _idToken;

Map<String, String> get _headers => {
if (_idToken != null) 'Authorization': 'Bearer $_idToken',
};

/// Questions resource.
late final QuestionsResource questionsResource = const QuestionsResource();
Expand All @@ -84,11 +99,17 @@ class ApiClient {
final response = await sendRequest();

if (response.statusCode == HttpStatus.unauthorized) {
_idToken = await _refreshIdToken();
return sendRequest();
}
return response;
}

/// Dispose of resources used by this client.
Future<void> dispose() async {
await _idTokenSubscription.cancel();
}

/// Sends a POST request to the specified [path] with the given [body].
Future<http.Response> post(
String path, {
Expand All @@ -102,6 +123,7 @@ class ApiClient {
queryParameters: queryParameters,
),
body: body,
headers: _headers..addContentTypeJson(),
);

return response;
Expand All @@ -121,6 +143,7 @@ class ApiClient {
queryParameters: queryParameters,
),
body: body,
headers: _headers..addContentTypeJson(),
);

return response;
Expand All @@ -136,6 +159,7 @@ class ApiClient {
final response = await _put(
_base.replace(path: path),
body: body,
headers: _headers..addContentTypeJson(),
);

return response;
Expand All @@ -153,9 +177,16 @@ class ApiClient {
path: path,
queryParameters: queryParameters,
),
headers: _headers,
);

return response;
});
}
}

extension on Map<String, String> {
void addContentTypeJson() {
addAll({HttpHeaders.contentTypeHeader: ContentType.json.value});
}
}
Loading

0 comments on commit 96dad91

Please sign in to comment.