Skip to content

Commit

Permalink
♻️ refactor chopper (#460)
Browse files Browse the repository at this point in the history
* refactor getService
* refactor CurlInterceptor
* refactor utils._Instance
  • Loading branch information
techouse authored Aug 2, 2023
1 parent 7cd824c commit aa63283
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
9 changes: 3 additions & 6 deletions chopper/lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import 'package:chopper/src/utils.dart';
import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';

Type _typeOf<T>() => T;

@visibleForTesting
const List<Type> allowedInterceptorsType = [
RequestInterceptor,
Expand Down Expand Up @@ -168,15 +166,14 @@ base class ChopperClient {
/// final todoService = chopper.getService<TodosListService>();
/// ```
ServiceType getService<ServiceType extends ChopperService>() {
final Type serviceType = _typeOf<ServiceType>();
if (serviceType == dynamic || serviceType == ChopperService) {
if (ServiceType == dynamic || ServiceType == ChopperService) {
throw Exception(
'Service type should be provided, `dynamic` is not allowed.',
);
}
final ChopperService? service = _services[serviceType];
final ChopperService? service = _services[ServiceType];
if (service == null) {
throw Exception('Service of type \'$serviceType\' not found.');
throw Exception("Service of type '$ServiceType' not found.");
}

return service as ServiceType;
Expand Down
31 changes: 13 additions & 18 deletions chopper/lib/src/interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,32 +137,27 @@ class CurlInterceptor implements RequestInterceptor {
@override
Future<Request> onRequest(Request request) async {
final http.BaseRequest baseRequest = await request.toBaseRequest();
final String method = baseRequest.method;
final String url = baseRequest.url.toString();
final Map<String, String> headers = baseRequest.headers;
String curl = 'curl -v -X $method';
headers.forEach((k, v) {
curl += ' -H \'$k: $v\'';
});
final List<String> curlParts = ['curl -v -X ${baseRequest.method}'];
for (final MapEntry<String, String> header in baseRequest.headers.entries) {
curlParts.add("-H '${header.key}: ${header.value}'");
}
// this is fairly naive, but it should cover most cases
if (baseRequest is http.Request) {
final body = baseRequest.body;
final String body = baseRequest.body;
if (body.isNotEmpty) {
curl += ' -d \'$body\'';
curlParts.add("-d '$body'");
}
}
if (baseRequest is http.MultipartRequest) {
final fields = baseRequest.fields;
final files = baseRequest.files;
fields.forEach((k, v) {
curl += ' -f \'$k: $v\'';
});
for (var file in files) {
curl += ' -f \'${file.field}: ${file.filename ?? ''}\'';
for (final MapEntry<String, String> field in baseRequest.fields.entries) {
curlParts.add("-f '${field.key}: ${field.value}'");
}
for (final http.MultipartFile file in baseRequest.files) {
curlParts.add("-f '${file.field}: ${file.filename ?? ''}'");
}
}
curl += ' "$url"';
chopperLogger.info(curl);
curlParts.add('"${baseRequest.url}"');
chopperLogger.info(curlParts.join(' '));

return request;
}
Expand Down
4 changes: 1 addition & 3 deletions chopper/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,4 @@ final class _Pair<A, B> with EquatableMixin {

bool isTypeOf<ThisType, OfType>() => _Instance<ThisType>() is _Instance<OfType>;

class _Instance<T> {
//
}
final class _Instance<T> {}
14 changes: 13 additions & 1 deletion chopper/test/base_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ void main() {
);

group('Base', () {
test('getService', () async {
final httpClient = MockClient(
(_) async => http.Response('get response', 200),
);

final chopper = buildClient(httpClient);
final service = chopper.getService<HttpTestService>();

expect(service, isNotNull);
expect(service, isA<HttpTestService>());
});

test('get service errors', () async {
final chopper = ChopperClient(
baseUrl: baseUrl,
Expand All @@ -38,7 +50,7 @@ void main() {
} on Exception catch (e) {
expect(
e.toString(),
equals('Exception: Service of type \'HttpTestService\' not found.'),
equals("Exception: Service of type 'HttpTestService' not found."),
);
}

Expand Down

0 comments on commit aa63283

Please sign in to comment.