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

refactor: clean up api client #76

Merged
merged 2 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
77 changes: 0 additions & 77 deletions packages/api_client/lib/src/api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,6 @@ typedef PostCall = Future<http.Response> Function(
Map<String, String>? headers,
});

/// Definition of a patch call used by this client.
typedef PatchCall = Future<http.Response> Function(
Uri, {
Object? body,
Map<String, String>? headers,
});

/// Definition of a put call used by this client.
typedef PutCall = Future<http.Response> Function(
Uri, {
Object? body,
Map<String, String>? headers,
});

/// Definition of a get call used by this client.
typedef GetCall = Future<http.Response> Function(
Uri, {
Map<String, String>? headers,
});

/// {@template api_client}
/// Client to access the api.
/// {@endtemplate}
Expand All @@ -60,22 +40,13 @@ class ApiClient {
ApiClient({
required String baseUrl,
PostCall postCall = http.post,
PutCall putCall = http.put,
PatchCall patchCall = http.patch,
GetCall getCall = http.get,
bool realApiEnabled = false,
}) : _base = Uri.parse(baseUrl),
_post = postCall,
_put = putCall,
_patch = patchCall,
_get = getCall,
_realApiEnabled = realApiEnabled;

final Uri _base;
final PostCall _post;
final PostCall _put;
final PatchCall _patch;
final GetCall _get;
final bool _realApiEnabled;

Map<String, String> get _headers => {};
Expand Down Expand Up @@ -103,54 +74,6 @@ class ApiClient {

return response;
}

/// Sends a PATCH request to the specified [path] with the given [body].
Future<http.Response> patch(
String path, {
Object? body,
Map<String, String>? queryParameters,
}) async {
final response = await _patch(
_base.replace(
path: path,
queryParameters: queryParameters,
),
body: body,
headers: _headers..addContentTypeJson(),
);

return response;
}

/// Sends a PUT request to the specified [path] with the given [body].
Future<http.Response> put(
String path, {
Object? body,
}) async {
final response = await _put(
_base.replace(path: path),
body: body,
headers: _headers..addContentTypeJson(),
);

return response;
}

/// Sends a GET request to the specified [path].
Future<http.Response> get(
String path, {
Map<String, String>? queryParameters,
}) async {
final response = await _get(
_base.replace(
path: path,
queryParameters: queryParameters,
),
headers: _headers,
);

return response;
}
}

extension on Map<String, String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class QuestionsResource {
Future<VertexResponse> getVertexResponse(String query) async {
String body;
if (_realApiEnabled) {
final response = await _apiClient.get(
final response = await _apiClient.post(
// TODO(oscar): update with real API once is enabled
// and add possible failures.
'google.es',
Expand Down
87 changes: 0 additions & 87 deletions packages/api_client/test/src/api_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ void main() {

apiClient = ApiClient(
baseUrl: baseUrl,
getCall: httpClient.get,
postCall: httpClient.post,
patchCall: httpClient.patch,
putCall: httpClient.put,
);
});

Expand All @@ -93,41 +90,6 @@ void main() {
expect(apiClient.questionsResource, isA<QuestionsResource>());
});

group('get', () {
setUp(() {
when(
() => httpClient.get(
any(),
headers: any(named: 'headers'),
),
).thenAnswer((_) async => expectedResponse);
});

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

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

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

verify(
() => httpClient.get(
Uri.parse('$baseUrl/path/to/endpoint?param1=value1&param2=value2'),
headers: {},
),
).called(1);
});
});

group('post', () {
test('returns the response', () async {
final response = await apiClient.post('/');
Expand All @@ -153,55 +115,6 @@ void main() {
});
});

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

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

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

verify(
() => httpClient.patch(
Uri.parse('$baseUrl/path/to/endpoint?param1=value1&param2=value2'),
body: 'BODY_CONTENT',
headers: {HttpHeaders.contentTypeHeader: ContentType.json.value},
),
).called(1);
});
});

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

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

test('sends the request correctly', () async {
await apiClient.put(
'/path/to/endpoint',
body: 'BODY_CONTENT',
);

verify(
() => httpClient.put(
Uri.parse('$baseUrl/path/to/endpoint'),
body: 'BODY_CONTENT',
headers: {HttpHeaders.contentTypeHeader: ContentType.json.value},
),
).called(1);
});
});

group('ApiClientError', () {
test('toString returns the cause', () {
expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void main() {
QuestionsResource(apiClient: apiClient, realApiEnabled: true);
questionsResourceApiNotEnabled = QuestionsResource(apiClient: apiClient);
when(
() => apiClient.get(
() => apiClient.post(
any(),
queryParameters: any(named: 'queryParameters'),
),
Expand Down