forked from loafle/openapi-generator-original
[WIP] Add elixir client (#4675)
* Add elixir client * Add test for elixir client * Add shell script for generating sample codes for elixir client It just copied from bin/bash-petstore.sh * Make elixir-petstore.sh to generate sample codes for elixir client * Add sample codes for elixir client
This commit is contained in:
parent
d82c2bbae7
commit
f77bee8b8a
31
bin/elixir-petstore.sh
Executable file
31
bin/elixir-petstore.sh
Executable file
@ -0,0 +1,31 @@
|
||||
#!/bin/sh
|
||||
|
||||
SCRIPT="$0"
|
||||
|
||||
while [ -h "$SCRIPT" ] ; do
|
||||
ls=`ls -ld "$SCRIPT"`
|
||||
link=`expr "$ls" : '.*-> \(.*\)$'`
|
||||
if expr "$link" : '/.*' > /dev/null; then
|
||||
SCRIPT="$link"
|
||||
else
|
||||
SCRIPT=`dirname "$SCRIPT"`/"$link"
|
||||
fi
|
||||
done
|
||||
|
||||
if [ ! -d "${APP_DIR}" ]; then
|
||||
APP_DIR=`dirname "$SCRIPT"`/..
|
||||
APP_DIR=`cd "${APP_DIR}"; pwd`
|
||||
fi
|
||||
|
||||
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
|
||||
|
||||
if [ ! -f "$executable" ]
|
||||
then
|
||||
mvn clean package
|
||||
fi
|
||||
|
||||
# if you've executed sbt assembly previously it will use that instead.
|
||||
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
|
||||
args="$@ generate -t modules/swagger-codegen/src/main/resources/elixir -i modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -l elixir -o samples/client/petstore/elixir"
|
||||
|
||||
java $JAVA_OPTS -jar $executable $args
|
@ -0,0 +1,377 @@
|
||||
package io.swagger.codegen.languages;
|
||||
|
||||
import com.samskivert.mustache.Mustache;
|
||||
import com.samskivert.mustache.Template;
|
||||
import io.swagger.codegen.*;
|
||||
import io.swagger.models.properties.ArrayProperty;
|
||||
import io.swagger.models.properties.MapProperty;
|
||||
import io.swagger.models.properties.Property;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ElixirClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
// source folder where to write the files
|
||||
protected String sourceFolder = "lib";
|
||||
protected String apiVersion = "1.0.0";
|
||||
|
||||
String supportedElixirVersion = "1.4";
|
||||
List<String> extraApplications = Arrays.asList(":logger");
|
||||
List<String> deps = Arrays.asList(
|
||||
"{:tesla, \"~> 0.5.0\"}",
|
||||
"{:poison, \">= 1.0.0\"}"
|
||||
);
|
||||
|
||||
|
||||
public ElixirClientCodegen() {
|
||||
super();
|
||||
|
||||
// set the output folder here
|
||||
outputFolder = "generated-code/elixir";
|
||||
|
||||
/**
|
||||
* Models. You can write model files using the modelTemplateFiles map.
|
||||
* if you want to create one template for file, you can do so here.
|
||||
* for multiple files for model, just put another entry in the `modelTemplateFiles` with
|
||||
* a different extension
|
||||
*/
|
||||
modelTemplateFiles.put(
|
||||
"model.mustache", // the template to use
|
||||
".ex"); // the extension for each file to write
|
||||
|
||||
/**
|
||||
* Api classes. You can write classes for each Api file with the apiTemplateFiles map.
|
||||
* as with models, add multiple entries with different extensions for multiple files per
|
||||
* class
|
||||
*/
|
||||
apiTemplateFiles.put(
|
||||
"api.mustache", // the template to use
|
||||
".ex"); // the extension for each file to write
|
||||
|
||||
/**
|
||||
* Template Location. This is the location which templates will be read from. The generator
|
||||
* will use the resource stream to attempt to read the templates.
|
||||
*/
|
||||
templateDir = "elixir";
|
||||
|
||||
/**
|
||||
* Reserved words. Override this with reserved words specific to your language
|
||||
*/
|
||||
reservedWords = new HashSet<String>(
|
||||
Arrays.asList(
|
||||
"sample1", // replace with static values
|
||||
"sample2")
|
||||
);
|
||||
|
||||
/**
|
||||
* Additional Properties. These values can be passed to the templates and
|
||||
* are available in models, apis, and supporting files
|
||||
*/
|
||||
additionalProperties.put("apiVersion", apiVersion);
|
||||
|
||||
/**
|
||||
* Supporting Files. You can write single files for the generator with the
|
||||
* entire object tree available. If the input file has a suffix of `.mustache
|
||||
* it will be processed by the template engine. Otherwise, it will be copied
|
||||
*/
|
||||
supportingFiles.add(new SupportingFile("README.md.mustache", // the input template or file
|
||||
"", // the destination folder, relative `outputFolder`
|
||||
"README.md") // the output file
|
||||
);
|
||||
supportingFiles.add(new SupportingFile("config.exs.mustache",
|
||||
"config",
|
||||
"config.exs")
|
||||
);
|
||||
supportingFiles.add(new SupportingFile("mix.exs.mustache",
|
||||
"",
|
||||
"mix.exs")
|
||||
);
|
||||
supportingFiles.add(new SupportingFile("test_helper.exs.mustache",
|
||||
"test",
|
||||
"test_helper.exs")
|
||||
);
|
||||
|
||||
/**
|
||||
* Language Specific Primitives. These types will not trigger imports by
|
||||
* the client generator
|
||||
*/
|
||||
languageSpecificPrimitives = new HashSet<String>(
|
||||
Arrays.asList(
|
||||
"Type1", // replace these with your types
|
||||
"Type2")
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the type of generator.
|
||||
*
|
||||
* @return the CodegenType for this generator
|
||||
* @see io.swagger.codegen.CodegenType
|
||||
*/
|
||||
public CodegenType getTag() {
|
||||
return CodegenType.CLIENT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures a friendly name for the generator. This will be used by the generator
|
||||
* to select the library with the -l flag.
|
||||
*
|
||||
* @return the friendly name for the generator
|
||||
*/
|
||||
public String getName() {
|
||||
return "elixir";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns human-friendly help for the generator. Provide the consumer with help
|
||||
* tips, parameters here
|
||||
*
|
||||
* @return A string value for the help message
|
||||
*/
|
||||
public String getHelp() {
|
||||
return "Generates an elixir client library (alpha).";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processOpts() {
|
||||
super.processOpts();
|
||||
additionalProperties.put("supportedElixirVersion", supportedElixirVersion);
|
||||
additionalProperties.put("extraApplications", join(",", extraApplications));
|
||||
additionalProperties.put("deps", deps);
|
||||
additionalProperties.put("underscored", new Mustache.Lambda() {
|
||||
@Override
|
||||
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
|
||||
writer.write(underscored(fragment.execute()));
|
||||
}
|
||||
});
|
||||
additionalProperties.put("modulized", new Mustache.Lambda() {
|
||||
@Override
|
||||
public void execute(Template.Fragment fragment, Writer writer) throws IOException {
|
||||
writer.write(modulized(fragment.execute()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
|
||||
Map<String, Object> operations = (Map<String, Object>) super.postProcessOperations(objs).get("operations");
|
||||
List<CodegenOperation> os = (List<CodegenOperation>) operations.get("operation");
|
||||
List<ExtendedCodegenOperation> newOs = new ArrayList<ExtendedCodegenOperation>();
|
||||
Pattern pattern = Pattern.compile("(.*)\\{([^\\}]+)\\}(.*)");
|
||||
for (CodegenOperation o : os) {
|
||||
ArrayList<String> pathTemplateNames = new ArrayList<String>();
|
||||
Matcher matcher = pattern.matcher(o.path);
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
while (matcher.find()) {
|
||||
String pathTemplateName = matcher.group(2);
|
||||
matcher.appendReplacement(buffer, "$1" + "#{" + underscore(pathTemplateName) + "}" + "$3");
|
||||
pathTemplateNames.add(pathTemplateName);
|
||||
}
|
||||
ExtendedCodegenOperation eco = new ExtendedCodegenOperation(o);
|
||||
if (buffer.toString().isEmpty()) {
|
||||
eco.setReplacedPathName(o.path);
|
||||
} else {
|
||||
eco.setReplacedPathName(buffer.toString());
|
||||
}
|
||||
eco.setPathTemplateNames(pathTemplateNames);
|
||||
newOs.add(eco);
|
||||
}
|
||||
operations.put("operation", newOs);
|
||||
return objs;
|
||||
}
|
||||
|
||||
// We should use String.join if we can use Java8
|
||||
String join(CharSequence charSequence, Iterable<String> iterable) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
for (String str : iterable) {
|
||||
if (0 < buf.length()) {
|
||||
buf.append((charSequence));
|
||||
}
|
||||
buf.append(str);
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
String underscored(String words) {
|
||||
ArrayList<String> underscoredWords = new ArrayList<String>();
|
||||
for (String word : words.split(" ")) {
|
||||
underscoredWords.add(underscore(word));
|
||||
}
|
||||
return join("_", underscoredWords);
|
||||
}
|
||||
|
||||
String modulized(String words) {
|
||||
ArrayList<String> modulizedWords = new ArrayList<String>();
|
||||
for (String word : words.split(" ")) {
|
||||
modulizedWords.add(camelize(word));
|
||||
}
|
||||
return join("", modulizedWords);
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes a reserved word as defined in the `reservedWords` array. Handle escaping
|
||||
* those terms here. This logic is only called if a variable matches the reseved words
|
||||
*
|
||||
* @return the escaped term
|
||||
*/
|
||||
@Override
|
||||
public String escapeReservedWord(String name) {
|
||||
return "_" + name; // add an underscore to the name
|
||||
}
|
||||
|
||||
/**
|
||||
* Location to write model files. You can use the modelPackage() as defined when the class is
|
||||
* instantiated
|
||||
*/
|
||||
public String modelFileFolder() {
|
||||
return outputFolder + "/" + sourceFolder + "/" + underscored((String) additionalProperties.get("appName")) + "/" + "model";
|
||||
}
|
||||
|
||||
/**
|
||||
* Location to write api files. You can use the apiPackage() as defined when the class is
|
||||
* instantiated
|
||||
*/
|
||||
@Override
|
||||
public String apiFileFolder() {
|
||||
return outputFolder + "/" + sourceFolder + "/" + underscored((String) additionalProperties.get("appName")) + "/" + "api";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toApiName(String name) {
|
||||
if (name.length() == 0) {
|
||||
return "Default";
|
||||
}
|
||||
return initialCaps(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toApiFilename(String name) {
|
||||
return snakeCase(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toModelFilename(String name) {
|
||||
return snakeCase(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional - type declaration. This is a String which is used by the templates to instantiate your
|
||||
* types. There is typically special handling for different property types
|
||||
*
|
||||
* @return a string value used as the `dataType` field for model templates, `returnType` for api templates
|
||||
*/
|
||||
@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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional - swagger type conversion. This is used to map swagger types in a `Property` into
|
||||
* either language specific types via `typeMapping` or into complex models if there is not a mapping.
|
||||
*
|
||||
* @return a string value of the type or complex model for this property
|
||||
* @see io.swagger.models.properties.Property
|
||||
*/
|
||||
@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 toModelName(type);
|
||||
} else
|
||||
type = swaggerType;
|
||||
return toModelName(type);
|
||||
}
|
||||
|
||||
class ExtendedCodegenOperation extends CodegenOperation {
|
||||
private List<String> pathTemplateNames = new ArrayList<String>();
|
||||
private String replacedPathName;
|
||||
|
||||
public ExtendedCodegenOperation(CodegenOperation o) {
|
||||
super();
|
||||
|
||||
// Copy all fields of CodegenOperation
|
||||
this.responseHeaders.addAll(o.responseHeaders);
|
||||
this.hasAuthMethods = o.hasAuthMethods;
|
||||
this.hasConsumes = o.hasConsumes;
|
||||
this.hasProduces = o.hasProduces;
|
||||
this.hasParams = o.hasParams;
|
||||
this.hasOptionalParams = o.hasOptionalParams;
|
||||
this.returnTypeIsPrimitive = o.returnTypeIsPrimitive;
|
||||
this.returnSimpleType = o.returnSimpleType;
|
||||
this.subresourceOperation = o.subresourceOperation;
|
||||
this.isMapContainer = o.isMapContainer;
|
||||
this.isListContainer = o.isListContainer;
|
||||
this.isMultipart = o.isMultipart;
|
||||
this.hasMore = o.hasMore;
|
||||
this.isResponseBinary = o.isResponseBinary;
|
||||
this.hasReference = o.hasReference;
|
||||
this.isRestfulIndex = o.isRestfulIndex;
|
||||
this.isRestfulShow = o.isRestfulShow;
|
||||
this.isRestfulCreate = o.isRestfulCreate;
|
||||
this.isRestfulUpdate = o.isRestfulUpdate;
|
||||
this.isRestfulDestroy = o.isRestfulDestroy;
|
||||
this.isRestful = o.isRestful;
|
||||
this.path = o.path;
|
||||
this.operationId = o.operationId;
|
||||
this.returnType = o.returnType;
|
||||
this.httpMethod = o.httpMethod;
|
||||
this.returnBaseType = o.returnBaseType;
|
||||
this.returnContainer = o.returnContainer;
|
||||
this.summary = o.summary;
|
||||
this.unescapedNotes = o.unescapedNotes;
|
||||
this.notes = o.notes;
|
||||
this.baseName = o.baseName;
|
||||
this.defaultResponse = o.defaultResponse;
|
||||
this.discriminator = o.discriminator;
|
||||
this.consumes = o.consumes;
|
||||
this.produces = o.produces;
|
||||
this.bodyParam = o.bodyParam;
|
||||
this.allParams = o.allParams;
|
||||
this.bodyParams = o.bodyParams;
|
||||
this.pathParams = o.pathParams;
|
||||
this.queryParams = o.queryParams;
|
||||
this.headerParams = o.headerParams;
|
||||
this.formParams = o.formParams;
|
||||
this.authMethods = o.authMethods;
|
||||
this.tags = o.tags;
|
||||
this.responses = o.responses;
|
||||
this.imports = o.imports;
|
||||
this.examples = o.examples;
|
||||
this.externalDocs = o.externalDocs;
|
||||
this.vendorExtensions = o.vendorExtensions;
|
||||
this.nickname = o.nickname;
|
||||
this.operationIdLowerCase = o.operationIdLowerCase;
|
||||
}
|
||||
|
||||
public List<String> getPathTemplateNames() {
|
||||
return pathTemplateNames;
|
||||
}
|
||||
|
||||
public void setPathTemplateNames(List<String> pathTemplateNames) {
|
||||
this.pathTemplateNames = pathTemplateNames;
|
||||
}
|
||||
|
||||
public String getReplacedPathName() {
|
||||
return replacedPathName;
|
||||
}
|
||||
|
||||
public void setReplacedPathName(String replacedPathName) {
|
||||
this.replacedPathName = replacedPathName;
|
||||
}
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@ io.swagger.codegen.languages.ConfluenceWikiGenerator
|
||||
io.swagger.codegen.languages.CSharpClientCodegen
|
||||
io.swagger.codegen.languages.CppRestClientCodegen
|
||||
io.swagger.codegen.languages.DartClientCodegen
|
||||
io.swagger.codegen.languages.ElixirClientCodegen
|
||||
io.swagger.codegen.languages.FlashClientCodegen
|
||||
io.swagger.codegen.languages.FlaskConnexionCodegen
|
||||
io.swagger.codegen.languages.GoClientCodegen
|
||||
|
@ -0,0 +1,18 @@
|
||||
# {{#modulized}}{{appName}}{{/modulized}}
|
||||
|
||||
**TODO: Add description**
|
||||
|
||||
## Installation
|
||||
|
||||
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
|
||||
by adding `{{#underscored}}{{appName}}{{/underscored}}` to your list of dependencies in `mix.exs`:
|
||||
|
||||
```elixir
|
||||
def deps do
|
||||
[{:{{#underscored}}{{appName}}{{/underscored}}, "~> 0.1.0"}]
|
||||
end
|
||||
```
|
||||
|
||||
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
|
||||
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
|
||||
be found at [https://hexdocs.pm/{{#underscored}}{{appName}}{{/underscored}}](https://hexdocs.pm/{{#underscored}}{{appName}}{{/underscored}}).
|
@ -0,0 +1,28 @@
|
||||
defmodule {{#modulized}}{{appName}}{{/modulized}}.Api.{{classname}} do
|
||||
@moduledoc """
|
||||
Documentation for {{#modulized}}{{appName}}{{/modulized}}.Api.{{classname}}.
|
||||
"""
|
||||
|
||||
use Tesla
|
||||
|
||||
plug Tesla.Middleware.BaseUrl, "{{basePath}}"
|
||||
plug Tesla.Middleware.JSON
|
||||
{{#operations}}
|
||||
{{#operation}}
|
||||
|
||||
def {{#underscored}}{{operationId}}{{/underscored}}({{#allParams}}{{^-first}}, {{/-first}}{{#underscored}}{{paramName}}{{/underscored}}{{/allParams}}) do
|
||||
method = [method: :{{#underscored}}{{httpMethod}}{{/underscored}}]
|
||||
url = [url: "{{replacedPathName}}"]
|
||||
query_params = [{{^queryParams.isEmpty}}query: [{{#queryParams}}{{^-first}}, {{/-first}}{:"{{paramName}}", {{#underscored}}{{paramName}}{{/underscored}}}{{/queryParams}}]{{/queryParams.isEmpty}}]
|
||||
header_params = [{{^headerParams.isEmpty}}header: [{{#headerParams}}{{^-first}}, {{/-first}}{:"{{paramName}}", {{#underscored}}{{paramName}}{{/underscored}}}{{/headerParams}}]{{/headerParams.isEmpty}}]
|
||||
body_params = [{{^bodyParams.isEmpty}}body: {{#bodyParams}}{{#underscored}}{{paramName}}{{/underscored}}{{/bodyParams}}{{/bodyParams.isEmpty}}]
|
||||
form_params = [{{^formParams.isEmpty}}body: Enum.map_join([{{#formParams}}{{^-first}}, {{/-first}}{:"{{paramName}}", {{#underscored}}{{paramName}}{{/underscored}}}{{/formParams}}], "&", &("#{elem(&1, 0)}=#{elem(&1, 1)}")){{/formParams.isEmpty}}]
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
{{/operation}}
|
||||
{{/operations}}
|
||||
end
|
@ -0,0 +1,30 @@
|
||||
# This file is responsible for configuring your application
|
||||
# and its dependencies with the aid of the Mix.Config module.
|
||||
use Mix.Config
|
||||
|
||||
# This configuration is loaded before any dependency and is restricted
|
||||
# to this project. If another project depends on this project, this
|
||||
# file won't be loaded nor affect the parent project. For this reason,
|
||||
# if you want to provide default values for your application for
|
||||
# 3rd-party users, it should be done in your "mix.exs" file.
|
||||
|
||||
# You can configure for your application as:
|
||||
#
|
||||
# config :{{#underscored}}{{appName}}{{/underscored}}, key: :value
|
||||
#
|
||||
# And access this configuration in your application as:
|
||||
#
|
||||
# Application.get_env(:{{#underscored}}{{appName}}{{/underscored}}, :key)
|
||||
#
|
||||
# Or configure a 3rd-party app:
|
||||
#
|
||||
# config :logger, level: :info
|
||||
#
|
||||
|
||||
# It is also possible to import configuration files, relative to this
|
||||
# directory. For example, you can emulate configuration per environment
|
||||
# by uncommenting the line below and defining dev.exs, test.exs and such.
|
||||
# Configuration from the imported file will override the ones defined
|
||||
# here (which is why it is important to import them last).
|
||||
#
|
||||
# import_config "#{Mix.env}.exs"
|
@ -0,0 +1,37 @@
|
||||
defmodule {{#modulized}}{{appName}}{{/modulized}}.Mixfile do
|
||||
use Mix.Project
|
||||
|
||||
def project do
|
||||
[app: :{{#underscored}}{{appName}}{{/underscored}},
|
||||
version: "0.1.0",
|
||||
elixir: "~> {{supportedElixirVersion}}",
|
||||
build_embedded: Mix.env == :prod,
|
||||
start_permanent: Mix.env == :prod,
|
||||
deps: deps()]
|
||||
end
|
||||
|
||||
# Configuration for the OTP application
|
||||
#
|
||||
# Type "mix help compile.app" for more information
|
||||
def application do
|
||||
# Specify extra applications you'll use from Erlang/Elixir
|
||||
[extra_applications: [{{extraApplications}}]]
|
||||
end
|
||||
|
||||
# Dependencies can be Hex packages:
|
||||
#
|
||||
# {:my_dep, "~> 0.3.0"}
|
||||
#
|
||||
# Or git/path repositories:
|
||||
#
|
||||
# {:my_dep, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
|
||||
#
|
||||
# Type "mix help deps" for more examples and options
|
||||
defp deps do
|
||||
[
|
||||
{{#deps}}
|
||||
{{{this}}}{{^-last}},{{/-last}}
|
||||
{{/deps}}
|
||||
]
|
||||
end
|
||||
end
|
@ -0,0 +1 @@
|
||||
ExUnit.start()
|
@ -0,0 +1,31 @@
|
||||
package io.swagger.codegen.elixir;
|
||||
|
||||
import io.swagger.codegen.AbstractOptionsTest;
|
||||
import io.swagger.codegen.CodegenConfig;
|
||||
import io.swagger.codegen.languages.ElixirClientCodegen;
|
||||
import io.swagger.codegen.options.ElixirClientOptionsProvider;
|
||||
import mockit.Expectations;
|
||||
import mockit.Tested;
|
||||
|
||||
public class ElixirClientOptionsTest extends AbstractOptionsTest {
|
||||
|
||||
@Tested
|
||||
private ElixirClientCodegen clientCodegen;
|
||||
|
||||
public ElixirClientOptionsTest() {
|
||||
super(new ElixirClientOptionsProvider());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CodegenConfig getCodegenConfig() {
|
||||
return clientCodegen;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Override
|
||||
protected void setExpectations() {
|
||||
new Expectations(clientCodegen) {{
|
||||
// TODO
|
||||
}};
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package io.swagger.codegen.options;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import io.swagger.codegen.CodegenConstants;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class ElixirClientOptionsProvider implements OptionsProvider {
|
||||
|
||||
@Override
|
||||
public String getLanguage() {
|
||||
return "elixir";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> createOptions() {
|
||||
ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<String, String>();
|
||||
return builder
|
||||
.put(CodegenConstants.SORT_PARAMS_BY_REQUIRED_FLAG, "false")
|
||||
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, "false")
|
||||
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, "false")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isServer() {
|
||||
return false;
|
||||
}
|
||||
}
|
23
samples/client/petstore/elixir/.swagger-codegen-ignore
Normal file
23
samples/client/petstore/elixir/.swagger-codegen-ignore
Normal file
@ -0,0 +1,23 @@
|
||||
# Swagger Codegen Ignore
|
||||
# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen
|
||||
|
||||
# Use this file to prevent files from being overwritten by the generator.
|
||||
# The patterns follow closely to .gitignore or .dockerignore.
|
||||
|
||||
# As an example, the C# client generator defines ApiClient.cs.
|
||||
# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line:
|
||||
#ApiClient.cs
|
||||
|
||||
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
|
||||
#foo/*/qux
|
||||
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
|
||||
|
||||
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
|
||||
#foo/**/qux
|
||||
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
|
||||
|
||||
# You can also negate patterns with an exclamation (!).
|
||||
# For example, you can ignore all files in a docs folder with the file extension .md:
|
||||
#docs/*.md
|
||||
# Then explicitly reverse the ignore rule for a single file:
|
||||
#!docs/README.md
|
18
samples/client/petstore/elixir/README.md
Normal file
18
samples/client/petstore/elixir/README.md
Normal file
@ -0,0 +1,18 @@
|
||||
# SwaggerPetstore
|
||||
|
||||
**TODO: Add description**
|
||||
|
||||
## Installation
|
||||
|
||||
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
|
||||
by adding `swagger_petstore` to your list of dependencies in `mix.exs`:
|
||||
|
||||
```elixir
|
||||
def deps do
|
||||
[{:swagger_petstore, "~> 0.1.0"}]
|
||||
end
|
||||
```
|
||||
|
||||
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
|
||||
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
|
||||
be found at [https://hexdocs.pm/swagger_petstore](https://hexdocs.pm/swagger_petstore).
|
30
samples/client/petstore/elixir/config/config.exs
Normal file
30
samples/client/petstore/elixir/config/config.exs
Normal file
@ -0,0 +1,30 @@
|
||||
# This file is responsible for configuring your application
|
||||
# and its dependencies with the aid of the Mix.Config module.
|
||||
use Mix.Config
|
||||
|
||||
# This configuration is loaded before any dependency and is restricted
|
||||
# to this project. If another project depends on this project, this
|
||||
# file won't be loaded nor affect the parent project. For this reason,
|
||||
# if you want to provide default values for your application for
|
||||
# 3rd-party users, it should be done in your "mix.exs" file.
|
||||
|
||||
# You can configure for your application as:
|
||||
#
|
||||
# config :swagger_petstore, key: :value
|
||||
#
|
||||
# And access this configuration in your application as:
|
||||
#
|
||||
# Application.get_env(:swagger_petstore, :key)
|
||||
#
|
||||
# Or configure a 3rd-party app:
|
||||
#
|
||||
# config :logger, level: :info
|
||||
#
|
||||
|
||||
# It is also possible to import configuration files, relative to this
|
||||
# directory. For example, you can emulate configuration per environment
|
||||
# by uncommenting the line below and defining dev.exs, test.exs and such.
|
||||
# Configuration from the imported file will override the ones defined
|
||||
# here (which is why it is important to import them last).
|
||||
#
|
||||
# import_config "#{Mix.env}.exs"
|
@ -0,0 +1,52 @@
|
||||
defmodule SwaggerPetstore.Api.Fake do
|
||||
@moduledoc """
|
||||
Documentation for SwaggerPetstore.Api.Fake.
|
||||
"""
|
||||
|
||||
use Tesla
|
||||
|
||||
plug Tesla.Middleware.BaseUrl, "http://petstore.swagger.io/v2"
|
||||
plug Tesla.Middleware.JSON
|
||||
|
||||
def test_client_model(body) do
|
||||
method = [method: :patch]
|
||||
url = [url: "/fake"]
|
||||
query_params = []
|
||||
header_params = []
|
||||
body_params = [body: body]
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def test_endpoint_parameters(number, double, pattern_without_delimiter, byte, integer, int32, int64, float, string, binary, date, date_time, password, callback) do
|
||||
method = [method: :post]
|
||||
url = [url: "/fake"]
|
||||
query_params = []
|
||||
header_params = []
|
||||
body_params = []
|
||||
form_params = [body: Enum.map_join([{:"integer", integer}, {:"int32", int32}, {:"int64", int64}, {:"number", number}, {:"float", float}, {:"double", double}, {:"string", string}, {:"patternWithoutDelimiter", pattern_without_delimiter}, {:"byte", byte}, {:"binary", binary}, {:"date", date}, {:"dateTime", date_time}, {:"password", password}, {:"callback", callback}], "&", &("#{elem(&1, 0)}=#{elem(&1, 1)}"))]
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def test_enum_parameters(enum_form_string_array, enum_form_string, enum_header_string_array, enum_header_string, enum_query_string_array, enum_query_string, enum_query_integer, enum_query_double) do
|
||||
method = [method: :get]
|
||||
url = [url: "/fake"]
|
||||
query_params = [query: [{:"enumQueryStringArray", enum_query_string_array}, {:"enumQueryString", enum_query_string}, {:"enumQueryInteger", enum_query_integer}]]
|
||||
header_params = [header: [{:"enumHeaderStringArray", enum_header_string_array}, {:"enumHeaderString", enum_header_string}]]
|
||||
body_params = []
|
||||
form_params = [body: Enum.map_join([{:"enumFormStringArray", enum_form_string_array}, {:"enumFormString", enum_form_string}, {:"enumQueryDouble", enum_query_double}], "&", &("#{elem(&1, 0)}=#{elem(&1, 1)}"))]
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
end
|
122
samples/client/petstore/elixir/lib/swagger_petstore/api/pet.ex
Normal file
122
samples/client/petstore/elixir/lib/swagger_petstore/api/pet.ex
Normal file
@ -0,0 +1,122 @@
|
||||
defmodule SwaggerPetstore.Api.Pet do
|
||||
@moduledoc """
|
||||
Documentation for SwaggerPetstore.Api.Pet.
|
||||
"""
|
||||
|
||||
use Tesla
|
||||
|
||||
plug Tesla.Middleware.BaseUrl, "http://petstore.swagger.io/v2"
|
||||
plug Tesla.Middleware.JSON
|
||||
|
||||
def add_pet(body) do
|
||||
method = [method: :post]
|
||||
url = [url: "/pet"]
|
||||
query_params = []
|
||||
header_params = []
|
||||
body_params = [body: body]
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def delete_pet(pet_id, api_key) do
|
||||
method = [method: :delete]
|
||||
url = [url: "/pet/#{pet_id}"]
|
||||
query_params = []
|
||||
header_params = [header: [{:"apiKey", api_key}]]
|
||||
body_params = []
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def find_pets_by_status(status) do
|
||||
method = [method: :get]
|
||||
url = [url: "/pet/findByStatus"]
|
||||
query_params = [query: [{:"status", status}]]
|
||||
header_params = []
|
||||
body_params = []
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def find_pets_by_tags(tags) do
|
||||
method = [method: :get]
|
||||
url = [url: "/pet/findByTags"]
|
||||
query_params = [query: [{:"tags", tags}]]
|
||||
header_params = []
|
||||
body_params = []
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def get_pet_by_id(pet_id) do
|
||||
method = [method: :get]
|
||||
url = [url: "/pet/#{pet_id}"]
|
||||
query_params = []
|
||||
header_params = []
|
||||
body_params = []
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def update_pet(body) do
|
||||
method = [method: :put]
|
||||
url = [url: "/pet"]
|
||||
query_params = []
|
||||
header_params = []
|
||||
body_params = [body: body]
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def update_pet_with_form(pet_id, name, status) do
|
||||
method = [method: :post]
|
||||
url = [url: "/pet/#{pet_id}"]
|
||||
query_params = []
|
||||
header_params = []
|
||||
body_params = []
|
||||
form_params = [body: Enum.map_join([{:"name", name}, {:"status", status}], "&", &("#{elem(&1, 0)}=#{elem(&1, 1)}"))]
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def upload_file(pet_id, additional_metadata, file) do
|
||||
method = [method: :post]
|
||||
url = [url: "/pet/#{pet_id}/uploadImage"]
|
||||
query_params = []
|
||||
header_params = []
|
||||
body_params = []
|
||||
form_params = [body: Enum.map_join([{:"additionalMetadata", additional_metadata}, {:"file", file}], "&", &("#{elem(&1, 0)}=#{elem(&1, 1)}"))]
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
end
|
@ -0,0 +1,66 @@
|
||||
defmodule SwaggerPetstore.Api.Store do
|
||||
@moduledoc """
|
||||
Documentation for SwaggerPetstore.Api.Store.
|
||||
"""
|
||||
|
||||
use Tesla
|
||||
|
||||
plug Tesla.Middleware.BaseUrl, "http://petstore.swagger.io/v2"
|
||||
plug Tesla.Middleware.JSON
|
||||
|
||||
def delete_order(order_id) do
|
||||
method = [method: :delete]
|
||||
url = [url: "/store/order/#{order_id}"]
|
||||
query_params = []
|
||||
header_params = []
|
||||
body_params = []
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def get_inventory() do
|
||||
method = [method: :get]
|
||||
url = [url: "/store/inventory"]
|
||||
query_params = []
|
||||
header_params = []
|
||||
body_params = []
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def get_order_by_id(order_id) do
|
||||
method = [method: :get]
|
||||
url = [url: "/store/order/#{order_id}"]
|
||||
query_params = []
|
||||
header_params = []
|
||||
body_params = []
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def place_order(body) do
|
||||
method = [method: :post]
|
||||
url = [url: "/store/order"]
|
||||
query_params = []
|
||||
header_params = []
|
||||
body_params = [body: body]
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
end
|
122
samples/client/petstore/elixir/lib/swagger_petstore/api/user.ex
Normal file
122
samples/client/petstore/elixir/lib/swagger_petstore/api/user.ex
Normal file
@ -0,0 +1,122 @@
|
||||
defmodule SwaggerPetstore.Api.User do
|
||||
@moduledoc """
|
||||
Documentation for SwaggerPetstore.Api.User.
|
||||
"""
|
||||
|
||||
use Tesla
|
||||
|
||||
plug Tesla.Middleware.BaseUrl, "http://petstore.swagger.io/v2"
|
||||
plug Tesla.Middleware.JSON
|
||||
|
||||
def create_user(body) do
|
||||
method = [method: :post]
|
||||
url = [url: "/user"]
|
||||
query_params = []
|
||||
header_params = []
|
||||
body_params = [body: body]
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def create_users_with_array_input(body) do
|
||||
method = [method: :post]
|
||||
url = [url: "/user/createWithArray"]
|
||||
query_params = []
|
||||
header_params = []
|
||||
body_params = [body: body]
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def create_users_with_list_input(body) do
|
||||
method = [method: :post]
|
||||
url = [url: "/user/createWithList"]
|
||||
query_params = []
|
||||
header_params = []
|
||||
body_params = [body: body]
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def delete_user(username) do
|
||||
method = [method: :delete]
|
||||
url = [url: "/user/#{username}"]
|
||||
query_params = []
|
||||
header_params = []
|
||||
body_params = []
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def get_user_by_name(username) do
|
||||
method = [method: :get]
|
||||
url = [url: "/user/#{username}"]
|
||||
query_params = []
|
||||
header_params = []
|
||||
body_params = []
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def login_user(username, password) do
|
||||
method = [method: :get]
|
||||
url = [url: "/user/login"]
|
||||
query_params = [query: [{:"username", username}, {:"password", password}]]
|
||||
header_params = []
|
||||
body_params = []
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def logout_user() do
|
||||
method = [method: :get]
|
||||
url = [url: "/user/logout"]
|
||||
query_params = []
|
||||
header_params = []
|
||||
body_params = []
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
|
||||
def update_user(username, body) do
|
||||
method = [method: :put]
|
||||
url = [url: "/user/#{username}"]
|
||||
query_params = []
|
||||
header_params = []
|
||||
body_params = [body: body]
|
||||
form_params = []
|
||||
params = query_params ++ header_params ++ body_params ++ form_params
|
||||
opts = []
|
||||
options = method ++ url ++ params ++ opts
|
||||
|
||||
request(options)
|
||||
end
|
||||
end
|
36
samples/client/petstore/elixir/mix.exs
Normal file
36
samples/client/petstore/elixir/mix.exs
Normal file
@ -0,0 +1,36 @@
|
||||
defmodule SwaggerPetstore.Mixfile do
|
||||
use Mix.Project
|
||||
|
||||
def project do
|
||||
[app: :swagger_petstore,
|
||||
version: "0.1.0",
|
||||
elixir: "~> 1.4",
|
||||
build_embedded: Mix.env == :prod,
|
||||
start_permanent: Mix.env == :prod,
|
||||
deps: deps()]
|
||||
end
|
||||
|
||||
# Configuration for the OTP application
|
||||
#
|
||||
# Type "mix help compile.app" for more information
|
||||
def application do
|
||||
# Specify extra applications you'll use from Erlang/Elixir
|
||||
[extra_applications: [:logger]]
|
||||
end
|
||||
|
||||
# Dependencies can be Hex packages:
|
||||
#
|
||||
# {:my_dep, "~> 0.3.0"}
|
||||
#
|
||||
# Or git/path repositories:
|
||||
#
|
||||
# {:my_dep, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
|
||||
#
|
||||
# Type "mix help deps" for more examples and options
|
||||
defp deps do
|
||||
[
|
||||
{:tesla, "~> 0.5.0"},
|
||||
{:poison, ">= 1.0.0"}
|
||||
]
|
||||
end
|
||||
end
|
1
samples/client/petstore/elixir/test/test_helper.exs
Normal file
1
samples/client/petstore/elixir/test/test_helper.exs
Normal file
@ -0,0 +1 @@
|
||||
ExUnit.start()
|
Loading…
x
Reference in New Issue
Block a user