From f16c98f13df1c675f5f367c84867be82467906c1 Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Wed, 6 Dec 2023 14:48:43 -0300 Subject: [PATCH 1/8] feat: integrating with the API --- lib/main_production.dart | 3 ++- .../lib/src/resources/questions_resource.dart | 16 ++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/main_production.dart b/lib/main_production.dart index 7016e31..ca78926 100644 --- a/lib/main_production.dart +++ b/lib/main_production.dart @@ -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 = diff --git a/packages/api_client/lib/src/resources/questions_resource.dart b/packages/api_client/lib/src/resources/questions_resource.dart index ec177f6..6cac1c1 100644 --- a/packages/api_client/lib/src/resources/questions_resource.dart +++ b/packages/api_client/lib/src/resources/questions_resource.dart @@ -22,17 +22,17 @@ class QuestionsResource { Future getVertexResponse(String query) async { String body; if (_realApiEnabled) { - final response = await _apiClient.get( - // TODO(oscar): update with real API once is enabled - // and add possible failures. - 'google.es', - queryParameters: { - 'query': query, - }, + final response = await _apiClient.post( + '', + body: jsonEncode( + { + 'search_term': query, + }, + ), ); if (response.statusCode != 200) { throw ApiClientError( - 'GET getVertexResponse with query=$query ' + 'POST getVertexResponse with query=$query ' 'returned status ${response.statusCode} ' 'with the following response: "${response.body}"', StackTrace.current, From 08ba0ac0cfa2639b1fc0f4d626da063f37a5e299 Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Wed, 6 Dec 2023 14:57:06 -0300 Subject: [PATCH 2/8] fix test --- .../test/src/resources/questions_resource_test.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/api_client/test/src/resources/questions_resource_test.dart b/packages/api_client/test/src/resources/questions_resource_test.dart index 15b13db..168f310 100644 --- a/packages/api_client/test/src/resources/questions_resource_test.dart +++ b/packages/api_client/test/src/resources/questions_resource_test.dart @@ -23,9 +23,9 @@ void main() { QuestionsResource(apiClient: apiClient, realApiEnabled: true); questionsResourceApiNotEnabled = QuestionsResource(apiClient: apiClient); when( - () => apiClient.get( + () => apiClient.post( any(), - queryParameters: any(named: 'queryParameters'), + body: any(named: 'body'), ), ).thenAnswer((_) async => response); }); From fc9887b460d94baccdb0d08592caf9e11d39f810 Mon Sep 17 00:00:00 2001 From: Oscar Date: Thu, 7 Dec 2023 16:02:59 +0100 Subject: [PATCH 3/8] chore: gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9190715..bd8e325 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ From 47a9f606c86c8c0496d2d579a4707ac2fd3fde1c Mon Sep 17 00:00:00 2001 From: Oscar Date: Thu, 7 Dec 2023 16:04:47 +0100 Subject: [PATCH 4/8] chore: delete launch --- .vscode/launch.json | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index f0752cd..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "Launch development", - "request": "launch", - "type": "dart", - "program": "lib/main_development.dart", - "args": [ - "--flavor", - "development", - "--target", - "lib/main_development.dart" - ] - }, - { - "name": "Launch production", - "request": "launch", - "type": "dart", - "program": "lib/main_production.dart", - "args": [ - "--flavor", - "production", - "--target", - "lib/main_production.dart" - ] - } - ] -} \ No newline at end of file From aa6276043ebee940d3d661edec15371db68bd117 Mon Sep 17 00:00:00 2001 From: Oscar Date: Thu, 7 Dec 2023 16:05:54 +0100 Subject: [PATCH 5/8] chore: refactor --- packages/api_client/lib/src/api_client.dart | 10 +++------- .../lib/src/resources/questions_resource.dart | 1 - 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/api_client/lib/src/api_client.dart b/packages/api_client/lib/src/api_client.dart index ef67032..d4b8a8b 100644 --- a/packages/api_client/lib/src/api_client.dart +++ b/packages/api_client/lib/src/api_client.dart @@ -86,17 +86,13 @@ class ApiClient { realApiEnabled: _realApiEnabled, ); - /// Sends a POST request to the specified [path] with the given [body]. - Future post( - String path, { + /// Sends a POST request with the given [body]. + Future post({ Object? body, Map? queryParameters, }) async { final response = await _post( - _base.replace( - path: path, - queryParameters: queryParameters, - ), + _base, body: body, headers: _headers..addContentTypeJson(), ); diff --git a/packages/api_client/lib/src/resources/questions_resource.dart b/packages/api_client/lib/src/resources/questions_resource.dart index 6cac1c1..9da9f19 100644 --- a/packages/api_client/lib/src/resources/questions_resource.dart +++ b/packages/api_client/lib/src/resources/questions_resource.dart @@ -23,7 +23,6 @@ class QuestionsResource { String body; if (_realApiEnabled) { final response = await _apiClient.post( - '', body: jsonEncode( { 'search_term': query, From f1cdf8dc2e68aa63e4f48dc4e950fccdbb57c87b Mon Sep 17 00:00:00 2001 From: Oscar Date: Thu, 7 Dec 2023 16:15:33 +0100 Subject: [PATCH 6/8] chore: delete --- packages/api_client/test/src/api_client_test.dart | 3 +-- .../api_client/test/src/resources/questions_resource_test.dart | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/api_client/test/src/api_client_test.dart b/packages/api_client/test/src/api_client_test.dart index ac71c37..1b2fa49 100644 --- a/packages/api_client/test/src/api_client_test.dart +++ b/packages/api_client/test/src/api_client_test.dart @@ -130,7 +130,7 @@ 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)); @@ -138,7 +138,6 @@ void main() { test('sends the request correctly', () async { await apiClient.post( - '/path/to/endpoint', queryParameters: {'param1': 'value1', 'param2': 'value2'}, body: 'BODY_CONTENT', ); diff --git a/packages/api_client/test/src/resources/questions_resource_test.dart b/packages/api_client/test/src/resources/questions_resource_test.dart index 168f310..1c3fa96 100644 --- a/packages/api_client/test/src/resources/questions_resource_test.dart +++ b/packages/api_client/test/src/resources/questions_resource_test.dart @@ -24,7 +24,6 @@ void main() { questionsResourceApiNotEnabled = QuestionsResource(apiClient: apiClient); when( () => apiClient.post( - any(), body: any(named: 'body'), ), ).thenAnswer((_) async => response); From c696b444d7396550a62a82040d4f108ccc8bbfe8 Mon Sep 17 00:00:00 2001 From: Oscar Date: Thu, 7 Dec 2023 16:35:42 +0100 Subject: [PATCH 7/8] chore: try catch --- .../lib/src/resources/questions_resource.dart | 58 ++++++++----------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/packages/api_client/lib/src/resources/questions_resource.dart b/packages/api_client/lib/src/resources/questions_resource.dart index 9da9f19..f94f227 100644 --- a/packages/api_client/lib/src/resources/questions_resource.dart +++ b/packages/api_client/lib/src/resources/questions_resource.dart @@ -21,46 +21,36 @@ class QuestionsResource { /// Future getVertexResponse(String query) async { String body; - if (_realApiEnabled) { - final response = await _apiClient.post( - body: jsonEncode( - { - 'search_term': query, - }, - ), - ); - if (response.statusCode != 200) { - throw ApiClientError( - 'POST getVertexResponse with query=$query ' - 'returned status ${response.statusCode} ' - 'with the following response: "${response.body}"', - StackTrace.current, + try { + if (_realApiEnabled) { + final response = await _apiClient.post( + body: jsonEncode( + { + 'search_term': query, + }, + ), ); - } - body = response.body; - } else { - await Future.delayed(const Duration(seconds: 2)); + body = response.body; + } else { + await Future.delayed(const Duration(seconds: 2)); - body = switch (query) { - 'What is flutter?' => FakeResponses.whatIsFlutterResponse, - 'What platforms does flutter support today?' => - FakeResponses.platformsResponse, - 'What language do you use to write flutter apps?' => - FakeResponses.languageResponse, - 'How does hot reload work in flutter?' => - FakeResponses.hotReloadResponse, - _ => FakeResponses.invalidResponse, - }; - } + body = switch (query) { + 'What is flutter?' => FakeResponses.whatIsFlutterResponse, + 'What platforms does flutter support today?' => + FakeResponses.platformsResponse, + 'What language do you use to write flutter apps?' => + FakeResponses.languageResponse, + 'How does hot reload work in flutter?' => + FakeResponses.hotReloadResponse, + _ => FakeResponses.invalidResponse, + }; + } - try { final json = jsonDecode(body) as Map; return VertexResponse.fromJson(json); } catch (e) { - throw ApiClientError( - 'GET getVertexResponse with query=$query ' - 'returned invalid response "$body"', - StackTrace.current, + return VertexResponse.fromJson( + jsonDecode(FakeResponses.invalidResponse) as Map, ); } } From ebf9d89f04f4c99db9798bb298a1db032aaff163 Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Thu, 7 Dec 2023 16:22:51 -0300 Subject: [PATCH 8/8] fix --- packages/api_client/lib/src/api_client.dart | 1 - .../lib/src/resources/questions_resource.dart | 52 ++++++++++--------- .../api_client/test/src/api_client_test.dart | 3 +- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/packages/api_client/lib/src/api_client.dart b/packages/api_client/lib/src/api_client.dart index df65382..8de72dd 100644 --- a/packages/api_client/lib/src/api_client.dart +++ b/packages/api_client/lib/src/api_client.dart @@ -60,7 +60,6 @@ class ApiClient { /// Sends a POST request with the given [body]. Future post({ Object? body, - Map? queryParameters, }) async { final response = await _post( _base, diff --git a/packages/api_client/lib/src/resources/questions_resource.dart b/packages/api_client/lib/src/resources/questions_resource.dart index f94f227..1a571e3 100644 --- a/packages/api_client/lib/src/resources/questions_resource.dart +++ b/packages/api_client/lib/src/resources/questions_resource.dart @@ -21,36 +21,38 @@ class QuestionsResource { /// Future getVertexResponse(String query) async { String body; - try { - if (_realApiEnabled) { - final response = await _apiClient.post( - body: jsonEncode( - { - 'search_term': query, - }, - ), - ); - body = response.body; - } else { - await Future.delayed(const Duration(seconds: 2)); + if (_realApiEnabled) { + final response = await _apiClient.post( + body: jsonEncode( + { + 'search_term': query, + }, + ), + ); + body = response.body; + } else { + await Future.delayed(const Duration(seconds: 2)); - body = switch (query) { - 'What is flutter?' => FakeResponses.whatIsFlutterResponse, - 'What platforms does flutter support today?' => - FakeResponses.platformsResponse, - 'What language do you use to write flutter apps?' => - FakeResponses.languageResponse, - 'How does hot reload work in flutter?' => - FakeResponses.hotReloadResponse, - _ => FakeResponses.invalidResponse, - }; - } + body = switch (query) { + 'What is flutter?' => FakeResponses.whatIsFlutterResponse, + 'What platforms does flutter support today?' => + FakeResponses.platformsResponse, + 'What language do you use to write flutter apps?' => + FakeResponses.languageResponse, + 'How does hot reload work in flutter?' => + FakeResponses.hotReloadResponse, + _ => FakeResponses.invalidResponse, + }; + } + try { final json = jsonDecode(body) as Map; return VertexResponse.fromJson(json); } catch (e) { - return VertexResponse.fromJson( - jsonDecode(FakeResponses.invalidResponse) as Map, + throw ApiClientError( + 'GET getVertexResponse with query=$query ' + 'returned invalid response "$body"', + StackTrace.current, ); } } diff --git a/packages/api_client/test/src/api_client_test.dart b/packages/api_client/test/src/api_client_test.dart index 4df156f..ed38337 100644 --- a/packages/api_client/test/src/api_client_test.dart +++ b/packages/api_client/test/src/api_client_test.dart @@ -100,13 +100,12 @@ void main() { test('sends the request correctly', () async { await apiClient.post( - queryParameters: {'param1': 'value1', 'param2': 'value2'}, body: 'BODY_CONTENT', ); verify( () => httpClient.post( - Uri.parse('$baseUrl/path/to/endpoint?param1=value1¶m2=value2'), + Uri.parse(baseUrl), body: 'BODY_CONTENT', headers: {HttpHeaders.contentTypeHeader: ContentType.json.value}, ),