-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Code generated by oto; DO NOT EDIT. | ||
|
||
import 'package:json_annotation/json_annotation.dart'; | ||
import 'package:dio/dio.dart'; | ||
|
||
class Client { | ||
final Dio httpClient; | ||
final String endpoint; | ||
final String bearerToken; | ||
|
||
Client( | ||
this.httpClient, | ||
this.endpoint, | ||
this.bearerToken, | ||
); | ||
|
||
// getAuthorizationHeader returns the value for the Authorization header. | ||
String getAuthorizationHeader() { | ||
return 'Bearer $bearerToken'; | ||
} | ||
} | ||
|
||
class Error { | ||
@JsonKey(name: 'error') | ||
final String errorMessage; | ||
|
||
Error({ | ||
required this.errorMessage, | ||
}); | ||
|
||
factory Error.fromJson(Map<String, dynamic> json) { | ||
return Error( | ||
errorMessage: json['error'], | ||
); | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
return { | ||
'error': errorMessage, | ||
}; | ||
} | ||
} | ||
|
||
<%= for (service) in def.Services { %> | ||
class <%= service.Name %>Service { | ||
final Client Client; | ||
<%= service.Name %>Service({ | ||
required this.Client, | ||
}); | ||
<%= for (method) in service.Methods { %> | ||
Future<<%= method.OutputObject.TypeName %>> <%= camelize_down(method.Name) %>(<%= method.InputObject.TypeName %> request) async { | ||
final response = await Client.httpClient.post( | ||
'${Client.endpoint}/<%= service.Name %>/<%= method.Name %>', | ||
data: request.toJson(), | ||
options: Options(headers: { | ||
'Content-Type': 'application/json; charset=utf-8', | ||
'Accept': 'application/json', | ||
'Agent': 'otohttp-dart-client/0.1.0', | ||
'Authorization': Client.getAuthorizationHeader(), | ||
}) | ||
); | ||
if (response.statusCode != 200) { | ||
throw Error.fromJson(response.data); | ||
} | ||
return <%= method.OutputObject.TypeName %>.fromJson(response.data); | ||
} | ||
<% } %> | ||
} | ||
<% } %> | ||
|
||
<%= for (object) in def.Objects { %> | ||
@JsonSerializable() | ||
class <%= object.Name %> { | ||
<%= for (field) in object.Fields { %> | ||
@JsonKey(name: '<%= camelize_down(field.Name) %>') | ||
final <%= raw(field.Type.DartType) %><%= if (!field.Metadata["required"]) { %>?<% } %> <%= camelize_down(field.Name) %>; | ||
<% } %> | ||
<%= object.Name %>(<%= if (len(object.Fields) > 0) { %>{<%= for (field) in object.Fields { %> | ||
<%= if (field.Metadata["required"] == true) { %>required <% } %>this.<%= camelize_down(field.Name) %>,<% } %> | ||
}<% } %>); | ||
|
||
factory <%= object.Name %>.fromJson(Map<String, dynamic> json) { | ||
return <%= object.Name %>(<%= for (field) in object.Fields { %> | ||
<%= camelize_down(field.Name) %>: <%= if (field.Type.IsObject) { %> <%= field.Type.CleanObjectName %>.fromJson(json['<%= camelize_down(field.Name) %>']) <% } else { %> json['<%= camelize_down(field.Name) %>']<% } %>,<% } %> | ||
); | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
return {<%= for (field) in object.Fields { %> | ||
'<%= camelize_down(field.Name) %>': <%= camelize_down(field.Name) %>,<% } %> | ||
}; | ||
} | ||
|
||
} | ||
<% } %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters