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

feat: integrating with the API #70

Merged
merged 9 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ app.*.map.json
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
!/dev/ci/**/Gemfile.lock
!.vscode/extensions.json
!.vscode/launch.json
!.idea/codeStyles/
!.idea/dictionaries/
!.idea/runConfigurations/
Expand Down
32 changes: 0 additions & 32 deletions .vscode/launch.json

This file was deleted.

3 changes: 2 additions & 1 deletion lib/main_production.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ void main() async {
await bootstrap(
() async {
final apiClient = ApiClient(
baseUrl: 'http://production',
realApiEnabled: true,
baseUrl: const String.fromEnvironment('AI_API_URL'),
);

final questionsRepository =
Expand Down
11 changes: 3 additions & 8 deletions packages/api_client/lib/src/api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,12 @@ class ApiClient {
realApiEnabled: _realApiEnabled,
);

/// Sends a POST request to the specified [path] with the given [body].
Future<http.Response> post(
String path, {
/// Sends a POST request with the given [body].
Future<http.Response> post({
Object? body,
Map<String, String>? queryParameters,
}) async {
final response = await _post(
_base.replace(
path: path,
queryParameters: queryParameters,
),
_base,
body: body,
headers: _headers..addContentTypeJson(),
);
Expand Down
19 changes: 5 additions & 14 deletions packages/api_client/lib/src/resources/questions_resource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,12 @@ class QuestionsResource {
String body;
if (_realApiEnabled) {
final response = await _apiClient.post(
// TODO(oscar): update with real API once is enabled
// and add possible failures.
'google.es',
queryParameters: {
'query': query,
},
body: jsonEncode(
{
'search_term': query,
},
),
);
if (response.statusCode != 200) {
throw ApiClientError(
'GET getVertexResponse with query=$query '
'returned status ${response.statusCode} '
'with the following response: "${response.body}"',
StackTrace.current,
);
}
body = response.body;
} else {
await Future<void>.delayed(const Duration(seconds: 2));
Expand Down
6 changes: 2 additions & 4 deletions packages/api_client/test/src/api_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,20 @@ void main() {

group('post', () {
test('returns the response', () async {
final response = await apiClient.post('/');
final response = await apiClient.post();

expect(response.statusCode, equals(expectedResponse.statusCode));
expect(response.body, equals(expectedResponse.body));
});

test('sends the request correctly', () async {
await apiClient.post(
'/path/to/endpoint',
queryParameters: {'param1': 'value1', 'param2': 'value2'},
body: 'BODY_CONTENT',
);

verify(
() => httpClient.post(
Uri.parse('$baseUrl/path/to/endpoint?param1=value1&param2=value2'),
Uri.parse(baseUrl),
body: 'BODY_CONTENT',
headers: {HttpHeaders.contentTypeHeader: ContentType.json.value},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ void main() {
questionsResourceApiNotEnabled = QuestionsResource(apiClient: apiClient);
when(
() => apiClient.post(
any(),
queryParameters: any(named: 'queryParameters'),
body: any(named: 'body'),
),
).thenAnswer((_) async => response);
});
Expand Down