8000 add meta data capabilities on route by jaumard · Pull Request #13 · Jaguar-dart/client · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

add meta data capabilities on route #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion resty/lib/src/jaguar_resty_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ OptionsMethod options(String url) => OptionsMethod(url);
ht.BaseClient globalClient;

class RouteBase {
RouteBase([String url]) {
Map<String, dynamic> metaData;

RouteBase([String url, Map<String, dynamic> metaData]) {
if (url != null) setUrl(url);
}

Expand Down Expand Up @@ -153,6 +155,11 @@ class RouteBase {
return this;
}

RouteBase setMetaData(Map<String, dynamic> metaData) {
this.metaData = metaData;
return this;
}

RouteBase setUrl(String url) {
final purl = Uri.parse(url);
if (purl.hasAuthority) origin(purl.origin);
Expand Down Expand Up @@ -278,6 +285,8 @@ class Get extends RouteBase {

Get query(String key, value) => super.query(key, value);

Get setMetaData(Map<String, dynamic> metaData) => super.setMetaData(metaData);

Get queries(Map<String, dynamic> value) => super.queries(value);

Get header(String key, String value) => super.header(key, value);
Expand Down Expand Up @@ -415,6 +424,8 @@ class Post extends RouteBase {

Post query(String key, value) => super.query(key, value);

Post setMetaData(Map<String, dynamic> metaData) => super.setMetaData(metaData);

Post queries(Map<String, dynamic> value) => super.queries(value);

Post header(String key, String value) => super.header(key, value);
Expand Down Expand Up @@ -639,6 +650,8 @@ class Put extends RouteBase {

Put query(String key, value) => super.query(key, value);

Put setMetaData(Map<String, dynamic> metaData) => super.setMetaData(metaData);

Put queries(Map<String, dynamic> value) => super.queries(value);

Put header(String key, String value) => super.header(key, value);
Expand Down Expand Up @@ -862,6 +875,8 @@ class Delete extends RouteBase {

Delete query(String key, value) => super.query(key, value);

Delete setMetaData(Map<String, dynamic> metaData) => super.setMetaData(metaData);

Delete queries(Map<String, dynamic> value) => super.queries(value);

Delete header(String key, String value) => super.header(key, value);
Expand Down Expand Up @@ -1000,6 +1015,8 @@ class OptionsMethod extends RouteBase {

OptionsMethod query(String key, value) => super.query(key, value);

OptionsMethod setMetaData(Map<String, dynamic> metaData) => super.setMetaData(metaData);

OptionsMethod queries(Map<String, dynamic> value) => super.queries(value);

OptionsMethod header(String key, String value) => super.header(key, value);
Expand Down
11 changes: 8 additions & 3 deletions retrofit/example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import 'package:jaguar/jaguar.dart' as jaguar;
part 'example.jretro.dart';

/// Example showing how to define an [ApiClient]
@GenApiClient(path: "/users")
@GenApiClient(path: "/users", metaData: {"base": "test"})
class UserApi extends _$UserApiClient implements ApiClient {
final resty.Route base;

final SerializerRepo serializers;

UserApi({this.base, this.serializers});

@GetReq("/:id")
@GetReq("/:id", {"token": "test", "bool": true, "int": 1, "double": 2.2})
Future<User> getUserById(String id, @QueryParam("test") String test);

@PostReq("/")
Expand Down Expand Up @@ -75,7 +75,12 @@ void server() async {

void client() async {
globalClient = IOClient();
var api = UserApi(base: route("http://localhost:10000"), serializers: repo);
var api = UserApi(base: route("http://localhost:10000")..interceptBefore((route) {
if(route.metaData != null) {
print("Meta data");
print(route.metaData);
}
}), serializers: repo);
8000
try {
await api.login(Login(username: 'teja', password: 'pass'));
Expand Down
44 changes: 39 additions & 5 deletions retrofit/example/example.jretro.dart

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

3 changes: 2 additions & 1 deletion retrofit/lib/annotations/annotations.dart
const GenApiClient({this.path, this.metaData});
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export 'params.dart';

class GenApiClient {
final String path;
const GenApiClient({this.path});
final Map<String, dynamic> metaData;
}
19 changes: 13 additions & 6 deletions retrofit/lib/annotations/requests.dart
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
abstract class Req {
String get path;
Map<String, dynamic> get metaData;
}

class GetReq implements Req {
final String path;
const GetReq([this.path = ""]);
final Map<String, dynamic> metaData;
const GetReq([this.path = "", this.metaData]);
}

class PostReq implements Req {
final String path;
const PostReq([this.path = ""]);
final Map<String, dynamic> metaData;
const PostReq([this.path = "", this.metaData]);
}

class PutReq implements Req {
final String path;
const PutReq([this.path = ""]);
final Map<String, dynamic> metaData;
const PutReq([this.path = "", this.metaData]);
}

class DeleteReq implements Req {
final String path;
const DeleteReq([this.path = ""]);
final Map<String, dynamic> metaData;
const DeleteReq([this.path = "", this.metaData]);
}

class HeadReq implements Req {
final String path;
const HeadReq([this.path = ""]);
final Map<String, dynamic> metaData;
const HeadReq([this.path = "", this.metaData]);
}

class PatchReq implements Req {
final String path;
const PatchReq([this.path = "/"]);
final Map<String, dynamic> metaData;
const PatchReq([this.path = "/", this.metaData]);
}
3 changes: 2 additions & 1 deletion retrofit/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ dependencies:
jaguar_serializer: ^2.2.0
http: ^0.11.3
meta: ^1.1.5
jaguar_resty: ^2.6.10

dev_dependencies:
test: ^1.3.0
Expand All @@ -22,3 +21,5 @@ dev_dependencies:
jaguar: ^2.2.20
jaguar_retrofit_gen:
path: ../retrofit_gen
jaguar_resty:
path: ../resty
7 changes: 6 additions & 1 deletion retrofit_gen/lib/parsed_info/parsed_info.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart' show MethodElement;
import 'package:analyzer/dart/element/type.dart';
import 'package:jaguar_retrofit_gen/utils/utils.dart';
Expand Down Expand Up @@ -88,6 +89,8 @@ class Req {

final String path;

final Map<DartObject, DartObject> metaData;

final Set<String> pathParams;

final Map<String, String> query;
Expand All @@ -106,6 +109,7 @@ class Req {

Req(this.method, this.me,
{this.path,
this.metaData,
this.query,
this.headers,
this.body,
Expand All @@ -118,8 +122,9 @@ class Req {
class WriteInfo {
final String name;
final String basePath;
final Map<DartObject, DartObject> baseMetaData;

final List<Req> requests;

WriteInfo(this.name, this.basePath, this.requests);
WriteInfo(this.name, this.basePath, this.baseMetaData, this.requests);
}
6 changes: 5 additions & 1 deletion retrofit_gen/lib/parser/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import '../parsed_info/parsed_info.dart';
Req _parseReq(String httpMethod, DartObject annot, MethodElement method) {
final reader = ConstantReader(annot);
String path = reader.read('path').stringValue;
final metaDataContent = reader.read('metaData');
Map<DartObject, DartObject> metaData = metaDataContent.isNull ? null : metaDataContent.mapValue;
var varPathSegs = <String>[];
if (path != null)
varPathSegs = path
Expand Down Expand Up @@ -91,6 +93,7 @@ Req _parseReq(String httpMethod, DartObject annot, MethodElement method) {
return Req(httpMethod, method,
path: path,
query: query,
metaData: metaData,
headers: headers,
body: body,
pathParams: pathParams,
Expand All @@ -102,6 +105,7 @@ WriteInfo parse(ClassElement element, ConstantReader annotation) {

final an = isGenApiClient.firstAnnotationOfExact(element);
final basePath = an.getField("path").toStringValue();
final baseMetaData = an.getField("metaData").toMapValue();

final reqs = <Req>[];

Expand All @@ -123,5 +127,5 @@ WriteInfo parse(ClassElement element, ConstantReader annotation) {
}
}

return WriteInfo(element.displayName, basePath, reqs);
return WriteInfo(element.displayName, basePath, baseMetaData, reqs);
}
36 changes: 36 additions & 0 deletions retrofit_gen/lib/writer/writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,49 @@ class Writer {
sb.writeln('}');
}

void _writeMetaData(Map<dynamic, dynamic> metaData) {
sb.write('.setMetaData({');
metaData.forEach((key, value) {
sb.write('"${key.toStringValue()}":');

if(value.toStringValue() != null) {
sb.write('"${value.toStringValue()}",');
}
else if(value.toBoolValue() != null) {
sb.write('${value.toBoolValue()},');
}
else if(value.toIntValue() != null) {
sb.write('${value.toIntValue()},');
}
else if(value.toDoubleValue() != null) {
sb.write('${value.toDoubleValue()},');
}
else if(value is List) {
throw UnsupportedError("$key: List is not supported as meta data");
}
else if(value is Map) {
throw UnsupportedError("$key: Map is not supported as meta data");
}
else {
throw UnsupportedError("$key: Type is not supported as meta data");
}
});
sb.write('})');
}

void _writeRequest(Req r) {
sb.write(_writeFunctionSignature(r.me));

sb.writeln(' async {');

sb.write('var req = base.${r.method}');

if (r.metaData != null) {
_writeMetaData(r.metaData);
} else if(i.baseMetaData != null) {
_writeMetaData(i.baseMetaData);
}

if (i.basePath != null) sb.write('.path(basePath)');
if (r.path != null) sb.write('.path("${r.path}")');

Expand Down
0