dart-jaguar.apilib.mustache Maven / Gradle / Ivy
library {{pubName}}.api;
import 'package:http/http.dart';
import 'package:jaguar_serializer/jaguar_serializer.dart';
import 'package:jaguar_retrofit/jaguar_retrofit.dart';
import 'package:{{pubName}}/auth/api_key_auth.dart';
import 'package:{{pubName}}/auth/basic_auth.dart';
import 'package:{{pubName}}/auth/oauth.dart';
{{#apiInfo}}{{#apis}}import 'package:{{pubName}}/api/{{classFilename}}.dart';
{{/apis}}{{/apiInfo}}
{{#models}}{{#model}}import 'package:{{pubName}}/model/{{classFilename}}.dart';
{{/model}}{{/models}}
final jsonJaguarRepo = JsonRepo()
{{#models}}{{#model}}..add({{classname}}Serializer())
{{/model}}{{/models}};
final _defaultInterceptors = [OAuthInterceptor(), BasicAuthInterceptor(), ApiKeyAuthInterceptor()];
class JaguarApiGen {
List interceptors;
String basePath = "{{basePath}}";
Route _baseRoute;
/**
* Add custom global interceptors, put overrideInterceptors to true to set your interceptors only (auth interceptors will not be added)
*/
JaguarApiGen({List interceptors, bool overrideInterceptors = false, String baseUrl}) {
_baseRoute = Route(baseUrl ?? basePath).withClient(globalClient ?? IOClient());
if(interceptors == null) {
this.interceptors = _defaultInterceptors;
}
else if(overrideInterceptors){
this.interceptors = interceptors;
}
else {
this.interceptors = List.from(_defaultInterceptors)..addAll(interceptors);
}
this.interceptors.forEach((interceptor) {
_baseRoute.before(interceptor.before);
_baseRoute.after(interceptor.after);
});
}
void setOAuthToken(String name, String token) {
(_defaultInterceptors[0] as OAuthInterceptor).tokens[name] = token;
}
void setBasicAuth(String name, String username, String password) {
(_defaultInterceptors[1] as BasicAuthInterceptor).authInfo[name] = BasicAuthInfo(username, password);
}
void setApiKey(String name, String apiKey) {
(_defaultInterceptors[2] as ApiKeyAuthInterceptor).apiKeys[name] = apiKey;
}
{{#apiInfo}}{{#apis}}
/**
* Get {{classname}} instance, base route and serializer can be overridden by a given but be careful,
* by doing that all interceptors will not be executed
*/
{{classname}} get{{classname}}({Route base, SerializerRepo serializers}) {
if(base == null) {
base = _baseRoute;
}
if(serializers == null) {
serializers = jsonJaguarRepo;
}
return {{classname}}(base: base, serializers: serializers);
}
{{/apis}}{{/apiInfo}}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy