Skip to content

Commit

Permalink
Request extends http.BaseRequest (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse committed Oct 13, 2022
1 parent 0882a7e commit e3fd623
Show file tree
Hide file tree
Showing 14 changed files with 561 additions and 245 deletions.
1 change: 1 addition & 0 deletions chopper/lib/chopper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export 'src/annotations.dart';
export 'src/authenticator.dart';
export 'src/base.dart';
export 'src/constants.dart';
export 'src/extensions.dart';
export 'src/interceptor.dart';
export 'src/request.dart';
export 'src/response.dart';
Expand Down
7 changes: 3 additions & 4 deletions chopper/lib/src/annotations.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import 'dart:async';

import 'package:chopper/src/constants.dart';
import 'package:chopper/src/request.dart';
import 'package:chopper/src/response.dart';
import 'package:meta/meta.dart';

import 'constants.dart';
import 'request.dart';
import 'response.dart';

/// Defines a Chopper API.
///
/// Must be used on an abstract class that extends the [ChopperService] class.
Expand Down
15 changes: 7 additions & 8 deletions chopper/lib/src/base.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import 'dart:async';

import 'package:chopper/src/annotations.dart';
import 'package:chopper/src/authenticator.dart';
import 'package:chopper/src/constants.dart';
import 'package:chopper/src/interceptor.dart';
import 'package:chopper/src/request.dart';
import 'package:chopper/src/response.dart';
import 'package:chopper/src/utils.dart';
import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';

import 'annotations.dart';
import 'authenticator.dart';
import 'constants.dart';
import 'interceptor.dart';
import 'request.dart';
import 'response.dart';
import 'utils.dart';

Type _typeOf<T>() => T;

@visibleForTesting
Expand Down
2 changes: 1 addition & 1 deletion chopper/lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const String formEncodedHeaders = 'application/x-www-form-urlencoded';
// Represent the header for a json api response https://jsonapi.org/#mime-types
const String jsonApiHeaders = 'application/vnd.api+json';

class HttpMethod {
abstract class HttpMethod {
static const String Get = 'GET';
static const String Post = 'POST';
static const String Put = 'PUT';
Expand Down
27 changes: 27 additions & 0 deletions chopper/lib/src/extensions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
extension StripStringExtension on String {
/// The string without any leading whitespace and optional [character]
String leftStrip([String? character]) {
final String trimmed = trimLeft();

if (character != null && trimmed.startsWith(character)) {
return trimmed.substring(1);
}

return trimmed;
}

/// The string without any trailing whitespace and optional [character]
String rightStrip([String? character]) {
final String trimmed = trimRight();

if (character != null && trimmed.endsWith(character)) {
return trimmed.substring(0, trimmed.length - 1);
}

return trimmed;
}

/// The string without any leading and trailing whitespace and optional [character]
String strip([String? character]) =>
character != null ? leftStrip(character).rightStrip(character) : trim();
}
13 changes: 6 additions & 7 deletions chopper/lib/src/interceptor.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import 'dart:async';
import 'dart:convert';

import 'package:chopper/src/constants.dart';
import 'package:chopper/src/request.dart';
import 'package:chopper/src/response.dart';
import 'package:chopper/src/utils.dart';
import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';

import 'constants.dart';
import 'request.dart';
import 'response.dart';
import 'utils.dart';

/// An interface for implementing response interceptors.
///
/// [ResponseInterceptor]s are called after [Converter.convertResponse].
Expand Down Expand Up @@ -172,7 +171,7 @@ class HttpLoggingInterceptor
@override
FutureOr<Request> onRequest(Request request) async {
final http.BaseRequest base = await request.toBaseRequest();
chopperLogger.info('--> ${base.method} ${base.url}');
chopperLogger.info('--> ${base.method} ${base.url.toString()}');
base.headers.forEach((k, v) => chopperLogger.info('$k: $v'));

String bytes = '';
Expand All @@ -192,7 +191,7 @@ class HttpLoggingInterceptor
@override
FutureOr<Response> onResponse(Response response) {
final http.BaseRequest? base = response.base.request;
chopperLogger.info('<-- ${response.statusCode} ${base!.url}');
chopperLogger.info('<-- ${response.statusCode} ${base!.url.toString()}');

response.base.headers.forEach((k, v) => chopperLogger.info('$k: $v'));

Expand Down
Loading

0 comments on commit e3fd623

Please sign in to comment.