diff --git a/packages/api_client/lib/src/resources/questions_resource.dart b/packages/api_client/lib/src/resources/questions_resource.dart index 3166f4b..55aee64 100644 --- a/packages/api_client/lib/src/resources/questions_resource.dart +++ b/packages/api_client/lib/src/resources/questions_resource.dart @@ -1,7 +1,7 @@ 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 @@ -21,22 +21,39 @@ class QuestionsResource { /// /// Returns a [List]. Future getVertexResponse(String query) async { + String body; if (_realApiEnabled) { final response = await _apiClient.get( - // TODO(oscar): update with real API once is enabled. + // TODO(oscar): update with real API once is enabled + // and add possible failures. 'google.es', queryParameters: { 'query': query, }, ); - final json = jsonDecode(response.body); - return VertexResponse.fromJson(json as Map); + 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'; - final fakeResponse = await File(path).readAsString(); + body = await XFile(path).readAsString(); + } - final json = jsonDecode(fakeResponse) as Map; + 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, + ); } } } diff --git a/packages/api_client/pubspec.yaml b/packages/api_client/pubspec.yaml index c9d52aa..abf074c 100644 --- a/packages/api_client/pubspec.yaml +++ b/packages/api_client/pubspec.yaml @@ -7,6 +7,7 @@ environment: sdk: ">=3.1.0 <4.0.0" dependencies: + cross_file: ^0.3.3+6 equatable: ^2.0.5 http: ^1.1.0 json_annotation: ^4.8.1 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 79165ce..6ff5fa9 100644 --- a/packages/api_client/test/src/resources/questions_resource_test.dart +++ b/packages/api_client/test/src/resources/questions_resource_test.dart @@ -31,27 +31,51 @@ void main() { }); group('getVertexResponse', () { - test('returns a VertexResponse for api enabled', () async { - when(() => response.statusCode).thenReturn(200); - final vertexResponse = VertexResponse( - summary: '', - documents: const [ - VertexDocument( - metadata: VertexMetadata(url: '', title: '', description: ''), - ), - ], - ); - when(() => response.body).thenReturn( - jsonEncode(vertexResponse.toJson()), - ); - final result = await questionsResourceApiEnabled.getVertexResponse(''); - expect(result, equals(vertexResponse)); + group('realApiEnabled=true', () { + test('returns a VertexResponse if success', () async { + when(() => response.statusCode).thenReturn(200); + final vertexResponse = VertexResponse( + summary: '', + documents: const [ + VertexDocument( + metadata: VertexMetadata(url: '', title: '', description: ''), + ), + ], + ); + when(() => response.body).thenReturn( + jsonEncode(vertexResponse.toJson()), + ); + final result = + await questionsResourceApiEnabled.getVertexResponse(''); + expect(result, equals(vertexResponse)); + }); + + test('throws ApiClientError when request fails', () async { + when(() => response.statusCode).thenReturn(500); + when(() => response.body).thenReturn('Ops'); + await expectLater( + questionsResourceApiEnabled.getVertexResponse(''), + throwsA(isA()), + ); + }); + + test('throws ApiClientError when request success but wrong body', + () async { + when(() => response.statusCode).thenReturn(200); + when(() => response.body).thenReturn('Ops'); + await expectLater( + questionsResourceApiEnabled.getVertexResponse(''), + throwsA(isA()), + ); + }); }); - test('returns a VertexResponse for api disabled', () async { - final result = - await questionsResourceApiNotEnabled.getVertexResponse(''); - expect(result, isA()); + group('realApiEnabled=false', () { + test('returns a VertexResponse', () async { + final result = + await questionsResourceApiNotEnabled.getVertexResponse(''); + expect(result, isA()); + }); }); }); });