refactor: expose generated code and client business logic to share with Kapacitor (#103)
* refactor: take clients out of internal * refactor: move stdio to pkg * Move internal/api to api * refactor: final changes for Kapacitor to access shared functionality * chore: regenerate mocks * fix: bad automated refactor * chore: extra formatting not caught by make fmt
This commit is contained in:
35
api/templates/README.md
Normal file
35
api/templates/README.md
Normal file
@ -0,0 +1,35 @@
|
||||
# Custom OpenAPI Templates
|
||||
|
||||
This directory contains custom mustache templates used by the OpenAPI code generator.
|
||||
The original templates were extracted by running:
|
||||
```shell
|
||||
openapi-generator author template -g go
|
||||
```
|
||||
NOTE: This command extracts a copy of every template used by the generator, but we only
|
||||
track templates that we've modified here. The generator can handle sourcing templates from
|
||||
multiple locations.
|
||||
|
||||
## What have we changed?
|
||||
|
||||
`api.mustache`
|
||||
* Add `GetX()` methods for each request parameter `X`, for use in unit tests
|
||||
* Add checks for `isByteArray` to generate `[]byte` request fields instead of `*string`
|
||||
* Update creation of `GenericOpenAPIError` to track sub-error models by reference
|
||||
* Add checks for `isResponseBinary` to directly return the response-body-reader, instead of
|
||||
pulling the entire body into memory and transforming it into an `*os.File`
|
||||
* GUnzip response bodies when `Content-Encoding: gzip` is set
|
||||
* Remove `*http.Response`s from the return values of generated operations
|
||||
|
||||
`client.mustache`
|
||||
* Removed use of `golang.org/x/oauth2` to avoid its heavy dependencies
|
||||
* Fixed error strings to be idiomatic according to staticcheck (lowercase, no punctuation)
|
||||
* Use `strings.EqualFold` instead of comparing two `strings.ToLower` calls
|
||||
* GZip request bodies when `Content-Encoding: gzip` is set
|
||||
* Update the `GenericOpenAPIError` type to enforce that error response models implement the `error` interface
|
||||
|
||||
`configuration.mustache`
|
||||
* Deleted `ContextOAuth2` key to match modification in client
|
||||
* Fixed error strings to be idiomatic according to staticcheck (lowercase, no punctuation)
|
||||
|
||||
`model_oneof.mustache`
|
||||
* Fixed error strings to be idiomatic according to staticcheck (lowercase, no punctuation)
|
424
api/templates/api.mustache
Normal file
424
api/templates/api.mustache
Normal file
@ -0,0 +1,424 @@
|
||||
{{>partial_header}}
|
||||
package {{packageName}}
|
||||
|
||||
{{#operations}}
|
||||
import (
|
||||
"bytes"
|
||||
_gzip "compress/gzip"
|
||||
_context "context"
|
||||
_io "io"
|
||||
_ioutil "io/ioutil"
|
||||
_nethttp "net/http"
|
||||
_neturl "net/url"
|
||||
{{#imports}} "{{import}}"
|
||||
{{/imports}}
|
||||
)
|
||||
|
||||
// Linger please
|
||||
var (
|
||||
_ _context.Context
|
||||
)
|
||||
{{#generateInterfaces}}
|
||||
|
||||
type {{classname}} interface {
|
||||
{{#operation}}
|
||||
|
||||
/*
|
||||
* {{operationId}}{{#summary}} {{{.}}}{{/summary}}{{^summary}} Method for {{operationId}}{{/summary}}
|
||||
{{#notes}}
|
||||
* {{{unescapedNotes}}}
|
||||
{{/notes}}
|
||||
* @param ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{{#pathParams}}
|
||||
* @param {{paramName}}{{#description}} {{{.}}}{{/description}}{{/pathParams}}
|
||||
* @return {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request
|
||||
*/
|
||||
{{{nickname}}}(ctx _context.Context{{#pathParams}}, {{paramName}} {{{dataType}}}{{/pathParams}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request
|
||||
|
||||
/*
|
||||
* {{nickname}}Execute executes the request{{#returnType}}
|
||||
* @return {{{.}}}{{/returnType}}
|
||||
*/
|
||||
{{nickname}}Execute(r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) ({{#returnType}}{{#isResponseBinary}}_io.ReadCloser{{/isResponseBinary}}{{^isResponseBinary}}{{{returnType}}}{{/isResponseBinary}}, {{/returnType}}error)
|
||||
{{/operation}}
|
||||
}
|
||||
{{/generateInterfaces}}
|
||||
|
||||
// {{#lambda.camelcase}}{{classname}}{{/lambda.camelcase}}GzipReadCloser supports streaming gzip response-bodies directly from the server.
|
||||
type {{#lambda.camelcase}}{{classname}}{{/lambda.camelcase}}GzipReadCloser struct {
|
||||
underlying _io.ReadCloser
|
||||
gzip _io.ReadCloser
|
||||
}
|
||||
func (gzrc *{{#lambda.camelcase}}{{classname}}{{/lambda.camelcase}}GzipReadCloser) Read(p []byte) (int, error) {
|
||||
return gzrc.gzip.Read(p)
|
||||
}
|
||||
func (gzrc *{{#lambda.camelcase}}{{classname}}{{/lambda.camelcase}}GzipReadCloser) Close() error {
|
||||
if err := gzrc.gzip.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return gzrc.underlying.Close()
|
||||
}
|
||||
|
||||
// {{classname}}Service {{classname}} service
|
||||
type {{classname}}Service service
|
||||
{{#operation}}
|
||||
|
||||
type {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request struct {
|
||||
ctx _context.Context{{#generateInterfaces}}
|
||||
ApiService {{classname}}
|
||||
{{/generateInterfaces}}{{^generateInterfaces}}
|
||||
ApiService *{{classname}}Service
|
||||
{{/generateInterfaces}}
|
||||
{{#allParams}}
|
||||
{{paramName}} {{#isByteArray}}[]byte{{/isByteArray}}{{^isByteArray}}{{^isPathParam}}*{{/isPathParam}}{{{dataType}}}{{/isByteArray}}
|
||||
{{/allParams}}
|
||||
}
|
||||
{{#allParams}}
|
||||
func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) {{vendorExtensions.x-export-param-name}}({{paramName}} {{#isByteArray}}[]byte{{/isByteArray}}{{^isByteArray}}{{{dataType}}}{{/isByteArray}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request {
|
||||
r.{{paramName}} = {{^isByteArray}}{{^isPathParam}}&{{/isPathParam}}{{/isByteArray}}{{paramName}}
|
||||
return r
|
||||
}
|
||||
func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) Get{{vendorExtensions.x-export-param-name}}() {{#isByteArray}}[]byte{{/isByteArray}}{{^isByteArray}}{{^isPathParam}}*{{/isPathParam}}{{{dataType}}}{{/isByteArray}} {
|
||||
return r.{{paramName}}
|
||||
}
|
||||
|
||||
{{/allParams}}
|
||||
|
||||
func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) Execute() ({{#returnType}}{{#isResponseBinary}}_io.ReadCloser{{/isResponseBinary}}{{^isResponseBinary}}{{{returnType}}}{{/isResponseBinary}}, {{/returnType}}error) {
|
||||
return r.ApiService.{{nickname}}Execute(r)
|
||||
}
|
||||
|
||||
/*
|
||||
* {{operationId}}{{#summary}} {{{.}}}{{/summary}}{{^summary}} Method for {{operationId}}{{/summary}}
|
||||
{{#notes}}
|
||||
* {{{unescapedNotes}}}
|
||||
{{/notes}}
|
||||
* @param ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{{#pathParams}}
|
||||
* @param {{paramName}}{{#description}} {{{.}}}{{/description}}{{/pathParams}}
|
||||
* @return {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request
|
||||
*/
|
||||
func (a *{{{classname}}}Service) {{{nickname}}}(ctx _context.Context{{#pathParams}}, {{paramName}} {{{dataType}}}{{/pathParams}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request {
|
||||
return {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request{
|
||||
ApiService: a,
|
||||
ctx: ctx,
|
||||
{{#pathParams}}
|
||||
{{paramName}}: {{paramName}},
|
||||
{{/pathParams}}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Execute executes the request{{#returnType}}
|
||||
* @return {{{.}}}{{/returnType}}
|
||||
*/
|
||||
func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) ({{#returnType}}{{#isResponseBinary}}_io.ReadCloser{{/isResponseBinary}}{{^isResponseBinary}}{{{.}}}{{/isResponseBinary}}, {{/returnType}}error) {
|
||||
var (
|
||||
localVarHTTPMethod = _nethttp.Method{{httpMethod}}
|
||||
localVarPostBody interface{}
|
||||
localVarFormFileName string
|
||||
localVarFileName string
|
||||
localVarFileBytes []byte
|
||||
{{#returnType}}
|
||||
{{#isResponseBinary}}
|
||||
localVarReturnValue _io.ReadCloser
|
||||
{{/isResponseBinary}}
|
||||
{{^isResponseBinary}}
|
||||
localVarReturnValue {{{returnType}}}
|
||||
{{/isResponseBinary}}
|
||||
{{/returnType}}
|
||||
)
|
||||
|
||||
localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "{{{classname}}}Service.{{{nickname}}}")
|
||||
if err != nil {
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}GenericOpenAPIError{error: err.Error()}
|
||||
}
|
||||
|
||||
localVarPath := localBasePath + "{{{path}}}"{{#pathParams}}
|
||||
localVarPath = strings.Replace(localVarPath, "{"+"{{baseName}}"+"}", _neturl.PathEscape(parameterToString(r.{{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}")), -1){{/pathParams}}
|
||||
|
||||
localVarHeaderParams := make(map[string]string)
|
||||
localVarQueryParams := _neturl.Values{}
|
||||
localVarFormParams := _neturl.Values{}
|
||||
{{#allParams}}
|
||||
{{#required}}
|
||||
{{^isPathParam}}
|
||||
if r.{{paramName}} == nil {
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}reportError("{{paramName}} is required and must be specified")
|
||||
}
|
||||
{{/isPathParam}}
|
||||
{{#minItems}}
|
||||
if len({{^isPathParam}}*{{/isPathParam}}r.{{paramName}}) < {{minItems}} {
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}reportError("{{paramName}} must have at least {{minItems}} elements")
|
||||
}
|
||||
{{/minItems}}
|
||||
{{#maxItems}}
|
||||
if len({{^isPathParam}}*{{/isPathParam}}r.{{paramName}}) > {{maxItems}} {
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}reportError("{{paramName}} must have less than {{maxItems}} elements")
|
||||
}
|
||||
{{/maxItems}}
|
||||
{{#minLength}}
|
||||
if strlen({{^isPathParam}}*{{/isPathParam}}r.{{paramName}}) < {{minLength}} {
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}reportError("{{paramName}} must have at least {{minLength}} elements")
|
||||
}
|
||||
{{/minLength}}
|
||||
{{#maxLength}}
|
||||
if strlen({{^isPathParam}}*{{/isPathParam}}r.{{paramName}}) > {{maxLength}} {
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}reportError("{{paramName}} must have less than {{maxLength}} elements")
|
||||
}
|
||||
{{/maxLength}}
|
||||
{{#minimum}}
|
||||
{{#isString}}
|
||||
{{paramName}}Txt, err := atoi({{^isPathParam}}*{{/isPathParam}}r.{{paramName}})
|
||||
if {{paramName}}Txt < {{minimum}} {
|
||||
{{/isString}}
|
||||
{{^isString}}
|
||||
if {{^isPathParam}}*{{/isPathParam}}r.{{paramName}} < {{minimum}} {
|
||||
{{/isString}}
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}reportError("{{paramName}} must be greater than {{minimum}}")
|
||||
}
|
||||
{{/minimum}}
|
||||
{{#maximum}}
|
||||
{{#isString}}
|
||||
{{paramName}}Txt, err := atoi({{^isPathParam}}*{{/isPathParam}}r.{{paramName}})
|
||||
if {{paramName}}Txt > {{maximum}} {
|
||||
{{/isString}}
|
||||
{{^isString}}
|
||||
if {{^isPathParam}}*{{/isPathParam}}r.{{paramName}} > {{maximum}} {
|
||||
{{/isString}}
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}reportError("{{paramName}} must be less than {{maximum}}")
|
||||
}
|
||||
{{/maximum}}
|
||||
{{/required}}
|
||||
{{/allParams}}
|
||||
|
||||
{{#queryParams}}
|
||||
{{#required}}
|
||||
{{#isCollectionFormatMulti}}
|
||||
{
|
||||
t := *r.{{paramName}}
|
||||
if reflect.TypeOf(t).Kind() == reflect.Slice {
|
||||
s := reflect.ValueOf(t)
|
||||
for i := 0; i < s.Len(); i++ {
|
||||
localVarQueryParams.Add("{{baseName}}", parameterToString(s.Index(i), "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
|
||||
}
|
||||
} else {
|
||||
localVarQueryParams.Add("{{baseName}}", parameterToString(t, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
|
||||
}
|
||||
}
|
||||
{{/isCollectionFormatMulti}}
|
||||
{{^isCollectionFormatMulti}}
|
||||
localVarQueryParams.Add("{{baseName}}", parameterToString(*r.{{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
|
||||
{{/isCollectionFormatMulti}}
|
||||
{{/required}}
|
||||
{{^required}}
|
||||
if r.{{paramName}} != nil {
|
||||
{{#isCollectionFormatMulti}}
|
||||
t := *r.{{paramName}}
|
||||
if reflect.TypeOf(t).Kind() == reflect.Slice {
|
||||
s := reflect.ValueOf(t)
|
||||
for i := 0; i < s.Len(); i++ {
|
||||
localVarQueryParams.Add("{{baseName}}", parameterToString(s.Index(i), "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
|
||||
}
|
||||
} else {
|
||||
localVarQueryParams.Add("{{baseName}}", parameterToString(t, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
|
||||
}
|
||||
{{/isCollectionFormatMulti}}
|
||||
{{^isCollectionFormatMulti}}
|
||||
localVarQueryParams.Add("{{baseName}}", parameterToString(*r.{{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
|
||||
{{/isCollectionFormatMulti}}
|
||||
}
|
||||
{{/required}}
|
||||
{{/queryParams}}
|
||||
// to determine the Content-Type header
|
||||
{{=<% %>=}}
|
||||
localVarHTTPContentTypes := []string{<%#consumes%>"<%&mediaType%>"<%^-last%>, <%/-last%><%/consumes%>}
|
||||
<%={{ }}=%>
|
||||
|
||||
// set Content-Type header
|
||||
localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes)
|
||||
if localVarHTTPContentType != "" {
|
||||
localVarHeaderParams["Content-Type"] = localVarHTTPContentType
|
||||
}
|
||||
|
||||
// to determine the Accept header
|
||||
{{=<% %>=}}
|
||||
localVarHTTPHeaderAccepts := []string{<%#produces%>"<%&mediaType%>"<%^-last%>, <%/-last%><%/produces%>}
|
||||
<%={{ }}=%>
|
||||
|
||||
// set Accept header
|
||||
localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
|
||||
if localVarHTTPHeaderAccept != "" {
|
||||
localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
|
||||
}
|
||||
{{#headerParams}}
|
||||
{{#required}}
|
||||
localVarHeaderParams["{{baseName}}"] = parameterToString(*r.{{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}")
|
||||
{{/required}}
|
||||
{{^required}}
|
||||
if r.{{paramName}} != nil {
|
||||
localVarHeaderParams["{{baseName}}"] = parameterToString(*r.{{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}")
|
||||
}
|
||||
{{/required}}
|
||||
{{/headerParams}}
|
||||
{{#formParams}}
|
||||
{{#isFile}}
|
||||
localVarFormFileName = "{{baseName}}"
|
||||
{{#required}}
|
||||
localVarFile := *r.{{paramName}}
|
||||
{{/required}}
|
||||
{{^required}}
|
||||
var localVarFile {{dataType}}
|
||||
if r.{{paramName}} != nil {
|
||||
localVarFile = *r.{{paramName}}
|
||||
}
|
||||
{{/required}}
|
||||
if localVarFile != nil {
|
||||
fbs, _ := _ioutil.ReadAll(localVarFile)
|
||||
localVarFileBytes = fbs
|
||||
localVarFileName = localVarFile.Name()
|
||||
localVarFile.Close()
|
||||
}
|
||||
{{/isFile}}
|
||||
{{^isFile}}
|
||||
{{#required}}
|
||||
localVarFormParams.Add("{{baseName}}", parameterToString(*r.{{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
|
||||
{{/required}}
|
||||
{{^required}}
|
||||
{{#isModel}}
|
||||
if r.{{paramName}} != nil {
|
||||
paramJson, err := parameterToJson(*r.{{paramName}})
|
||||
if err != nil {
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}err
|
||||
}
|
||||
localVarFormParams.Add("{{baseName}}", paramJson)
|
||||
}
|
||||
{{/isModel}}
|
||||
{{^isModel}}
|
||||
if r.{{paramName}} != nil {
|
||||
localVarFormParams.Add("{{baseName}}", parameterToString(*r.{{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
|
||||
}
|
||||
{{/isModel}}
|
||||
{{/required}}
|
||||
{{/isFile}}
|
||||
{{/formParams}}
|
||||
{{#bodyParams}}
|
||||
// body params
|
||||
localVarPostBody = r.{{paramName}}
|
||||
{{/bodyParams}}
|
||||
{{#authMethods}}
|
||||
{{#isApiKey}}
|
||||
{{^isKeyInCookie}}
|
||||
if r.ctx != nil {
|
||||
// API Key Authentication
|
||||
if auth, ok := r.ctx.Value(ContextAPIKeys).(map[string]APIKey); ok {
|
||||
{{#vendorExtensions.x-auth-id-alias}}
|
||||
if apiKey, ok := auth["{{.}}"]; ok {
|
||||
var key string
|
||||
if prefix, ok := auth["{{name}}"]; ok && prefix.Prefix != "" {
|
||||
key = prefix.Prefix + " " + apiKey.Key
|
||||
} else {
|
||||
key = apiKey.Key
|
||||
}
|
||||
{{/vendorExtensions.x-auth-id-alias}}
|
||||
{{^vendorExtensions.x-auth-id-alias}}
|
||||
if apiKey, ok := auth["{{name}}"]; ok {
|
||||
var key string
|
||||
if apiKey.Prefix != "" {
|
||||
key = apiKey.Prefix + " " + apiKey.Key
|
||||
} else {
|
||||
key = apiKey.Key
|
||||
}
|
||||
{{/vendorExtensions.x-auth-id-alias}}
|
||||
{{#isKeyInHeader}}
|
||||
localVarHeaderParams["{{keyParamName}}"] = key
|
||||
{{/isKeyInHeader}}
|
||||
{{#isKeyInQuery}}
|
||||
localVarQueryParams.Add("{{keyParamName}}", key)
|
||||
{{/isKeyInQuery}}
|
||||
}
|
||||
}
|
||||
}
|
||||
{{/isKeyInCookie}}
|
||||
{{/isApiKey}}
|
||||
{{/authMethods}}
|
||||
req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFileName, localVarFileName, localVarFileBytes)
|
||||
if err != nil {
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}err
|
||||
}
|
||||
|
||||
localVarHTTPResponse, err := a.client.callAPI(req)
|
||||
if err != nil || localVarHTTPResponse == nil {
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}err
|
||||
}
|
||||
|
||||
var body _io.ReadCloser = localVarHTTPResponse.Body
|
||||
if localVarHTTPResponse.Header.Get("Content-Encoding") == "gzip" {
|
||||
gzr, err := _gzip.NewReader(body)
|
||||
if err != nil {
|
||||
body.Close()
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}err
|
||||
}
|
||||
body = &{{#lambda.camelcase}}{{classname}}{{/lambda.camelcase}}GzipReadCloser{underlying: body, gzip: gzr}
|
||||
}
|
||||
|
||||
if localVarHTTPResponse.StatusCode >= 300 {
|
||||
localVarBody, err := _ioutil.ReadAll(body)
|
||||
body.Close()
|
||||
if err != nil {
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}err
|
||||
}
|
||||
newErr := GenericOpenAPIError{
|
||||
body: localVarBody,
|
||||
error: localVarHTTPResponse.Status,
|
||||
}
|
||||
{{#responses}}
|
||||
{{#dataType}}
|
||||
{{^is1xx}}
|
||||
{{^is2xx}}
|
||||
{{^wildcard}}
|
||||
if localVarHTTPResponse.StatusCode == {{{code}}} {
|
||||
{{/wildcard}}
|
||||
var v {{{dataType}}}
|
||||
err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
|
||||
if err != nil {
|
||||
newErr.error = err.Error()
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}newErr
|
||||
}
|
||||
newErr.model = &v
|
||||
{{^-last}}
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}newErr
|
||||
{{/-last}}
|
||||
{{^wildcard}}
|
||||
}
|
||||
{{/wildcard}}
|
||||
{{/is2xx}}
|
||||
{{/is1xx}}
|
||||
{{/dataType}}
|
||||
{{/responses}}
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}newErr
|
||||
}
|
||||
|
||||
{{#returnType}}
|
||||
{{#isResponseBinary}}
|
||||
localVarReturnValue = body
|
||||
{{/isResponseBinary}}
|
||||
{{^isResponseBinary}}
|
||||
localVarBody, err := _ioutil.ReadAll(body)
|
||||
body.Close()
|
||||
if err != nil {
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}err
|
||||
}
|
||||
err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
|
||||
if err != nil {
|
||||
newErr := GenericOpenAPIError{
|
||||
body: localVarBody,
|
||||
error: err.Error(),
|
||||
}
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}newErr
|
||||
}
|
||||
{{/isResponseBinary}}
|
||||
|
||||
{{/returnType}}
|
||||
return {{#returnType}}localVarReturnValue, {{/returnType}}nil
|
||||
}
|
||||
{{/operation}}
|
||||
{{/operations}}
|
600
api/templates/client.mustache
Normal file
600
api/templates/client.mustache
Normal file
@ -0,0 +1,600 @@
|
||||
{{>partial_header}}
|
||||
package {{packageName}}
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
{{#withAWSV4Signature}}
|
||||
awsv4 "github.com/aws/aws-sdk-go/aws/signer/v4"
|
||||
awscredentials "github.com/aws/aws-sdk-go/aws/credentials"
|
||||
{{/withAWSV4Signature}}
|
||||
)
|
||||
|
||||
var (
|
||||
jsonCheck = regexp.MustCompile(`(?i:(?:application|text)/(?:vnd\.[^;]+\+)?json)`)
|
||||
xmlCheck = regexp.MustCompile(`(?i:(?:application|text)/xml)`)
|
||||
)
|
||||
|
||||
// APIClient manages communication with the {{appName}} API v{{version}}
|
||||
// In most cases there should be only one, shared, APIClient.
|
||||
type APIClient struct {
|
||||
cfg *Configuration
|
||||
common service // Reuse a single struct instead of allocating one for each service on the heap.
|
||||
|
||||
// API Services
|
||||
{{#apiInfo}}
|
||||
{{#apis}}
|
||||
{{#operations}}
|
||||
|
||||
{{#generateInterfaces}}
|
||||
{{classname}} {{classname}}
|
||||
{{/generateInterfaces}}
|
||||
{{^generateInterfaces}}
|
||||
{{classname}} *{{classname}}Service
|
||||
{{/generateInterfaces}}
|
||||
{{/operations}}
|
||||
{{/apis}}
|
||||
{{/apiInfo}}
|
||||
}
|
||||
|
||||
type service struct {
|
||||
client *APIClient
|
||||
}
|
||||
|
||||
// NewAPIClient creates a new API client. Requires a userAgent string describing your application.
|
||||
// optionally a custom http.Client to allow for advanced features such as caching.
|
||||
func NewAPIClient(cfg *Configuration) *APIClient {
|
||||
if cfg.HTTPClient == nil {
|
||||
cfg.HTTPClient = http.DefaultClient
|
||||
}
|
||||
|
||||
c := &APIClient{}
|
||||
c.cfg = cfg
|
||||
c.common.client = c
|
||||
|
||||
{{#apiInfo}}
|
||||
// API Services
|
||||
{{#apis}}
|
||||
{{#operations}}
|
||||
c.{{classname}} = (*{{classname}}Service)(&c.common)
|
||||
{{/operations}}
|
||||
{{/apis}}
|
||||
{{/apiInfo}}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
func atoi(in string) (int, error) {
|
||||
return strconv.Atoi(in)
|
||||
}
|
||||
|
||||
// selectHeaderContentType select a content type from the available list.
|
||||
func selectHeaderContentType(contentTypes []string) string {
|
||||
if len(contentTypes) == 0 {
|
||||
return ""
|
||||
}
|
||||
if contains(contentTypes, "application/json") {
|
||||
return "application/json"
|
||||
}
|
||||
return contentTypes[0] // use the first content type specified in 'consumes'
|
||||
}
|
||||
|
||||
// selectHeaderAccept join all accept types and return
|
||||
func selectHeaderAccept(accepts []string) string {
|
||||
if len(accepts) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
if contains(accepts, "application/json") {
|
||||
return "application/json"
|
||||
}
|
||||
|
||||
return strings.Join(accepts, ",")
|
||||
}
|
||||
|
||||
// contains is a case insenstive match, finding needle in a haystack
|
||||
func contains(haystack []string, needle string) bool {
|
||||
for _, a := range haystack {
|
||||
if strings.EqualFold(a, needle) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Verify optional parameters are of the correct type.
|
||||
func typeCheckParameter(obj interface{}, expected string, name string) error {
|
||||
// Make sure there is an object.
|
||||
if obj == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check the type is as expected.
|
||||
if reflect.TypeOf(obj).String() != expected {
|
||||
return fmt.Errorf("expected %s to be of type %s but received %s", name, expected, reflect.TypeOf(obj).String())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// parameterToString convert interface{} parameters to string, using a delimiter if format is provided.
|
||||
func parameterToString(obj interface{}, collectionFormat string) string {
|
||||
var delimiter string
|
||||
|
||||
switch collectionFormat {
|
||||
case "pipes":
|
||||
delimiter = "|"
|
||||
case "ssv":
|
||||
delimiter = " "
|
||||
case "tsv":
|
||||
delimiter = "\t"
|
||||
case "csv":
|
||||
delimiter = ","
|
||||
}
|
||||
|
||||
if reflect.TypeOf(obj).Kind() == reflect.Slice {
|
||||
return strings.Trim(strings.Replace(fmt.Sprint(obj), " ", delimiter, -1), "[]")
|
||||
} else if t, ok := obj.(time.Time); ok {
|
||||
return t.Format(time.RFC3339)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
// helper for converting interface{} parameters to json strings
|
||||
func parameterToJson(obj interface{}) (string, error) {
|
||||
jsonBuf, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(jsonBuf), err
|
||||
}
|
||||
|
||||
// callAPI do the request.
|
||||
func (c *APIClient) callAPI(request *http.Request) (*http.Response, error) {
|
||||
if c.cfg.Debug {
|
||||
dump, err := httputil.DumpRequestOut(request, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Printf("\n%s\n", string(dump))
|
||||
}
|
||||
|
||||
resp, err := c.cfg.HTTPClient.Do(request)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
if c.cfg.Debug {
|
||||
dump, err := httputil.DumpResponse(resp, true)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
log.Printf("\n%s\n", string(dump))
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// Allow modification of underlying config for alternate implementations and testing
|
||||
// Caution: modifying the configuration while live can cause data races and potentially unwanted behavior
|
||||
func (c *APIClient) GetConfig() *Configuration {
|
||||
return c.cfg
|
||||
}
|
||||
|
||||
// prepareRequest build the request
|
||||
func (c *APIClient) prepareRequest(
|
||||
ctx context.Context,
|
||||
path string, method string,
|
||||
postBody interface{},
|
||||
headerParams map[string]string,
|
||||
queryParams url.Values,
|
||||
formParams url.Values,
|
||||
formFileName string,
|
||||
fileName string,
|
||||
fileBytes []byte) (localVarRequest *http.Request, err error) {
|
||||
|
||||
var body *bytes.Buffer
|
||||
|
||||
// Detect postBody type and post.
|
||||
if postBody != nil {
|
||||
contentType := headerParams["Content-Type"]
|
||||
if contentType == "" {
|
||||
contentType = detectContentType(postBody)
|
||||
headerParams["Content-Type"] = contentType
|
||||
}
|
||||
|
||||
body, err = setBody(postBody, contentType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// add form parameters and file if available.
|
||||
if strings.HasPrefix(headerParams["Content-Type"], "multipart/form-data") && len(formParams) > 0 || (len(fileBytes) > 0 && fileName != "") {
|
||||
if body != nil {
|
||||
return nil, errors.New("cannot specify postBody and multipart form at the same time")
|
||||
}
|
||||
body = &bytes.Buffer{}
|
||||
w := multipart.NewWriter(body)
|
||||
|
||||
for k, v := range formParams {
|
||||
for _, iv := range v {
|
||||
if strings.HasPrefix(k, "@") { // file
|
||||
err = addFile(w, k[1:], iv)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else { // form value
|
||||
w.WriteField(k, iv)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(fileBytes) > 0 && fileName != "" {
|
||||
w.Boundary()
|
||||
//_, fileNm := filepath.Split(fileName)
|
||||
part, err := w.CreateFormFile(formFileName, filepath.Base(fileName))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = part.Write(fileBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// Set the Boundary in the Content-Type
|
||||
headerParams["Content-Type"] = w.FormDataContentType()
|
||||
|
||||
// Set Content-Length
|
||||
headerParams["Content-Length"] = fmt.Sprintf("%d", body.Len())
|
||||
w.Close()
|
||||
}
|
||||
|
||||
if strings.HasPrefix(headerParams["Content-Type"], "application/x-www-form-urlencoded") && len(formParams) > 0 {
|
||||
if body != nil {
|
||||
return nil, errors.New("cannot specify postBody and x-www-form-urlencoded form at the same time")
|
||||
}
|
||||
body = &bytes.Buffer{}
|
||||
body.WriteString(formParams.Encode())
|
||||
// Set Content-Length
|
||||
headerParams["Content-Length"] = fmt.Sprintf("%d", body.Len())
|
||||
}
|
||||
|
||||
// Setup path and query parameters
|
||||
url, err := url.Parse(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Override request host, if applicable
|
||||
if c.cfg.Host != "" {
|
||||
url.Host = c.cfg.Host
|
||||
}
|
||||
|
||||
// Override request scheme, if applicable
|
||||
if c.cfg.Scheme != "" {
|
||||
url.Scheme = c.cfg.Scheme
|
||||
}
|
||||
|
||||
// Adding Query Param
|
||||
query := url.Query()
|
||||
for k, v := range queryParams {
|
||||
for _, iv := range v {
|
||||
query.Add(k, iv)
|
||||
}
|
||||
}
|
||||
|
||||
// Encode the parameters.
|
||||
url.RawQuery = query.Encode()
|
||||
|
||||
// Generate a new request
|
||||
if body != nil {
|
||||
var b io.Reader = body
|
||||
if enc, ok := headerParams["Content-Encoding"]; ok && enc == "gzip" {
|
||||
b, err = compressWithGzip(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
localVarRequest, err = http.NewRequest(method, url.String(), b)
|
||||
} else {
|
||||
localVarRequest, err = http.NewRequest(method, url.String(), nil)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// add header parameters, if any
|
||||
if len(headerParams) > 0 {
|
||||
headers := http.Header{}
|
||||
for h, v := range headerParams {
|
||||
headers.Set(h, v)
|
||||
}
|
||||
localVarRequest.Header = headers
|
||||
}
|
||||
|
||||
// Add the user agent to the request.
|
||||
localVarRequest.Header.Add("User-Agent", c.cfg.UserAgent)
|
||||
|
||||
if ctx != nil {
|
||||
// add context to the request
|
||||
localVarRequest = localVarRequest.WithContext(ctx)
|
||||
|
||||
// Walk through any authentication.
|
||||
|
||||
// Basic HTTP Authentication
|
||||
if auth, ok := ctx.Value(ContextBasicAuth).(BasicAuth); ok {
|
||||
localVarRequest.SetBasicAuth(auth.UserName, auth.Password)
|
||||
}
|
||||
|
||||
// AccessToken Authentication
|
||||
if auth, ok := ctx.Value(ContextAccessToken).(string); ok {
|
||||
localVarRequest.Header.Add("Authorization", "Bearer "+auth)
|
||||
}
|
||||
|
||||
{{#withAWSV4Signature}}
|
||||
// AWS Signature v4 Authentication
|
||||
if auth, ok := ctx.Value(ContextAWSv4).(AWSv4); ok {
|
||||
creds := awscredentials.NewStaticCredentials(auth.AccessKey, auth.SecretKey, auth.SessionToken)
|
||||
signer := awsv4.NewSigner(creds)
|
||||
var reader *strings.Reader
|
||||
if body == nil {
|
||||
reader = strings.NewReader("")
|
||||
} else {
|
||||
reader = strings.NewReader(body.String())
|
||||
}
|
||||
|
||||
// Define default values for region and service to maintain backward compatibility
|
||||
region := auth.Region
|
||||
if region == "" {
|
||||
region = "eu-west-2"
|
||||
}
|
||||
service := auth.Service
|
||||
if service == "" {
|
||||
service = "oapi"
|
||||
}
|
||||
|
||||
timestamp := time.Now()
|
||||
_, err := signer.Sign(localVarRequest, reader, service, region, timestamp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
{{/withAWSV4Signature}}
|
||||
}
|
||||
|
||||
for header, value := range c.cfg.DefaultHeader {
|
||||
localVarRequest.Header.Add(header, value)
|
||||
}
|
||||
{{#hasHttpSignatureMethods}}
|
||||
if ctx != nil {
|
||||
// HTTP Signature Authentication. All request headers must be set (including default headers)
|
||||
// because the headers may be included in the signature.
|
||||
if auth, ok := ctx.Value(ContextHttpSignatureAuth).(HttpSignatureAuth); ok {
|
||||
err = SignRequest(ctx, localVarRequest, auth)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
{{/hasHttpSignatureMethods}}
|
||||
return localVarRequest, nil
|
||||
}
|
||||
|
||||
func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err error) {
|
||||
if len(b) == 0 {
|
||||
return nil
|
||||
}
|
||||
if s, ok := v.(*string); ok {
|
||||
*s = string(b)
|
||||
return nil
|
||||
}
|
||||
if xmlCheck.MatchString(contentType) {
|
||||
if err = xml.Unmarshal(b, v); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if jsonCheck.MatchString(contentType) {
|
||||
if actualObj, ok := v.(interface{ GetActualInstance() interface{} }); ok { // oneOf, anyOf schemas
|
||||
if unmarshalObj, ok := actualObj.(interface{ UnmarshalJSON([]byte) error }); ok { // make sure it has UnmarshalJSON defined
|
||||
if err = unmarshalObj.UnmarshalJSON(b); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return errors.New("unknown type with GetActualInstance but no unmarshalObj.UnmarshalJSON defined")
|
||||
}
|
||||
} else if err = json.Unmarshal(b, v); err != nil { // simple model
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return errors.New("undefined response type")
|
||||
}
|
||||
|
||||
// Add a file to the multipart request
|
||||
func addFile(w *multipart.Writer, fieldName, path string) error {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
part, err := w.CreateFormFile(fieldName, filepath.Base(path))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = io.Copy(part, file)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Prevent trying to import "fmt"
|
||||
func reportError(format string, a ...interface{}) error {
|
||||
return fmt.Errorf(format, a...)
|
||||
}
|
||||
|
||||
// Set request body from an interface{}
|
||||
func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err error) {
|
||||
if bodyBuf == nil {
|
||||
bodyBuf = &bytes.Buffer{}
|
||||
}
|
||||
|
||||
if reader, ok := body.(io.Reader); ok {
|
||||
_, err = bodyBuf.ReadFrom(reader)
|
||||
} else if fp, ok := body.(**os.File); ok {
|
||||
_, err = bodyBuf.ReadFrom(*fp)
|
||||
} else if b, ok := body.([]byte); ok {
|
||||
_, err = bodyBuf.Write(b)
|
||||
} else if s, ok := body.(string); ok {
|
||||
_, err = bodyBuf.WriteString(s)
|
||||
} else if s, ok := body.(*string); ok {
|
||||
_, err = bodyBuf.WriteString(*s)
|
||||
} else if jsonCheck.MatchString(contentType) {
|
||||
err = json.NewEncoder(bodyBuf).Encode(body)
|
||||
} else if xmlCheck.MatchString(contentType) {
|
||||
err = xml.NewEncoder(bodyBuf).Encode(body)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if bodyBuf.Len() == 0 {
|
||||
err = fmt.Errorf("invalid body type %s", contentType)
|
||||
return nil, err
|
||||
}
|
||||
return bodyBuf, nil
|
||||
}
|
||||
|
||||
func compressWithGzip(data io.Reader) (io.Reader, error) {
|
||||
pr, pw := io.Pipe()
|
||||
gw := gzip.NewWriter(pw)
|
||||
var err error
|
||||
|
||||
go func() {
|
||||
_, err = io.Copy(gw, data)
|
||||
gw.Close()
|
||||
pw.Close()
|
||||
}()
|
||||
|
||||
return pr, err
|
||||
}
|
||||
|
||||
// detectContentType method is used to figure out `Request.Body` content type for request header
|
||||
func detectContentType(body interface{}) string {
|
||||
contentType := "text/plain; charset=utf-8"
|
||||
kind := reflect.TypeOf(body).Kind()
|
||||
|
||||
switch kind {
|
||||
case reflect.Struct, reflect.Map, reflect.Ptr:
|
||||
contentType = "application/json; charset=utf-8"
|
||||
case reflect.String:
|
||||
contentType = "text/plain; charset=utf-8"
|
||||
default:
|
||||
if b, ok := body.([]byte); ok {
|
||||
contentType = http.DetectContentType(b)
|
||||
} else if kind == reflect.Slice {
|
||||
contentType = "application/json; charset=utf-8"
|
||||
}
|
||||
}
|
||||
|
||||
return contentType
|
||||
}
|
||||
|
||||
// Ripped from https://github.com/gregjones/httpcache/blob/master/httpcache.go
|
||||
type cacheControl map[string]string
|
||||
|
||||
func parseCacheControl(headers http.Header) cacheControl {
|
||||
cc := cacheControl{}
|
||||
ccHeader := headers.Get("Cache-Control")
|
||||
for _, part := range strings.Split(ccHeader, ",") {
|
||||
part = strings.Trim(part, " ")
|
||||
if part == "" {
|
||||
continue
|
||||
}
|
||||
if strings.ContainsRune(part, '=') {
|
||||
keyval := strings.Split(part, "=")
|
||||
cc[strings.Trim(keyval[0], " ")] = strings.Trim(keyval[1], ",")
|
||||
} else {
|
||||
cc[part] = ""
|
||||
}
|
||||
}
|
||||
return cc
|
||||
}
|
||||
|
||||
// CacheExpires helper function to determine remaining time before repeating a request.
|
||||
func CacheExpires(r *http.Response) time.Time {
|
||||
// Figure out when the cache expires.
|
||||
var expires time.Time
|
||||
now, err := time.Parse(time.RFC1123, r.Header.Get("date"))
|
||||
if err != nil {
|
||||
return time.Now()
|
||||
}
|
||||
respCacheControl := parseCacheControl(r.Header)
|
||||
|
||||
if maxAge, ok := respCacheControl["max-age"]; ok {
|
||||
lifetime, err := time.ParseDuration(maxAge + "s")
|
||||
if err != nil {
|
||||
expires = now
|
||||
} else {
|
||||
expires = now.Add(lifetime)
|
||||
}
|
||||
} else {
|
||||
expiresHeader := r.Header.Get("Expires")
|
||||
if expiresHeader != "" {
|
||||
expires, err = time.Parse(time.RFC1123, expiresHeader)
|
||||
if err != nil {
|
||||
expires = now
|
||||
}
|
||||
}
|
||||
}
|
||||
return expires
|
||||
}
|
||||
|
||||
func strlen(s string) int {
|
||||
return utf8.RuneCountInString(s)
|
||||
}
|
||||
|
||||
// GenericOpenAPIError Provides access to the body, error and model on returned errors.
|
||||
type GenericOpenAPIError struct {
|
||||
body []byte
|
||||
error string
|
||||
model error
|
||||
}
|
||||
|
||||
// Error returns non-empty string if there was an error.
|
||||
func (e GenericOpenAPIError) Error() string {
|
||||
if e.model != nil {
|
||||
return e.model.Error()
|
||||
}
|
||||
return e.error
|
||||
}
|
||||
|
||||
// Body returns the raw bytes of the response
|
||||
func (e GenericOpenAPIError) Body() []byte {
|
||||
return e.body
|
||||
}
|
||||
|
||||
// Model returns the unpacked model of the error
|
||||
func (e GenericOpenAPIError) Model() interface{} {
|
||||
return e.model
|
||||
}
|
303
api/templates/configuration.mustache
Normal file
303
api/templates/configuration.mustache
Normal file
@ -0,0 +1,303 @@
|
||||
{{>partial_header}}
|
||||
package {{packageName}}
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// contextKeys are used to identify the type of value in the context.
|
||||
// Since these are string, it is possible to get a short description of the
|
||||
// context key for logging and debugging using key.String().
|
||||
|
||||
type contextKey string
|
||||
|
||||
func (c contextKey) String() string {
|
||||
return "auth " + string(c)
|
||||
}
|
||||
|
||||
var (
|
||||
// ContextBasicAuth takes BasicAuth as authentication for the request.
|
||||
ContextBasicAuth = contextKey("basic")
|
||||
|
||||
// ContextAccessToken takes a string oauth2 access token as authentication for the request.
|
||||
ContextAccessToken = contextKey("accesstoken")
|
||||
|
||||
// ContextAPIKeys takes a string apikey as authentication for the request
|
||||
ContextAPIKeys = contextKey("apiKeys")
|
||||
|
||||
{{#withAWSV4Signature}}
|
||||
// ContextAWSv4 takes an Access Key and a Secret Key for signing AWS Signature v4
|
||||
ContextAWSv4 = contextKey("awsv4")
|
||||
|
||||
{{/withAWSV4Signature}}
|
||||
// ContextHttpSignatureAuth takes HttpSignatureAuth as authentication for the request.
|
||||
ContextHttpSignatureAuth = contextKey("httpsignature")
|
||||
|
||||
// ContextServerIndex uses a server configuration from the index.
|
||||
ContextServerIndex = contextKey("serverIndex")
|
||||
|
||||
// ContextOperationServerIndices uses a server configuration from the index mapping.
|
||||
ContextOperationServerIndices = contextKey("serverOperationIndices")
|
||||
|
||||
// ContextServerVariables overrides a server configuration variables.
|
||||
ContextServerVariables = contextKey("serverVariables")
|
||||
|
||||
// ContextOperationServerVariables overrides a server configuration variables using operation specific values.
|
||||
ContextOperationServerVariables = contextKey("serverOperationVariables")
|
||||
)
|
||||
|
||||
// BasicAuth provides basic http authentication to a request passed via context using ContextBasicAuth
|
||||
type BasicAuth struct {
|
||||
UserName string `json:"userName,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
}
|
||||
|
||||
// APIKey provides API key based authentication to a request passed via context using ContextAPIKey
|
||||
type APIKey struct {
|
||||
Key string
|
||||
Prefix string
|
||||
}
|
||||
|
||||
{{#withAWSV4Signature}}
|
||||
// AWSv4 provides AWS Signature to a request passed via context using ContextAWSv4
|
||||
// https://docs.aws.amazon.com/general/latest/gr/signature-version-4.html
|
||||
type AWSv4 struct {
|
||||
AccessKey string
|
||||
SecretKey string
|
||||
SessionToken string
|
||||
Region string
|
||||
Service string
|
||||
}
|
||||
|
||||
{{/withAWSV4Signature}}
|
||||
// ServerVariable stores the information about a server variable
|
||||
type ServerVariable struct {
|
||||
Description string
|
||||
DefaultValue string
|
||||
EnumValues []string
|
||||
}
|
||||
|
||||
// ServerConfiguration stores the information about a server
|
||||
type ServerConfiguration struct {
|
||||
URL string
|
||||
Description string
|
||||
Variables map[string]ServerVariable
|
||||
}
|
||||
|
||||
// ServerConfigurations stores multiple ServerConfiguration items
|
||||
type ServerConfigurations []ServerConfiguration
|
||||
|
||||
// Configuration stores the configuration of the API client
|
||||
type Configuration struct {
|
||||
Host string `json:"host,omitempty"`
|
||||
Scheme string `json:"scheme,omitempty"`
|
||||
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
|
||||
UserAgent string `json:"userAgent,omitempty"`
|
||||
Debug bool `json:"debug,omitempty"`
|
||||
Servers ServerConfigurations
|
||||
OperationServers map[string]ServerConfigurations
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
// NewConfiguration returns a new Configuration object
|
||||
func NewConfiguration() *Configuration {
|
||||
cfg := &Configuration{
|
||||
DefaultHeader: make(map[string]string),
|
||||
UserAgent: "{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{{packageVersion}}}/go{{/httpUserAgent}}",
|
||||
Debug: false,
|
||||
{{#servers}}
|
||||
{{#-first}}
|
||||
Servers: ServerConfigurations{
|
||||
{{/-first}}
|
||||
{
|
||||
URL: "{{{url}}}",
|
||||
Description: "{{{description}}}{{^description}}No description provided{{/description}}",
|
||||
{{#variables}}
|
||||
{{#-first}}
|
||||
Variables: map[string]ServerVariable{
|
||||
{{/-first}}
|
||||
"{{{name}}}": ServerVariable{
|
||||
Description: "{{{description}}}{{^description}}No description provided{{/description}}",
|
||||
DefaultValue: "{{{defaultValue}}}",
|
||||
{{#enumValues}}
|
||||
{{#-first}}
|
||||
EnumValues: []string{
|
||||
{{/-first}}
|
||||
"{{{.}}}",
|
||||
{{#-last}}
|
||||
},
|
||||
{{/-last}}
|
||||
{{/enumValues}}
|
||||
},
|
||||
{{#-last}}
|
||||
},
|
||||
{{/-last}}
|
||||
{{/variables}}
|
||||
},
|
||||
{{#-last}}
|
||||
},
|
||||
{{/-last}}
|
||||
{{/servers}}
|
||||
{{#apiInfo}}
|
||||
OperationServers: map[string]ServerConfigurations{
|
||||
{{#apis}}
|
||||
{{#operations}}
|
||||
{{#operation}}
|
||||
{{#servers}}
|
||||
{{#-first}}
|
||||
"{{{classname}}}Service.{{{nickname}}}": {
|
||||
{{/-first}}
|
||||
{
|
||||
URL: "{{{url}}}",
|
||||
Description: "{{{description}}}{{^description}}No description provided{{/description}}",
|
||||
{{#variables}}
|
||||
{{#-first}}
|
||||
Variables: map[string]ServerVariable{
|
||||
{{/-first}}
|
||||
"{{{name}}}": ServerVariable{
|
||||
Description: "{{{description}}}{{^description}}No description provided{{/description}}",
|
||||
DefaultValue: "{{{defaultValue}}}",
|
||||
{{#enumValues}}
|
||||
{{#-first}}
|
||||
EnumValues: []string{
|
||||
{{/-first}}
|
||||
"{{{.}}}",
|
||||
{{#-last}}
|
||||
},
|
||||
{{/-last}}
|
||||
{{/enumValues}}
|
||||
},
|
||||
{{#-last}}
|
||||
},
|
||||
{{/-last}}
|
||||
{{/variables}}
|
||||
},
|
||||
{{#-last}}
|
||||
},
|
||||
{{/-last}}
|
||||
{{/servers}}
|
||||
{{/operation}}
|
||||
{{/operations}}
|
||||
{{/apis}}
|
||||
},
|
||||
{{/apiInfo}}
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
// AddDefaultHeader adds a new HTTP header to the default header in the request
|
||||
func (c *Configuration) AddDefaultHeader(key string, value string) {
|
||||
c.DefaultHeader[key] = value
|
||||
}
|
||||
|
||||
// URL formats template on a index using given variables
|
||||
func (sc ServerConfigurations) URL(index int, variables map[string]string) (string, error) {
|
||||
if index < 0 || len(sc) <= index {
|
||||
return "", fmt.Errorf("index %v out of range %v", index, len(sc)-1)
|
||||
}
|
||||
server := sc[index]
|
||||
url := server.URL
|
||||
|
||||
// go through variables and replace placeholders
|
||||
for name, variable := range server.Variables {
|
||||
if value, ok := variables[name]; ok {
|
||||
found := bool(len(variable.EnumValues) == 0)
|
||||
for _, enumValue := range variable.EnumValues {
|
||||
if value == enumValue {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return "", fmt.Errorf("the variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues)
|
||||
}
|
||||
url = strings.Replace(url, "{"+name+"}", value, -1)
|
||||
} else {
|
||||
url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1)
|
||||
}
|
||||
}
|
||||
return url, nil
|
||||
}
|
||||
|
||||
// ServerURL returns URL based on server settings
|
||||
func (c *Configuration) ServerURL(index int, variables map[string]string) (string, error) {
|
||||
return c.Servers.URL(index, variables)
|
||||
}
|
||||
|
||||
func getServerIndex(ctx context.Context) (int, error) {
|
||||
si := ctx.Value(ContextServerIndex)
|
||||
if si != nil {
|
||||
if index, ok := si.(int); ok {
|
||||
return index, nil
|
||||
}
|
||||
return 0, reportError("Invalid type %T should be int", si)
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func getServerOperationIndex(ctx context.Context, endpoint string) (int, error) {
|
||||
osi := ctx.Value(ContextOperationServerIndices)
|
||||
if osi != nil {
|
||||
if operationIndices, ok := osi.(map[string]int); !ok {
|
||||
return 0, reportError("Invalid type %T should be map[string]int", osi)
|
||||
} else {
|
||||
index, ok := operationIndices[endpoint]
|
||||
if ok {
|
||||
return index, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return getServerIndex(ctx)
|
||||
}
|
||||
|
||||
func getServerVariables(ctx context.Context) (map[string]string, error) {
|
||||
sv := ctx.Value(ContextServerVariables)
|
||||
if sv != nil {
|
||||
if variables, ok := sv.(map[string]string); ok {
|
||||
return variables, nil
|
||||
}
|
||||
return nil, reportError("ctx value of ContextServerVariables has invalid type %T should be map[string]string", sv)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func getServerOperationVariables(ctx context.Context, endpoint string) (map[string]string, error) {
|
||||
osv := ctx.Value(ContextOperationServerVariables)
|
||||
if osv != nil {
|
||||
if operationVariables, ok := osv.(map[string]map[string]string); !ok {
|
||||
return nil, reportError("ctx value of ContextOperationServerVariables has invalid type %T should be map[string]map[string]string", osv)
|
||||
} else {
|
||||
variables, ok := operationVariables[endpoint]
|
||||
if ok {
|
||||
return variables, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return getServerVariables(ctx)
|
||||
}
|
||||
|
||||
// ServerURLWithContext returns a new server URL given an endpoint
|
||||
func (c *Configuration) ServerURLWithContext(ctx context.Context, endpoint string) (string, error) {
|
||||
sc, ok := c.OperationServers[endpoint]
|
||||
if !ok {
|
||||
sc = c.Servers
|
||||
}
|
||||
|
||||
if ctx == nil {
|
||||
return sc.URL(0, nil)
|
||||
}
|
||||
|
||||
index, err := getServerOperationIndex(ctx, endpoint)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
variables, err := getServerOperationVariables(ctx, endpoint)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return sc.URL(index, variables)
|
||||
}
|
109
api/templates/model_oneof.mustache
Normal file
109
api/templates/model_oneof.mustache
Normal file
@ -0,0 +1,109 @@
|
||||
// {{classname}} - {{#description}}{{{description}}}{{/description}}{{^description}}struct for {{{classname}}}{{/description}}
|
||||
type {{classname}} struct {
|
||||
{{#oneOf}}
|
||||
{{{.}}} *{{{.}}}
|
||||
{{/oneOf}}
|
||||
}
|
||||
|
||||
{{#oneOf}}
|
||||
// {{{.}}}As{{classname}} is a convenience function that returns {{{.}}} wrapped in {{classname}}
|
||||
func {{{.}}}As{{classname}}(v *{{{.}}}) {{classname}} {
|
||||
return {{classname}}{ {{{.}}}: v}
|
||||
}
|
||||
|
||||
{{/oneOf}}
|
||||
|
||||
// Unmarshal JSON data into one of the pointers in the struct
|
||||
func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
|
||||
var err error
|
||||
{{#isNullable}}
|
||||
// this object is nullable so check if the payload is null or empty string
|
||||
if string(data) == "" || string(data) == "{}" {
|
||||
return nil
|
||||
}
|
||||
|
||||
{{/isNullable}}
|
||||
{{#useOneOfDiscriminatorLookup}}
|
||||
{{#discriminator}}
|
||||
{{#mappedModels}}
|
||||
{{#-first}}
|
||||
// use discriminator value to speed up the lookup
|
||||
var jsonDict map[string]interface{}
|
||||
err = json.Unmarshal(data, &jsonDict)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to unmarshal JSON into map for the discrimintor lookup.")
|
||||
}
|
||||
|
||||
{{/-first}}
|
||||
// check if the discriminator value is '{{{mappingName}}}'
|
||||
if jsonDict["{{{propertyBaseName}}}"] == "{{{mappingName}}}" {
|
||||
// try to unmarshal JSON data into {{{modelName}}}
|
||||
err = json.Unmarshal(data, &dst.{{{modelName}}})
|
||||
if err == nil {
|
||||
return nil // data stored in dst.{{{modelName}}}, return on the first match
|
||||
} else {
|
||||
dst.{{{modelName}}} = nil
|
||||
return fmt.Errorf("Failed to unmarshal {{classname}} as {{{modelName}}}: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
{{/mappedModels}}
|
||||
{{/discriminator}}
|
||||
return nil
|
||||
{{/useOneOfDiscriminatorLookup}}
|
||||
{{^useOneOfDiscriminatorLookup}}
|
||||
match := 0
|
||||
{{#oneOf}}
|
||||
// try to unmarshal data into {{{.}}}
|
||||
err = json.Unmarshal(data, &dst.{{{.}}})
|
||||
if err == nil {
|
||||
json{{{.}}}, _ := json.Marshal(dst.{{{.}}})
|
||||
if string(json{{{.}}}) == "{}" { // empty struct
|
||||
dst.{{{.}}} = nil
|
||||
} else {
|
||||
match++
|
||||
}
|
||||
} else {
|
||||
dst.{{{.}}} = nil
|
||||
}
|
||||
|
||||
{{/oneOf}}
|
||||
if match > 1 { // more than 1 match
|
||||
// reset to nil
|
||||
{{#oneOf}}
|
||||
dst.{{{.}}} = nil
|
||||
{{/oneOf}}
|
||||
|
||||
return errors.New("data matches more than one schema in oneOf({{classname}})")
|
||||
} else if match == 1 {
|
||||
return nil // exactly one match
|
||||
} else { // no match
|
||||
return errors.New("data failed to match schemas in oneOf({{classname}})")
|
||||
}
|
||||
{{/useOneOfDiscriminatorLookup}}
|
||||
}
|
||||
|
||||
// Marshal data from the first non-nil pointers in the struct to JSON
|
||||
func (src {{classname}}) MarshalJSON() ([]byte, error) {
|
||||
{{#oneOf}}
|
||||
if src.{{{.}}} != nil {
|
||||
return json.Marshal(&src.{{{.}}})
|
||||
}
|
||||
|
||||
{{/oneOf}}
|
||||
return nil, nil // no data in oneOf schemas
|
||||
}
|
||||
|
||||
// Get the actual instance
|
||||
func (obj *{{classname}}) GetActualInstance() (interface{}) {
|
||||
{{#oneOf}}
|
||||
if obj.{{{.}}} != nil {
|
||||
return obj.{{{.}}}
|
||||
}
|
||||
|
||||
{{/oneOf}}
|
||||
// all schemas are nil
|
||||
return nil
|
||||
}
|
||||
|
||||
{{>nullable_model}}
|
Reference in New Issue
Block a user