forked from loafle/openapi-generator-original
Add Dart support
This commit is contained in:
@@ -0,0 +1,282 @@
|
||||
package io.swagger.codegen.languages;
|
||||
|
||||
import io.swagger.codegen.CliOption;
|
||||
import io.swagger.codegen.CodegenConfig;
|
||||
import io.swagger.codegen.CodegenType;
|
||||
import io.swagger.codegen.DefaultCodegen;
|
||||
import io.swagger.codegen.SupportingFile;
|
||||
import io.swagger.models.properties.ArrayProperty;
|
||||
import io.swagger.models.properties.MapProperty;
|
||||
import io.swagger.models.properties.Property;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class DartClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
protected boolean browserClient = true;
|
||||
protected String pubName = "swagger";
|
||||
protected String pubVersion = "1.0.0";
|
||||
protected String pubDescription = "Swagger API client";
|
||||
protected String sourceFolder = "";
|
||||
|
||||
public DartClientCodegen() {
|
||||
super();
|
||||
outputFolder = "generated-code/dart";
|
||||
modelTemplateFiles.put("model.mustache", ".dart");
|
||||
apiTemplateFiles.put("api.mustache", ".dart");
|
||||
templateDir = "dart";
|
||||
apiPackage = "lib.api";
|
||||
modelPackage = "lib.model";
|
||||
|
||||
reservedWords = new HashSet<String>(
|
||||
Arrays.asList(
|
||||
"abstract", "as", "assert", "async", "async*", "await",
|
||||
"break", "case", "catch", "class", "const", "continue",
|
||||
"default", "deferred", "do", "dynamic", "else", "enum",
|
||||
"export", "external", "extends", "factory", "false", "final",
|
||||
"finally", "for", "get", "if", "implements", "import", "in",
|
||||
"is", "library", "new", "null", "operator", "part", "rethrow",
|
||||
"return", "set", "static", "super", "switch", "sync*", "this",
|
||||
"throw", "true", "try", "typedef", "var", "void", "while",
|
||||
"with", "yield", "yield*" )
|
||||
);
|
||||
|
||||
languageSpecificPrimitives = new HashSet<String>(
|
||||
Arrays.asList(
|
||||
"String",
|
||||
"bool",
|
||||
"num",
|
||||
"int",
|
||||
"float")
|
||||
);
|
||||
instantiationTypes.put("array", "List");
|
||||
instantiationTypes.put("map", "Map");
|
||||
|
||||
typeMapping = new HashMap<String, String>();
|
||||
typeMapping.put("Array", "List");
|
||||
typeMapping.put("array", "List");
|
||||
typeMapping.put("List", "List");
|
||||
typeMapping.put("boolean", "bool");
|
||||
typeMapping.put("string", "String");
|
||||
typeMapping.put("int", "int");
|
||||
typeMapping.put("float", "num");
|
||||
typeMapping.put("long", "int");
|
||||
typeMapping.put("short", "int");
|
||||
typeMapping.put("char", "String");
|
||||
typeMapping.put("double", "num");
|
||||
typeMapping.put("object", "Object");
|
||||
typeMapping.put("integer", "int");
|
||||
typeMapping.put("Date", "DateTime");
|
||||
typeMapping.put("date", "DateTime");
|
||||
typeMapping.put("File", "MultipartFile");
|
||||
|
||||
cliOptions.add(new CliOption("browserClient", "Is the client browser based"));
|
||||
cliOptions.add(new CliOption("pubName", "Name in generated pubspec"));
|
||||
cliOptions.add(new CliOption("pubVersion", "Version in generated pubspec"));
|
||||
cliOptions.add(new CliOption("sourceFolder", "source folder for generated code"));
|
||||
}
|
||||
|
||||
public CodegenType getTag() {
|
||||
return CodegenType.CLIENT;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return "dart";
|
||||
}
|
||||
|
||||
public String getHelp() {
|
||||
return "Generates a Dart client library.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processOpts() {
|
||||
super.processOpts();
|
||||
|
||||
if (additionalProperties.containsKey("browserClient")) {
|
||||
this.setBrowserClient(Boolean.parseBoolean((String) additionalProperties.get("browserClient")));
|
||||
additionalProperties.put("browserClient", browserClient);
|
||||
} else {
|
||||
//not set, use to be passed to template
|
||||
additionalProperties.put("browserClient", browserClient);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("pubName")) {
|
||||
this.setPubName((String) additionalProperties.get("pubName"));
|
||||
} else {
|
||||
//not set, use to be passed to template
|
||||
additionalProperties.put("pubName", pubName);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("pubVersion")) {
|
||||
this.setPubVersion((String) additionalProperties.get("pubVersion"));
|
||||
} else {
|
||||
//not set, use to be passed to template
|
||||
additionalProperties.put("pubVersion", pubVersion);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("pubDescription")) {
|
||||
this.setPubDescription((String) additionalProperties.get("pubDescription"));
|
||||
} else {
|
||||
//not set, use to be passed to template
|
||||
additionalProperties.put("pubDescription", pubDescription);
|
||||
}
|
||||
|
||||
if (additionalProperties.containsKey("sourceFolder")) {
|
||||
this.setSourceFolder((String) additionalProperties.get("sourceFolder"));
|
||||
}
|
||||
|
||||
final String libFolder = sourceFolder + File.separator + "lib";
|
||||
supportingFiles.add(new SupportingFile("pubspec.mustache", "", "pubspec.yaml"));
|
||||
supportingFiles.add(new SupportingFile("api_client.mustache", libFolder, "api_client.dart"));
|
||||
supportingFiles.add(new SupportingFile("apiException.mustache", libFolder, "api_exception.dart"));
|
||||
supportingFiles.add(new SupportingFile("apilib.mustache", libFolder, "api.dart"));
|
||||
|
||||
final String authFolder = sourceFolder + File.separator + "auth";
|
||||
supportingFiles.add(new SupportingFile("auth/authentication.mustache", authFolder, "authentication.dart"));
|
||||
supportingFiles.add(new SupportingFile("auth/http_basic_auth.mustache", authFolder, "http_basic_auth.dart"));
|
||||
supportingFiles.add(new SupportingFile("auth/api_key_auth.mustache", authFolder, "api_key_auth.dart"));
|
||||
supportingFiles.add(new SupportingFile("auth/oauth.mustache", authFolder, "oauth.dart"));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String escapeReservedWord(String name) {
|
||||
return "_" + name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apiFileFolder() {
|
||||
return outputFolder + "/" + sourceFolder + "/" + apiPackage().replace('.', File.separatorChar);
|
||||
}
|
||||
|
||||
public String modelFileFolder() {
|
||||
return outputFolder + "/" + sourceFolder + "/" + modelPackage().replace('.', File.separatorChar);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toVarName(String name) {
|
||||
// replace - with _ e.g. created-at => created_at
|
||||
name = name.replaceAll("-", "_");
|
||||
|
||||
// if it's all uppper case, do nothing
|
||||
if (name.matches("^[A-Z_]*$")) {
|
||||
return name;
|
||||
}
|
||||
|
||||
// camelize (lower first character) the variable name
|
||||
// pet_id => petId
|
||||
name = camelize(name, true);
|
||||
|
||||
// for reserved word or word starting with number, append _
|
||||
if (reservedWords.contains(name) || name.matches("^\\d.*")) {
|
||||
name = escapeReservedWord(name);
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toParamName(String name) {
|
||||
// should be the same as variable name
|
||||
return toVarName(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toModelName(String name) {
|
||||
// model name cannot use reserved keyword, e.g. return
|
||||
if (reservedWords.contains(name)) {
|
||||
throw new RuntimeException(name + " (reserved word) cannot be used as a model name");
|
||||
}
|
||||
|
||||
// camelize the model name
|
||||
// phone_number => PhoneNumber
|
||||
return camelize(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toModelFilename(String name) {
|
||||
return underscore(toModelName(name));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toApiFilename(String name) {
|
||||
return underscore(toApiName(name));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toDefaultValue(Property p) {
|
||||
if (p instanceof MapProperty) {
|
||||
MapProperty ap = (MapProperty) p;
|
||||
String inner = getSwaggerType(ap.getAdditionalProperties());
|
||||
return "{}";
|
||||
} else if (p instanceof ArrayProperty) {
|
||||
ArrayProperty ap = (ArrayProperty) p;
|
||||
String inner = getSwaggerType(ap.getItems());
|
||||
return "[]";
|
||||
}
|
||||
|
||||
return super.toDefaultValue(p);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTypeDeclaration(Property p) {
|
||||
if (p instanceof ArrayProperty) {
|
||||
ArrayProperty ap = (ArrayProperty) p;
|
||||
Property inner = ap.getItems();
|
||||
return getSwaggerType(p) + "<" + getTypeDeclaration(inner) + ">";
|
||||
} else if (p instanceof MapProperty) {
|
||||
MapProperty mp = (MapProperty) p;
|
||||
Property inner = mp.getAdditionalProperties();
|
||||
|
||||
return getSwaggerType(p) + "<String, " + getTypeDeclaration(inner) + ">";
|
||||
}
|
||||
return super.getTypeDeclaration(p);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSwaggerType(Property p) {
|
||||
String swaggerType = super.getSwaggerType(p);
|
||||
String type = null;
|
||||
if (typeMapping.containsKey(swaggerType)) {
|
||||
type = typeMapping.get(swaggerType);
|
||||
if (languageSpecificPrimitives.contains(type)) {
|
||||
return type;
|
||||
}
|
||||
} else {
|
||||
type = swaggerType;
|
||||
}
|
||||
return toModelName(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toOperationId(String operationId) {
|
||||
// method name cannot use reserved keyword, e.g. return
|
||||
if (reservedWords.contains(operationId)) {
|
||||
throw new RuntimeException(operationId + " (reserved word) cannot be used as method name");
|
||||
}
|
||||
|
||||
return camelize(operationId, true);
|
||||
}
|
||||
|
||||
public void setBrowserClient(boolean browserClient) {
|
||||
this.browserClient = browserClient;
|
||||
}
|
||||
|
||||
public void setPubName(String pubName) {
|
||||
this.pubName = pubName;
|
||||
}
|
||||
|
||||
public void setPubVersion(String pubVersion) {
|
||||
this.pubVersion = pubVersion;
|
||||
}
|
||||
|
||||
public void setPubDescription(String pubDescription) {
|
||||
this.pubDescription = pubDescription;
|
||||
}
|
||||
|
||||
public void setSourceFolder(String sourceFolder) {
|
||||
this.sourceFolder = sourceFolder;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
io.swagger.codegen.languages.AndroidClientCodegen
|
||||
io.swagger.codegen.languages.AsyncScalaClientCodegen
|
||||
io.swagger.codegen.languages.CSharpClientCodegen
|
||||
io.swagger.codegen.languages.DartClientCodegen
|
||||
io.swagger.codegen.languages.FlashClientCodegen
|
||||
io.swagger.codegen.languages.JavaClientCodegen
|
||||
io.swagger.codegen.languages.JaxRSServerCodegen
|
||||
|
||||
83
modules/swagger-codegen/src/main/resources/dart/api.mustache
Normal file
83
modules/swagger-codegen/src/main/resources/dart/api.mustache
Normal file
@@ -0,0 +1,83 @@
|
||||
part of api;
|
||||
|
||||
{{#operations}}
|
||||
|
||||
class {{classname}} {
|
||||
String basePath = "{{basePath}}";
|
||||
ApiClient apiClient = ApiClient.defaultApiClient;
|
||||
|
||||
{{classname}}([ApiClient apiClient]) {
|
||||
if (apiClient != null) {
|
||||
this.apiClient = apiClient;
|
||||
}
|
||||
}
|
||||
|
||||
{{#operation}}
|
||||
/// {{summary}}
|
||||
///
|
||||
/// {{notes}}
|
||||
{{#returnType}}Future<{{{returnType}}}> {{/returnType}}{{^returnType}}Future {{/returnType}}{{nickname}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
|
||||
Object postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
|
||||
{{#requiredParamCount}}
|
||||
// verify required params are set
|
||||
if({{/requiredParamCount}}{{#requiredParams}} {{paramName}} == null {{#hasMore}}|| {{/hasMore}}{{/requiredParams}}{{#requiredParamCount}}) {
|
||||
throw new ApiException(400, "missing required params");
|
||||
}{{/requiredParamCount}}
|
||||
|
||||
// create path and map variables
|
||||
String path = "{{path}}".replaceAll("{format}","json"){{#pathParams}}.replaceAll("{" + "{{paramName}}" + "}", {{{paramName}}}.toString()){{/pathParams}};
|
||||
|
||||
// query params
|
||||
Map<String, String> queryParams = {};
|
||||
Map<String, String> headerParams = {};
|
||||
Map<String, String> formParams = {};
|
||||
{{#queryParams}}if("null" != {{paramName}})
|
||||
queryParams["{{baseName}}"] = {{paramName}} is List ? {{paramName}}.join(',') : {{paramName}};
|
||||
{{/queryParams}}
|
||||
{{#headerParams}}headerParams["{{baseName}}"] = {{paramName}};
|
||||
{{/headerParams}}
|
||||
|
||||
List<String> contentTypes = [{{#consumes}}"{{mediaType}}"{{#hasMore}},{{/hasMore}}{{/consumes}}];
|
||||
|
||||
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
|
||||
List<String> authNames = [{{#authMethods}}"{{name}}"{{#hasMore}}, {{/hasMore}}{{/authMethods}}];
|
||||
|
||||
if(contentType.startsWith("multipart/form-data")) {
|
||||
bool hasFields = false;
|
||||
MultipartRequest mp = new MultipartRequest(null, null);
|
||||
{{#formParams}}{{#notFile}}
|
||||
if ({{paramName}} != null) {
|
||||
hasFields = true;
|
||||
mp.fields['{{baseName}}'] = apiClient.parameterToString({{paramName}});
|
||||
}
|
||||
{{/notFile}}{{#isFile}}
|
||||
if ({{paramName}} != null) {
|
||||
hasFields = true;
|
||||
mp.fields['{{baseName}}'] = {{paramName}}.field;
|
||||
mp.files.add({{paramName}});
|
||||
}
|
||||
{{/isFile}}{{/formParams}}
|
||||
if(hasFields)
|
||||
postBody = mp;
|
||||
}
|
||||
else {
|
||||
{{#formParams}}{{#notFile}}if ({{paramName}} != null)
|
||||
formParams['{{baseName}}'] = apiClient.parameterToString({{paramName}});{{/notFile}}
|
||||
{{/formParams}}
|
||||
}
|
||||
|
||||
return apiClient.invokeAPI(basePath, path, '{{httpMethod}}', queryParams, postBody, headerParams, formParams, contentType, authNames).then((response) {
|
||||
if(response.statusCode >= 400) {
|
||||
throw new ApiException(response.statusCode, response.body);
|
||||
}
|
||||
else if(response.body != null){
|
||||
return {{#returnType}}ApiClient.deserialize(response.body, {{returnBaseType}}){{/returnType}};
|
||||
}
|
||||
else {
|
||||
return {{#returnType}}null{{/returnType}};
|
||||
}
|
||||
});
|
||||
}
|
||||
{{/operation}}
|
||||
}
|
||||
{{/operations}}
|
||||
@@ -0,0 +1,9 @@
|
||||
part of api;
|
||||
|
||||
class ApiException implements Exception {
|
||||
int code = 0;
|
||||
String message = null;
|
||||
|
||||
ApiException(this.code, this.message);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
part of api;
|
||||
|
||||
class ApiClient {
|
||||
static ApiClient defaultApiClient = new ApiClient();
|
||||
|
||||
Map<String, String> _defaultHeaderMap = {};
|
||||
Map<String, Authentication> _authentications = {};
|
||||
static final dson = new Dartson.JSON();
|
||||
final DateFormat _dateFormatter = new DateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
|
||||
|
||||
ApiClient() {
|
||||
// Setup authentications (key: authentication name, value: authentication).{{#authMethods}}{{#isBasic}}
|
||||
_authentications['{{name}}'] = new HttpBasicAuth();{{/isBasic}}{{#isApiKey}}
|
||||
_authentications['{{name}}'] = new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}");{{/isApiKey}}{{#isOAuth}}
|
||||
_authentications['{{name}}'] = new OAuth();{{/isOAuth}}{{/authMethods}}
|
||||
}
|
||||
|
||||
void addDefaultHeader(String key, String value) {
|
||||
_defaultHeaderMap[key] = value;
|
||||
}
|
||||
|
||||
/// Format the given Date object into string.
|
||||
String formatDate(DateTime date) {
|
||||
return _dateFormatter.format(date);
|
||||
}
|
||||
|
||||
/// Format the given parameter object into string.
|
||||
String parameterToString(Object param) {
|
||||
if (param == null) {
|
||||
return '';
|
||||
} else if (param is DateTime) {
|
||||
return formatDate(param);
|
||||
} else if (param is List) {
|
||||
return (param).join(',');
|
||||
} else {
|
||||
return param.toString();
|
||||
}
|
||||
}
|
||||
|
||||
static dynamic deserialize(String json, dynamic clazz) {
|
||||
var result = json;
|
||||
|
||||
try {
|
||||
var decodedJson = JSON.decode(json);
|
||||
|
||||
if(decodedJson is List) {
|
||||
result = [];
|
||||
for(var obj in decodedJson) {
|
||||
result.add(_createEntity(obj, clazz));
|
||||
}
|
||||
} else {
|
||||
result = _createEntity(json, clazz);
|
||||
}
|
||||
} on FormatException {
|
||||
// Just return the passed in value
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static dynamic _createEntity(dynamic json, dynamic clazz) {
|
||||
bool isMap = json is Map;
|
||||
|
||||
switch(clazz) {
|
||||
{{#models}}
|
||||
{{#model}}
|
||||
case {{classname}}:
|
||||
return isMap ? dson.map(json, new {{classname}}()) : dson.decode(json, new {{classname}}());
|
||||
{{/model}}
|
||||
{{/models}}
|
||||
default:
|
||||
throw new ApiException(500, 'Could not find a suitable class for deserialization');
|
||||
}
|
||||
}
|
||||
|
||||
static String serialize(Object obj) {
|
||||
String serialized = '';
|
||||
if (obj == null) {
|
||||
serialized = '';
|
||||
} else if (obj is String) {
|
||||
serialized = obj;
|
||||
} else {
|
||||
serialized = dson.encode(obj);
|
||||
}
|
||||
return serialized;
|
||||
}
|
||||
|
||||
Future<Response> invokeAPI( String host,
|
||||
String path,
|
||||
String method,
|
||||
Map<String, String> queryParams,
|
||||
Object body,
|
||||
Map<String, String> headerParams,
|
||||
Map<String, String> formParams,
|
||||
String contentType,
|
||||
List<String> authNames) {
|
||||
|
||||
updateParamsForAuth(authNames, queryParams, headerParams);
|
||||
|
||||
var client = new {{#browserClient}}Browser{{/browserClient}}Client();
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
||||
for(String key in queryParams.keys) {
|
||||
String value = queryParams[key];
|
||||
if (value != null){
|
||||
if(sb.toString().length == 0) {
|
||||
sb.write("?");
|
||||
} else {
|
||||
sb.write("&");
|
||||
}
|
||||
sb.write(key);
|
||||
sb.write("=");
|
||||
sb.write(value);
|
||||
}
|
||||
}
|
||||
String querystring = sb.toString();
|
||||
|
||||
String url = host + path + querystring;
|
||||
|
||||
headerParams.addAll(_defaultHeaderMap);
|
||||
headerParams['Content-Type'] = contentType;
|
||||
|
||||
var completer = new Completer();
|
||||
|
||||
if(body is MultipartRequest) {
|
||||
var request = new MultipartRequest(method, Uri.parse(url));
|
||||
request.fields.addAll(body.fields);
|
||||
request.files.addAll(body.files);
|
||||
request.headers.addAll(body.headers);
|
||||
request.headers.addAll(headerParams);
|
||||
client.send(request).then((response) => completer.complete(Response.fromStream(response)));
|
||||
} else {
|
||||
var msgBody = contentType == "application/x-www-form-urlencoded" ? formParams : serialize(body);
|
||||
switch(method) {
|
||||
case "GET":
|
||||
return client.get(url, headers: headerParams);
|
||||
case "POST":
|
||||
return client.post(url, headers: headerParams, body: msgBody);
|
||||
case "PUT":
|
||||
return client.put(url, headers: headerParams, body: msgBody);
|
||||
case "DELETE":
|
||||
return client.delete(url, headers: headerParams);
|
||||
}
|
||||
}
|
||||
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
/// Update query and header parameters based on authentication settings.
|
||||
/// @param authNames The authentications to apply
|
||||
void updateParamsForAuth(List<String> authNames, Map<String, String> queryParams, Map<String, String> headerParams) {
|
||||
authNames.forEach((authName) {
|
||||
Authentication auth = _authentications[authName];
|
||||
if (auth == null) throw new ArgumentError("Authentication undefined: " + authName);
|
||||
auth.applyToParams(queryParams, headerParams);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
library api;
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';{{#browserClient}}
|
||||
import 'dart:html';
|
||||
import 'package:http/browser_client.dart';{{/browserClient}}
|
||||
import 'package:http/http.dart';
|
||||
import 'package:dartson/dartson.dart';
|
||||
import 'package:crypto/crypto.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
part 'api_client.dart';
|
||||
part 'api_exception.dart';
|
||||
part 'auth/authentication.dart';
|
||||
part 'auth/api_key_auth.dart';
|
||||
part 'auth/oauth.dart';
|
||||
part 'auth/http_basic_auth.dart';
|
||||
|
||||
{{#apiInfo}}{{#apis}}part 'api/{{classVarName}}_api.dart';
|
||||
{{/apis}}{{/apiInfo}}
|
||||
{{#models}}{{#model}}part 'model/{{classVarName}}.dart';
|
||||
{{/model}}{{/models}}
|
||||
@@ -0,0 +1,28 @@
|
||||
part of api;
|
||||
|
||||
class ApiKeyAuth implements Authentication {
|
||||
|
||||
final String location;
|
||||
final String paramName;
|
||||
String apiKey;
|
||||
String apiKeyPrefix;
|
||||
|
||||
ApiKeyAuth(this.location, this.paramName);
|
||||
|
||||
@override
|
||||
void applyToParams(Map<String, String> queryParams, Map<String, String> headerParams) {
|
||||
String value;
|
||||
if (apiKeyPrefix != null) {
|
||||
value = '$apiKeyPrefix $apiKey';
|
||||
} else {
|
||||
value = apiKey;
|
||||
}
|
||||
|
||||
if (location == 'query' && value != null) {
|
||||
queryParams[paramName] = value;
|
||||
} else if (location == 'header' && value != null) {
|
||||
headerParams[paramName] = value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
part of api;
|
||||
|
||||
abstract class Authentication {
|
||||
|
||||
/// Apply authentication settings to header and query params.
|
||||
void applyToParams(Map<String, String> queryParams, Map<String, String> headerParams);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
part of api;
|
||||
|
||||
class HttpBasicAuth implements Authentication {
|
||||
|
||||
String username;
|
||||
String password;
|
||||
|
||||
@override
|
||||
void applyToParams(Map<String, String> queryParams, Map<String, String> headerParams) {
|
||||
String str = (username == null ? "" : username) + ":" + (password == null ? "" : password);
|
||||
headerParams["Authorization"] = "Basic " + CryptoUtils.bytesToBase64(UTF8.encode(str));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
part of api;
|
||||
|
||||
class OAuth implements Authentication {
|
||||
|
||||
@override
|
||||
void applyToParams(Map<String, String> queryParams, Map<String, String> headerParams) {
|
||||
// TODO: support oauth
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
part of api;
|
||||
|
||||
{{#models}}{{#model}}
|
||||
@Entity()
|
||||
class {{classname}} {
|
||||
{{#vars}}{{#description}}/* {{{description}}} */{{/description}}
|
||||
{{{datatype}}} {{name}} = {{{defaultValue}}};
|
||||
{{#allowableValues}}{{#min}} // range from {{min}} to {{max}}{{/min}}//{{^min}}enum {{name}}Enum { {{#values}} {{.}}, {{/values}} };{{/min}}{{/allowableValues}}
|
||||
{{/vars}}
|
||||
{{classname}}();
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return '{{classname}}[{{#vars}}{{name}}=${{name}}, {{/vars}}]';
|
||||
}
|
||||
|
||||
}
|
||||
{{/model}}{{/models}}
|
||||
@@ -0,0 +1,15 @@
|
||||
name: {{pubName}}
|
||||
version: {{pubVersion}}
|
||||
description: {{pubDescription}}
|
||||
dependencies:
|
||||
http: '>=0.11.1 <0.12.0'
|
||||
dartson: "^0.2.4"
|
||||
crypto: "^0.9.0"
|
||||
intl: "^0.12.4+2"
|
||||
|
||||
dev_dependencies:
|
||||
guinness: '^0.1.17'
|
||||
browser: any
|
||||
|
||||
transformers:
|
||||
- dartson
|
||||
Reference in New Issue
Block a user