Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add api client package #5

Merged
merged 4 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion packages/api_client/lib/api_client.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/// A Very Good Project created by Very Good CLI.
/// Client to access the api.
library api_client;

export 'src/api_client.dart';
export 'src/errors/api_client_error.dart';
export 'src/extensions/extensions.dart';
export 'src/http_calls/http_calls.dart';
102 changes: 2 additions & 100 deletions packages/api_client/lib/src/api_client.dart
Original file line number Diff line number Diff line change
@@ -1,60 +1,11 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:encrypt/encrypt.dart';
import 'package:api_client/api_client.dart';
import 'package:http/http.dart' as http;

/// {@template api_client_error}
/// Error throw when accessing api failed.
///
/// Check [cause] and [stackTrace] for specific details.
/// {@endtemplate}
class ApiClientError implements Exception {
/// {@macro api_client_error}
ApiClientError(this.cause, this.stackTrace);

/// Error cause.
final dynamic cause;

/// The stack trace of the error.
final StackTrace stackTrace;

@override
String toString() {
return cause.toString();
}
}

/// Definition of a post call used by this client.
typedef PostCall = Future<http.Response> Function(
Uri, {
Object? body,
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}
/// A Very Good Project created by Very Good CLI.
/// Client to access the api.
/// {@endtemplate}
class ApiClient {
/// {@macro api_client}
Expand Down Expand Up @@ -210,52 +161,3 @@ class ApiClient {
});
}
}

extension on http.Response {
http.Response get decrypted {
if (body.isEmpty) return this;

final key = Key.fromUtf8(_encryptionKey);
final iv = IV.fromUtf8(_encryptionIV);

final encrypter = Encrypter(AES(key));

final decrypted = encrypter.decrypt64(body, iv: iv);

return http.Response(
jsonDecode(decrypted).toString(),
statusCode,
headers: headers,
isRedirect: isRedirect,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase,
request: request,
);
}

String get _encryptionKey {
const value = String.fromEnvironment(
'ENCRYPTION_KEY',
// Default value is set at 32 characters to match required length of
// AES key. The default value can then be used for testing purposes.
defaultValue: 'encryption_key_not_set_123456789',
);
return value;
}

String get _encryptionIV {
const value = String.fromEnvironment(
'ENCRYPTION_IV',
// Default value is set at 116 characters to match required length of
// IV key. The default value can then be used for testing purposes.
defaultValue: 'iv_not_set_12345',
);
return value;
}
}

extension on Map<String, String> {
void addContentTypeJson() {
addAll({HttpHeaders.contentTypeHeader: ContentType.json.value});
}
}
20 changes: 20 additions & 0 deletions packages/api_client/lib/src/errors/api_client_error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// {@template api_client_error}
/// Error throw when accessing api failed.
///
/// Check [cause] and [stackTrace] for specific details.
/// {@endtemplate}
class ApiClientError implements Exception {
/// {@macro api_client_error}
ApiClientError(this.cause, this.stackTrace);

/// Error cause.
final dynamic cause;

/// The stack trace of the error.
final StackTrace stackTrace;

@override
String toString() {
return cause.toString();
B0berman marked this conversation as resolved.
Show resolved Hide resolved
}
}
2 changes: 2 additions & 0 deletions packages/api_client/lib/src/extensions/extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'json_content_type.dart';
export 'response_decryption.dart';
11 changes: 11 additions & 0 deletions packages/api_client/lib/src/extensions/json_content_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'dart:io';

/// {@template json_content_type}
/// Extension on [Map<String, String>] to add content-type value
/// {@endtemplate}
extension JsonContentType on Map<String, String> {
/// Adds content-type to map
void addContentTypeJson() {
addAll({HttpHeaders.contentTypeHeader: ContentType.json.value});
}
}
51 changes: 51 additions & 0 deletions packages/api_client/lib/src/extensions/response_decryption.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'dart:convert';

import 'package:encrypt/encrypt.dart';
import 'package:http/http.dart' as http;

/// {@template response_encryption}
/// Extension on [http.Response] to decrypt body
/// {@endtemplate}
extension ResponseDecryption on http.Response {
/// Get [http.Response] with decrypted body
http.Response get decrypted {
if (body.isEmpty) return this;

final key = Key.fromUtf8(_encryptionKey);
final iv = IV.fromUtf8(_encryptionIV);

final encrypter = Encrypter(AES(key));

final decrypted = encrypter.decrypt64(body, iv: iv);

return http.Response(
jsonDecode(decrypted).toString(),
statusCode,
headers: headers,
isRedirect: isRedirect,
persistentConnection: persistentConnection,
reasonPhrase: reasonPhrase,
request: request,
);
}

String get _encryptionKey {
const value = String.fromEnvironment(
'ENCRYPTION_KEY',
// Default value is set at 32 characters to match required length of
// AES key. The default value can then be used for testing purposes.
defaultValue: 'encryption_key_not_set_123456789',
);
return value;
}

String get _encryptionIV {
const value = String.fromEnvironment(
'ENCRYPTION_IV',
// Default value is set at 116 characters to match required length of
// IV key. The default value can then be used for testing purposes.
defaultValue: 'iv_not_set_12345',
);
return value;
}
}
28 changes: 28 additions & 0 deletions packages/api_client/lib/src/http_calls/http_calls.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:http/http.dart' as http;

/// Definition of a post call used by this client.
typedef PostCall = Future<http.Response> Function(
Uri, {
Object? body,
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,
});
9 changes: 0 additions & 9 deletions packages/api_client/test/src/api_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -535,14 +535,5 @@ void main() {
).called(1);
});
});

group('ApiClientError', () {
test('toString returns the cause', () {
expect(
ApiClientError('Ops', StackTrace.empty).toString(),
equals('Ops'),
);
});
});
});
}
13 changes: 13 additions & 0 deletions packages/api_client/test/src/errors/api_client_error_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:api_client/src/errors/api_client_error.dart';
import 'package:test/test.dart';

void main() {
group('ApiClientError', () {
test('toString returns the cause', () {
expect(
ApiClientError('Ops', StackTrace.empty).toString(),
equals('Ops'),
);
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'dart:convert';

import 'package:api_client/src/extensions/response_decryption.dart';
import 'package:encrypt/encrypt.dart';
import 'package:http/http.dart' as http;
import 'package:test/test.dart';

void main() {
group('ResponseDecryption', () {
test('decrypts correctly', () {
final key = Key.fromUtf8('encryption_key_not_set_123456789');
final iv = IV.fromUtf8('iv_not_set_12345');
final encrypter = Encrypter(AES(key));
final testJson = {'data': 'test'};
final encrypted = encrypter.encrypt(jsonEncode(testJson), iv: iv).base64;
final encryptedResponse = http.Response(encrypted, 200);
final expectedResponse = http.Response(testJson.toString(), 200);

expect(encryptedResponse.decrypted.body, equals(expectedResponse.body));
});
});
}
Loading