Skip to content

Commit

Permalink
🔖 release v8.0.0 (#609)
Browse files Browse the repository at this point in the history
# chopper

## 8.0.0

- Restructure interceptors ([#547](#547))
- Add per-request timeout ([#604](#604))

# chopper_generator

## 8.0.0

- Restructure interceptors ([#547](#547))
- Add per-request timeout ([#604](#604))

## 3.0.0

- Require Chopper ^8.0.0

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: Job Guldemeester <job@dutchcodingcompany.com>
  • Loading branch information
techouse and Guldem authored May 9, 2024
1 parent ae505af commit df9801a
Show file tree
Hide file tree
Showing 14 changed files with 764 additions and 343 deletions.
12 changes: 7 additions & 5 deletions chopper/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Changelog

## 8.0.0-rc.2
## 8.0.0

- Add per-request timeout ([#604](https://github.com/lejard-h/chopper/pull/604))

## 8.0.0-rc.1

- Restructure interceptors ([#547](https://github.com/lejard-h/chopper/pull/547))
- **BREAKING CHANGE**:
- Restructure interceptors ([#547](https://github.com/lejard-h/chopper/pull/547))
- `RequestInterceptor` and Function `RequestInterceptor`s are removed
- `ResponseInterceptor` and Function `ResponseInterceptor`s are removed
- See [Migrating to 8.0.0](https://docs.google.com/document/d/e/2PACX-1vQFoUDisnSJBzzXCMaf53ffUD1Bvpu-1GZ_stzfaaCa0Xd3WKIegbd1mmavEQcMT6r6v8z02UqloKuC/pub) for more information and examples
- add `onlyErrors` option to `HttpLoggingInterceptor` ([#610](https://github.com/lejard-h/chopper/pull/610))

## 7.4.0

Expand Down
121 changes: 77 additions & 44 deletions chopper/lib/src/interceptors/http_logging_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:async';
import 'package:chopper/src/chain/chain.dart';
import 'package:chopper/src/chopper_log_record.dart';
import 'package:chopper/src/interceptors/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;
Expand Down Expand Up @@ -75,116 +76,148 @@ enum Level {
@immutable
class HttpLoggingInterceptor implements Interceptor {
/// {@macro http_logging_interceptor}
HttpLoggingInterceptor({this.level = Level.body, Logger? logger})
: _logger = logger ?? chopperLogger,
HttpLoggingInterceptor({
this.level = Level.body,
this.onlyErrors = false,
Logger? logger,
}) : _logger = logger ?? chopperLogger,
_logBody = level == Level.body,
_logHeaders = level == Level.body || level == Level.headers;

final Level level;
final bool onlyErrors;
final Logger _logger;
final bool _logBody;
final bool _logHeaders;

@override
FutureOr<Response<BodyType>> intercept<BodyType>(
Chain<BodyType> chain) async {
final request = chain.request;
if (level == Level.none) return chain.proceed(request);
final Request request = chain.request;

final Stopwatch stopWatch = Stopwatch()..start();

final Response<BodyType> response = await chain.proceed(request);

stopWatch.stop();

if (level == Level.none || (onlyErrors && response.statusCode < 400)) {
return response;
}

final http.BaseRequest baseRequest = await request.toBaseRequest();

String startRequestMessage =
'--> ${baseRequest.method} ${baseRequest.url.toString()}';
String bodyRequestMessage = '';
final StringBuffer startRequestMessage = StringBuffer(
'--> ${baseRequest.method} ${baseRequest.url.toString()}',
);
final StringBuffer bodyRequestMessage = StringBuffer();
if (baseRequest is http.Request) {
if (baseRequest.body.isNotEmpty) {
bodyRequestMessage = baseRequest.body;
bodyRequestMessage.write(baseRequest.body);

if (!_logHeaders) {
startRequestMessage += ' (${baseRequest.bodyBytes.length}-byte body)';
startRequestMessage.write(
' (${baseRequest.bodyBytes.length}-byte body)',
);
}
}
}

// Always start on a new line
_logger.info(ChopperLogRecord('', request: request));
_logger.info(ChopperLogRecord(startRequestMessage, request: request));
_logger.info(
ChopperLogRecord(startRequestMessage.toString(), request: request),
);

if (_logHeaders) {
baseRequest.headers.forEach(
(k, v) => _logger.info(ChopperLogRecord('$k: $v', request: request)),
(String k, String v) => _logger.info(
ChopperLogRecord('$k: $v', request: request),
),
);

if (baseRequest.contentLength != null &&
baseRequest.headers['content-length'] == null) {
_logger.info(ChopperLogRecord(
'content-length: ${baseRequest.contentLength}',
request: request,
));
_logger.info(
ChopperLogRecord(
'content-length: ${baseRequest.contentLength}',
request: request,
),
);
}
}

if (_logBody && bodyRequestMessage.isNotEmpty) {
_logger.info(ChopperLogRecord('', request: request));
_logger.info(ChopperLogRecord(bodyRequestMessage, request: request));
_logger.info(
ChopperLogRecord(bodyRequestMessage.toString(), request: request),
);
}

if (_logHeaders || _logBody) {
_logger.info(ChopperLogRecord(
'--> END ${baseRequest.method}',
request: request,
));
_logger.info(
ChopperLogRecord('--> END ${baseRequest.method}', request: request),
);
}
final stopWatch = Stopwatch()..start();

final response = await chain.proceed(request);

stopWatch.stop();

if (level == Level.none) return response;
final baseResponse = response.base;
final http.BaseResponse baseResponse = response.base;

String bytes = '';
String reasonPhrase = response.statusCode.toString();
String bodyResponseMessage = '';
final StringBuffer bytes = StringBuffer();
final StringBuffer reasonPhrase = StringBuffer(
response.statusCode.toString(),
);
final StringBuffer bodyResponseMessage = StringBuffer();
if (baseResponse is http.Response) {
if (baseResponse.reasonPhrase != null) {
reasonPhrase +=
' ${baseResponse.reasonPhrase != reasonPhrase ? baseResponse.reasonPhrase : ''}';
if (baseResponse.reasonPhrase != reasonPhrase.toString()) {
reasonPhrase.write(' ${baseResponse.reasonPhrase}');
}
}

if (baseResponse.body.isNotEmpty) {
bodyResponseMessage = baseResponse.body;
bodyResponseMessage.write(baseResponse.body);

if (!_logBody && !_logHeaders) {
bytes = ', ${response.bodyBytes.length}-byte body';
bytes.write(', ${response.bodyBytes.length}-byte body');
}
}
}

// Always start on a new line
_logger.info(ChopperLogRecord('', response: response));
_logger.info(ChopperLogRecord(
'<-- $reasonPhrase ${baseResponse.request?.method} ${baseResponse.request?.url.toString()} (${stopWatch.elapsedMilliseconds}ms$bytes)',
response: response,
));
_logger.info(
ChopperLogRecord(
'<-- $reasonPhrase ${baseResponse.request?.method} ${baseResponse.request?.url.toString()} (${stopWatch.elapsedMilliseconds}ms$bytes)',
response: response,
),
);

if (_logHeaders) {
baseResponse.headers.forEach(
(k, v) => _logger.info(ChopperLogRecord('$k: $v', response: response)),
(String k, String v) => _logger.info(
ChopperLogRecord('$k: $v', response: response),
),
);

if (baseResponse.contentLength != null &&
baseResponse.headers['content-length'] == null) {
_logger.info(ChopperLogRecord(
'content-length: ${baseResponse.contentLength}',
response: response,
));
_logger.info(
ChopperLogRecord(
'content-length: ${baseResponse.contentLength}',
response: response,
),
);
}
}

if (_logBody && bodyResponseMessage.isNotEmpty) {
_logger.info(ChopperLogRecord('', response: response));
_logger.info(ChopperLogRecord(bodyResponseMessage, response: response));
_logger.info(
ChopperLogRecord(
bodyResponseMessage.toString(),
response: response,
),
);
}

if (_logBody || _logHeaders) {
Expand Down
4 changes: 2 additions & 2 deletions chopper/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: chopper
description: Chopper is an http client generator using source_gen, inspired by Retrofit
version: 8.0.0-rc.2
version: 8.0.0
documentation: https://hadrien-lejard.gitbook.io/chopper
repository: https://github.com/lejard-h/chopper

Expand All @@ -26,7 +26,7 @@ dev_dependencies:
lints: ^3.0.0
test: ^1.25.4
transparent_image: ^2.0.1
chopper_generator: ">=8.0.0-rc.1 <9.0.0" # Will be replaced with ^8.0.0 once released
chopper_generator: ">=8.0.0-rc.2 <9.0.0" # will be replaced by ^8.0.0 in the next release

dependency_overrides:
chopper_generator:
Expand Down
Loading

0 comments on commit df9801a

Please sign in to comment.