This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/app_ui_fonts
- Loading branch information
Showing
15 changed files
with
216 additions
and
332 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
51 changes: 43 additions & 8 deletions
51
packages/api_client/lib/src/resources/questions_resource.dart
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,23 +1,58 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:api_client/api_client.dart'; | ||
import 'package:cross_file/cross_file.dart'; | ||
|
||
/// {@template questions_resource} | ||
/// An api resource to get response to question from the VERTEX api | ||
/// {@endtemplate} | ||
class QuestionsResource { | ||
/// {@macro questions_resource} | ||
const QuestionsResource(); | ||
const QuestionsResource({ | ||
required ApiClient apiClient, | ||
bool realApiEnabled = false, | ||
}) : _apiClient = apiClient, | ||
_realApiEnabled = realApiEnabled; | ||
|
||
/// Get /game/prompt/terms | ||
final ApiClient _apiClient; | ||
final bool _realApiEnabled; | ||
|
||
/// Returns [VertexResponse] based on a query. | ||
/// | ||
/// Returns a [List<String>]. | ||
Future<VertexResponse> getVertexResponse(String query) async { | ||
const path = 'lib/src/resources/fake_response.json'; | ||
final fakeResponse = await File(path).readAsString(); | ||
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, | ||
}, | ||
); | ||
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 { | ||
const path = 'lib/src/resources/fake_response.json'; | ||
body = await XFile(path).readAsString(); | ||
} | ||
|
||
final json = jsonDecode(fakeResponse) as Map<String, dynamic>; | ||
return VertexResponse.fromJson(json); | ||
try { | ||
final json = jsonDecode(body) as Map<String, dynamic>; | ||
return VertexResponse.fromJson(json); | ||
} catch (e) { | ||
throw ApiClientError( | ||
'GET getVertexResponse with query=$query ' | ||
'returned invalid response "$body"', | ||
StackTrace.current, | ||
); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.