Skip to content

Commit

Permalink
2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lejard-h committed Feb 16, 2019
1 parent e916cf8 commit 9bbaba6
Show file tree
Hide file tree
Showing 30 changed files with 818 additions and 239 deletions.
6 changes: 6 additions & 0 deletions chopper/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## 2.0.0

- Fix type safety
- Fix json converter
- Handle BuiltList

## 2.0.0

- Request is now containing baseUrl
- Can call `Request.toHttpRequest()` direclty to get the `http.BaseRequest` will receive
- If a full url is specified in the `path` (ex: @Get(path: 'https://...')), it won't be concaten with the baseUrl of the ChopperClient and the ChopperAPI
Expand Down
12 changes: 6 additions & 6 deletions chopper/example/definition.chopper.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions chopper/lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ class ChopperClient {
return request;
}

Future<Response> _decodeResponse<Value>(
Future<Response<Body>> _decodeResponse<Body, ToDecode>(
Response response,
Converter withConverter,
) async {
if (withConverter == null) return response;

final converted = await withConverter.convertResponse<Value>(response);

final converted = await withConverter.convertResponse<ToDecode>(response);
if (converted == null) {
throw Exception("No converter found for type $Value");
throw Exception("No converter found for type $ToDecode");
}

return converted;
Expand Down Expand Up @@ -121,10 +121,10 @@ class ChopperClient {
return res;
}

Future<Response> send<Value>(
Future<Response<Body>> send<Body, ToDecode>(
Request request, {
ConvertRequest requestConverter,
ConvertResponse<Value> responseConverter,
ConvertResponse responseConverter,
}) async {
Request req = request;

Expand All @@ -148,7 +148,7 @@ class ChopperClient {
if (responseConverter != null) {
res = await responseConverter(res);
} else {
res = await _decodeResponse<Value>(res, converter);
res = await _decodeResponse<Body, ToDecode>(res, converter);
}
} else {
res = await _decodeResponse(res, errorConverter);
Expand Down
11 changes: 5 additions & 6 deletions chopper/lib/src/interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class JsonConverter implements Converter {
@override
Response convertResponse<ConvertedResponseType>(Response response) {
var contentType = response.headers[contentTypeKey];
var body = response.body;
if (contentType != null && contentType.contains(jsonHeaders)) {
// If we're decoding JSON, there's some ambiguity in https://tools.ietf.org/html/rfc2616
// about what encoding should be used if the content-type doesn't contain a 'charset'
Expand All @@ -133,13 +134,11 @@ class JsonConverter implements Converter {
// https://tools.ietf.org/html/rfc8259 says that JSON must be encoded using UTF-8. So,
// we're going to explicitly decode using UTF-8... if we don't do this, then we can easily
// end up with our JSON string containing incorrectly decoded characters.
return response.replace(
body: _tryDecodeJson(
utf8.decode(response.bodyBytes),
),
);
body = utf8.decode(response.bodyBytes);
}
return response;
return response.replace(
body: _tryDecodeJson(body),
);
}

dynamic _tryDecodeJson(String data) {
Expand Down
2 changes: 1 addition & 1 deletion 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 and inspired from retrofit
version: 2.0.0
version: 2.1.0
homepage: https://github.com/lejard-h/chopper
author: Hadrien Lejard <hadrien.lejard@gmail.com>

Expand Down
18 changes: 9 additions & 9 deletions chopper/test/test_service.chopper.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions chopper_generator/.packages
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by pub on 2019-02-12 20:57:44.854499.
# Generated by pub on 2019-02-16 14:20:09.365791.
analyzer:file:///Users/lejard_h/.pub-cache/hosted/pub.dartlang.org/analyzer-0.33.3/lib/
args:file:///Users/lejard_h/.pub-cache/hosted/pub.dartlang.org/args-1.4.4/lib/
async:file:///Users/lejard_h/.pub-cache/hosted/pub.dartlang.org/async-2.0.8/lib/
Expand All @@ -7,7 +7,7 @@ build:file:///Users/lejard_h/.pub-cache/hosted/pub.dartlang.org/build-1.0.1/lib/
built_collection:file:///Users/lejard_h/.pub-cache/hosted/pub.dartlang.org/built_collection-3.1.3/lib/
built_value:file:///Users/lejard_h/.pub-cache/hosted/pub.dartlang.org/built_value-5.5.5/lib/
charcode:file:///Users/lejard_h/.pub-cache/hosted/pub.dartlang.org/charcode-1.1.2/lib/
chopper:file:///Users/lejard_h/.pub-cache/hosted/pub.dartlang.org/chopper-2.0.0/lib/
chopper:../chopper/lib/
code_builder:file:///Users/lejard_h/.pub-cache/hosted/pub.dartlang.org/code_builder-3.1.2/lib/
collection:file:///Users/lejard_h/.pub-cache/hosted/pub.dartlang.org/collection-1.14.11/lib/
convert:file:///Users/lejard_h/.pub-cache/hosted/pub.dartlang.org/convert-2.0.2/lib/
Expand Down
17 changes: 14 additions & 3 deletions chopper_generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:analyzer/dart/element/type.dart';

import 'package:build/build.dart';
import 'package:build/src/builder/build_step.dart';
import 'package:built_collection/built_collection.dart';
import 'package:dart_style/dart_style.dart';

import 'package:source_gen/source_gen.dart';
Expand Down Expand Up @@ -114,6 +115,8 @@ class ChopperGenerator extends GeneratorForAnnotation<chopper.ChopperApi> {
final headers = _generateHeaders(m, method);
final url = _generateUrl(method, paths, baseUrl);
final responseType = _getResponseType(m.returnType);
final responseInnerType =
_getResponseInnerType(m.returnType) ?? responseType;

return new Method((b) {
b.name = m.displayName;
Expand Down Expand Up @@ -198,6 +201,7 @@ class ChopperGenerator extends GeneratorForAnnotation<chopper.ChopperApi> {
final typeArguments = <Reference>[];
if (responseType != null) {
typeArguments.add(refer(responseType.displayName));
typeArguments.add(refer(responseInnerType.displayName));
}

blocks.add(refer("client.send")
Expand Down Expand Up @@ -285,15 +289,22 @@ class ChopperGenerator extends GeneratorForAnnotation<chopper.ChopperApi> {
}

DartType _getResponseType(DartType type) {
return _genericOf(_genericOf(type));
}

DartType _getResponseInnerType(DartType type) {
final generic = _genericOf(type);

if (generic == null || _typeChecker(Map).isExactlyType(type)) return type;
if (generic == null ||
_typeChecker(Map).isExactlyType(type) ||
_typeChecker(BuiltMap).isExactlyType(type)) return type;

if (generic.isDynamic) return null;

if (_typeChecker(List).isExactlyType(type)) return generic;
if (_typeChecker(List).isExactlyType(type) ||
_typeChecker(BuiltList).isExactlyType(type)) return generic;

return _getResponseType(generic);
return _getResponseInnerType(generic);
}

Expression _generateUrl(
Expand Down
10 changes: 5 additions & 5 deletions chopper_generator/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ packages:
source: hosted
version: "1.0.1"
built_collection:
dependency: transitive
dependency: "direct main"
description:
name: built_collection
url: "https://pub.dartlang.org"
Expand All @@ -60,10 +60,10 @@ packages:
chopper:
dependency: "direct main"
description:
name: chopper
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
path: "../chopper"
relative: true
source: path
version: "2.1.0"
code_builder:
dependency: "direct main"
description:
Expand Down
3 changes: 2 additions & 1 deletion chopper_generator/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: chopper_generator
description: Chopper is an http client generator using source_gen and inspired from retrofit
version: 2.0.0
version: 2.1.0
homepage: https://github.com/lejard-h/chopper
author: Hadrien Lejard <hadrien.lejard@gmail.com>

Expand All @@ -16,6 +16,7 @@ dependencies:
chopper: ^2.0.0
code_builder: ^3.0.0
dart_style: ^1.0.10
built_collection: ^3.1.3

dev_dependencies:
test: ^1.3.0
Expand Down
Loading

0 comments on commit 9bbaba6

Please sign in to comment.