Skip to content

Commit

Permalink
feat: add generic type cast support (#26)
Browse files Browse the repository at this point in the history
* feat: add model type cast support

* docs: add model casting

* chore: clean Reponse from generic

* test: add unit tests for generic auto cast

* chore: bump generator to 0.3.0
  • Loading branch information
trevorwang authored Jul 11, 2019
1 parent 3327e06 commit 09bb663
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 322 deletions.
64 changes: 24 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ dev_dependencies:
### Define and Generate your API
```dart
import 'dart:io';

import 'package:retrofit/http.dart';
import 'package:dio/dio.dart';
import 'package:retrofit_example/http_get.dart';

part 'demo.retrofit.dart';
part 'example.g.dart';

@RestApi(baseUrl: "https://httpbin.org/")
abstract class RestClient {
Expand All @@ -36,63 +39,44 @@ abstract class RestClient {
@Headers({
"Header-One": " header 1",
})
Future<Response<String>> ip(@Query('query1') String query,
Future<HttpGet> ip(@Query('query1') String query,
{@Queries() Map<String, dynamic> queryies,
@Header("Header-Two") String header});

@GET("/profile/{id}")
Future<Response<String>> profile(@Path("id") String id,
{@Query("role") String role = "user",
@Queries() Map<String, dynamic> map = const {},
@Body() Map<String, dynamic> map2});

@POST("/post")
@Headers({
"Accept": "application/json",
})
Future<Response<String>> createProfile(@Query('query2') String query,
{@Queries() Map<String, dynamic> queryies,
@Header("Header-One") String header,
@Body() Map<String, dynamic> map2,
@Field() int field,
@Field("field-g") String ffff});

@PUT("/put")
Future<Response<String>> updateProfile2(@Query('query3') String query,
{@Queries() Map<String, dynamic> queryies,
@Header("Header-One") String header,
@Field() int field,
@Field("field-g") String ffff});

@PATCH("/patch")
Future<Response<String>> updateProfile(@Query('query4') String query,
{@Queries() Map<String, dynamic> queryies,
@Field() int field,
@Field("field-g") String ffff});
}

@JsonSerializable()
class HttpGet {
final Map<String, String> headers;
final String origin;
final String url;

HttpGet({this.headers, this.origin, this.url});
// There must be a [fromJson] factory method in model class.
factory HttpGet.fromJson(Map<String, dynamic> json) =>
_$HttpGetFromJson(json);
Map<String, dynamic> toJson() => _$HttpGetToJson(this);
```

then run the generator

```sh
# dart
pub run build_runner build

#flutter
# flutter
flutter packages pub run build_runner build
```

### Use it

```dart
import 'package:retrofit_example/demo.dart';
import 'package:dio/dio.dart';
main(List<String> args) {
final dio = Dio();
dio.options.headers["Demo-Header"] = "demo header";
final client = RestClient.instance(dio);
client.ip("trevor").then((it) => print(it));
dio.options.headers["Content-Type"] = "application/json";
final client = RestClient(dio);
client.ip("trevor").then((it) => print(it.toJson()));
}
```

[More details](example/lib/example.dart)
2 changes: 1 addition & 1 deletion annotation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
```dart
@http.POST('/path/')
@dio.Extra({'my_key':'my_value'})
Future<Response<String>>> myMethod();
Future<String>> myMethod();
```

* Fixed general dart style and code conventions
Expand Down
115 changes: 0 additions & 115 deletions annotation/README.md

This file was deleted.

1 change: 1 addition & 0 deletions annotation/README.md
6 changes: 3 additions & 3 deletions annotation/lib/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Method {
///
/// ```
/// @GET("ip")
/// Future<Response<String>> ip(@Query('query1') String query)
/// Future<String> ip(@Query('query1') String query)
/// ```
@immutable
class GET extends Method {
Expand Down Expand Up @@ -113,7 +113,7 @@ class Body {
///
/// ```
/// @POST("/post")
/// Future<Response<String>> example(
/// Future<String> example(
/// @Field() int foo,
/// @Field("bar") String barbar},
/// )
Expand Down Expand Up @@ -145,7 +145,7 @@ class Path {
///
///```
/// @GET("/get")
/// Future<Response<String>> foo(@Query('bar') String query)
/// Future<String> foo(@Query('bar') String query)
///```
/// Calling with `foo.friends(1)` yields `/get?bar=1`.
@immutable
Expand Down
3 changes: 1 addition & 2 deletions example/bin/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ main(List<String> args) {
dio.options.headers["Demo-Header"] = "demo header";
dio.options.headers["Content-Type"] = "application/json";
final client = RestClient(dio);

client.ip("trevor").then((it) => print(it));
client.ip("trevor").then((it) => print(it.toJson()));
}
19 changes: 9 additions & 10 deletions example/lib/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';

import 'package:retrofit/http.dart';
import 'package:dio/dio.dart';
import 'package:retrofit_example/http_get.dart';

part 'example.g.dart';

Expand All @@ -13,12 +14,12 @@ abstract class RestClient {
@Headers({
"Header-One": " header 1",
})
Future<Response<String>> ip(@Query('query1') String query,
Future<HttpGet> ip(@Query('query1') String query,
{@Queries() Map<String, dynamic> queryies,
@Header("Header-Two") String header});

@GET("/profile/{id}")
Future<Response<String>> profile(@Path("id") String id,
Future<String> profile(@Path("id") String id,
{@Query("role") String role = "user",
@Queries() Map<String, dynamic> map = const {},
@Body() Map<String, dynamic> map2});
Expand All @@ -27,36 +28,34 @@ abstract class RestClient {
@Headers({
"Accept": "application/json",
})
Future<Response<String>> createProfile(@Query('query2') String query,
Future<String> createProfile(@Query('query2') String query,
{@Queries() Map<String, dynamic> queryies,
@Header("Header-One") String header,
@Body() Map<String, dynamic> map2,
@Field() int field,
@Field("field-g") String ffff});

@PUT("/put")
Future<Response<String>> updateProfile2(@Query('query3') String query,
Future<String> updateProfile2(@Query('query3') String query,
{@Queries() Map<String, dynamic> queryies,
@Header("Header-One") String header,
@Field() int field,
@Field("field-g") String ffff});

@PATCH("/patch")
Future<Response<String>> updateProfile(@Query('query4') String query,
Future<String> updateProfile(@Query('query4') String query,
{@Queries() Map<String, dynamic> queryies,
@Field() int field,
@Field("field-g") String ffff});

@POST("/profile")
Future<Response<String>> setProfile(
@Field('image', 'my_profile_image.jpg') File image);
Future<String> setProfile(@Field('image', 'my_profile_image.jpg') File image);

/// This will add the image name from `image.path.split(Platform.pathSeperator).last`
@POST("/profile")
Future<Response<String>> setProfileImage(@Field() File image);
Future<String> setProfileImage(@Field() File image);

/// This will automatically work too.
@POST("/profile")
Future<Response<String>> setProfileImageWithInfo(
@Field() UploadFileInfo image);
Future<String> setProfileImageWithInfo(@Field() UploadFileInfo image);
}
Loading

0 comments on commit 09bb663

Please sign in to comment.