github.com/optim-corp/cios-golang-sdk@v0.5.1/openapi/codegen/golang-client/5.0.0/api.mustache (about)

     1  {{>partial_header}}
     2  package {{packageName}}
     3  
     4  {{#operations}}
     5  import (
     6  	_context "context"
     7  	_ioutil "io/ioutil"
     8  	_nethttp "net/http"
     9  	_neturl "net/url"
    10  {{#imports}}	"{{import}}"
    11  {{/imports}}
    12  )
    13  
    14  // Linger please
    15  var (
    16  	_ _context.Context
    17  )
    18  {{#generateInterfaces}}
    19  
    20  type {{classname}} interface {
    21    {{#operation}}
    22  
    23    /*
    24     * {{operationId}}{{#summary}} {{{.}}}{{/summary}}{{^summary}} Method for {{operationId}}{{/summary}}
    25    {{#notes}}
    26     * {{{unescapedNotes}}}
    27    {{/notes}}
    28     * @param Ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{{#pathParams}}
    29     * @param {{paramName}}{{#description}} {{{.}}}{{/description}}{{/pathParams}}
    30     * @return {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request
    31     */
    32    {{{nickname}}}(Ctx _context.Context{{#pathParams}}, {{paramName}} {{{dataType}}}{{/pathParams}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request
    33  
    34    /*
    35     * {{nickname}}Execute executes the request{{#returnType}}
    36     * @return {{{.}}}{{/returnType}}
    37     */
    38    {{nickname}}Execute(r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) ({{#returnType}}{{{.}}}, {{/returnType}}*_nethttp.Response, error)
    39    {{/operation}}
    40  }
    41  {{/generateInterfaces}}
    42  
    43  // {{classname}}Service {{classname}} service
    44  type {{classname}}Service service
    45  {{#operation}}
    46  
    47  type {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request struct {
    48  	Ctx _context.Context{{#generateInterfaces}}
    49  	ApiService {{classname}}
    50  {{/generateInterfaces}}{{^generateInterfaces}}
    51  	ApiService *{{classname}}Service
    52  {{/generateInterfaces}}
    53  {{#allParams}}
    54  	P_{{paramName}} {{^isPathParam}}*{{/isPathParam}}{{{dataType}}}
    55  {{/allParams}}
    56  }
    57  {{#allParams}}{{^isPathParam}}
    58  func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) {{vendorExtensions.x-export-param-name}}({{paramName}} {{{dataType}}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request {
    59  	r.P_{{paramName}} = &{{paramName}}
    60  	return r
    61  }{{/isPathParam}}{{/allParams}}
    62  
    63  func (r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) Execute() ({{#returnType}}{{{.}}}, {{/returnType}}*_nethttp.Response, error) {
    64  	return r.ApiService.{{nickname}}Execute(r)
    65  }
    66  
    67  /*
    68   * {{operationId}}{{#summary}} {{{.}}}{{/summary}}{{^summary}} Method for {{operationId}}{{/summary}}
    69  {{#notes}}
    70   * {{{unescapedNotes}}}
    71  {{/notes}}
    72   * @param Ctx _context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().{{#pathParams}}
    73   * @param {{paramName}}{{#description}} {{{.}}}{{/description}}{{/pathParams}}
    74   * @return {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request
    75   */
    76  func (a *{{{classname}}}Service) {{{nickname}}}(Ctx _context.Context{{#pathParams}}, {{paramName}} {{{dataType}}}{{/pathParams}}) {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request {
    77  	return {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request{
    78  		ApiService: a,
    79  		Ctx: Ctx,{{#pathParams}}
    80  		P_{{paramName}}: {{paramName}},{{/pathParams}}
    81  	}
    82  }
    83  
    84  /*
    85   * Execute executes the request{{#returnType}}
    86   * @return {{{.}}}{{/returnType}}
    87   */
    88  func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&classname}}{{/structPrefix}}Api{{operationId}}Request) ({{#returnType}}{{{.}}}, {{/returnType}}*_nethttp.Response, error) {
    89  	var (
    90  		localVarHTTPMethod   = _nethttp.Method{{httpMethod}}
    91  		localVarPostBody     interface{}
    92  		localVarFormFileName string
    93  		localVarFileName     string
    94  		localVarFileBytes    []byte
    95  		{{#returnType}}
    96  		localVarReturnValue  {{{.}}}
    97  		{{/returnType}}
    98  	)
    99  
   100  	localBasePath, err := a.client.cfg.ServerURLWithContext(r.Ctx, "{{{classname}}}Service.{{{nickname}}}")
   101  	if localBasePath == "/" {
   102  	    localBasePath = ""
   103  	}
   104  	if err != nil {
   105  		return {{#returnType}}localVarReturnValue, {{/returnType}}nil, GenericOpenAPIError{error: err.Error()}
   106  	}
   107  
   108  	localVarPath := localBasePath + "{{{path}}}"{{#pathParams}}
   109  	localVarPath = strings.Replace(localVarPath, "{"+"{{baseName}}"+"}", _neturl.PathEscape(parameterToString(r.P_{{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}")) , -1){{/pathParams}}
   110  
   111  	localVarHeaderParams := make(map[string]string)
   112  	localVarQueryParams := _neturl.Values{}
   113  	localVarFormParams := _neturl.Values{}
   114  	{{#allParams}}
   115  	{{#required}}
   116  	{{^isPathParam}}
   117  	if r.P_{{paramName}} == nil {
   118  		return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} is required and must be specified")
   119  	}
   120  	{{/isPathParam}}
   121  	{{#minItems}}
   122  	if len({{^isPathParam}}*{{/isPathParam}}r.P_{{paramName}}) < {{minItems}} {
   123  		return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} must have at least {{minItems}} elements")
   124  	}
   125  	{{/minItems}}
   126  	{{#maxItems}}
   127  	if len({{^isPathParam}}*{{/isPathParam}}r.P_{{paramName}}) > {{maxItems}} {
   128  		return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} must have less than {{maxItems}} elements")
   129  	}
   130  	{{/maxItems}}
   131  	{{#minLength}}
   132  	if strlen({{^isPathParam}}*{{/isPathParam}}r.P_{{paramName}}) < {{minLength}} {
   133  		return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} must have at least {{minLength}} elements")
   134  	}
   135  	{{/minLength}}
   136  	{{#maxLength}}
   137  	if strlen({{^isPathParam}}*{{/isPathParam}}r.P_{{paramName}}) > {{maxLength}} {
   138  		return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} must have less than {{maxLength}} elements")
   139  	}
   140  	{{/maxLength}}
   141  	{{#minimum}}
   142  	{{#isString}}
   143  	{{paramName}}Txt, err := atoi({{^isPathParam}}*{{/isPathParam}}r.P_{{paramName}})
   144  	if {{paramName}}Txt < {{minimum}} {
   145  	{{/isString}}
   146  	{{^isString}}
   147  	if {{^isPathParam}}*{{/isPathParam}}r.P_{{paramName}} < {{minimum}} {
   148  	{{/isString}}
   149  		return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} must be greater than {{minimum}}")
   150  	}
   151  	{{/minimum}}
   152  	{{#maximum}}
   153  	{{#isString}}
   154  	{{paramName}}Txt, err := atoi({{^isPathParam}}*{{/isPathParam}}r.P_{{paramName}})
   155  	if {{paramName}}Txt > {{maximum}} {
   156  	{{/isString}}
   157  	{{^isString}}
   158  	if {{^isPathParam}}*{{/isPathParam}}r.P_{{paramName}} > {{maximum}} {
   159  	{{/isString}}
   160  		return {{#returnType}}localVarReturnValue, {{/returnType}}nil, reportError("{{paramName}} must be less than {{maximum}}")
   161  	}
   162  	{{/maximum}}
   163  	{{/required}}
   164  	{{/allParams}}
   165  
   166  	{{#queryParams}}
   167  	{{#required}}
   168  	{{#isCollectionFormatMulti}}
   169  	{
   170  		t := *r.P_{{paramName}}
   171  		if reflect.TypeOf(t).Kind() == reflect.Slice {
   172  			s := reflect.ValueOf(t)
   173  			for i := 0; i < s.Len(); i++ {
   174  				localVarQueryParams.Add("{{baseName}}", parameterToString(s.Index(i), "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
   175  			}
   176  		} else {
   177  			localVarQueryParams.Add("{{baseName}}", parameterToString(t, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
   178  		}
   179  	}
   180  	{{/isCollectionFormatMulti}}
   181  	{{^isCollectionFormatMulti}}
   182  	localVarQueryParams.Add("{{baseName}}", parameterToString(*r.P_{{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
   183  	{{/isCollectionFormatMulti}}
   184  	{{/required}}
   185  	{{^required}}
   186  	if r.P_{{paramName}} != nil {
   187  	{{#isCollectionFormatMulti}}
   188  		t := *r.P_{{paramName}}
   189  		if reflect.TypeOf(t).Kind() == reflect.Slice {
   190  			s := reflect.ValueOf(t)
   191  			for i := 0; i < s.Len(); i++ {
   192  				localVarQueryParams.Add("{{baseName}}", parameterToString(s.Index(i), "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
   193  			}
   194  		} else {
   195  			localVarQueryParams.Add("{{baseName}}", parameterToString(t, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
   196  		}
   197  	{{/isCollectionFormatMulti}}
   198  	{{^isCollectionFormatMulti}}
   199  		localVarQueryParams.Add("{{baseName}}", parameterToString(*r.P_{{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
   200  	{{/isCollectionFormatMulti}}
   201  	}
   202  	{{/required}}
   203  	{{/queryParams}}
   204  	// to determine the Content-Type header
   205  {{=<% %>=}}
   206  	localVarHTTPContentTypes := []string{<%#consumes%>"<%&mediaType%>"<%^-last%>, <%/-last%><%/consumes%>}
   207  <%={{ }}=%>
   208  
   209  	// set Content-Type header
   210  	localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes)
   211  	if localVarHTTPContentType != "" {
   212  		localVarHeaderParams["Content-Type"] = localVarHTTPContentType
   213  	}
   214  
   215  	// to determine the Accept header
   216  {{=<% %>=}}
   217  	localVarHTTPHeaderAccepts := []string{<%#produces%>"<%&mediaType%>"<%^-last%>, <%/-last%><%/produces%>}
   218  <%={{ }}=%>
   219  
   220  	// set Accept header
   221  	localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
   222  	if localVarHTTPHeaderAccept != "" {
   223  		localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
   224  	}
   225  {{#headerParams}}
   226  	{{#required}}
   227  	localVarHeaderParams["{{baseName}}"] = parameterToString(*r.P_{{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}")
   228  	{{/required}}
   229  	{{^required}}
   230  	if r.P_{{paramName}} != nil {
   231  		localVarHeaderParams["{{baseName}}"] = parameterToString(*r.P_{{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}")
   232  	}
   233  	{{/required}}
   234  {{/headerParams}}
   235  {{#formParams}}
   236  {{#isFile}}
   237  	localVarFormFileName = "{{baseName}}"
   238  {{#required}}
   239  	localVarFile := *r.P_{{paramName}}
   240  {{/required}}
   241  {{^required}}
   242  	var localVarFile {{dataType}}
   243  	if r.P_{{paramName}} != nil {
   244  		localVarFile = *r.P_{{paramName}}
   245  	}
   246  {{/required}}
   247  	if localVarFile != nil {
   248  		fbs, _ := _ioutil.ReadAll(localVarFile)
   249  		localVarFileBytes = fbs
   250  		localVarFileName = localVarFile.Name()
   251  		localVarFile.Close()
   252  	}
   253  {{/isFile}}
   254  {{^isFile}}
   255  {{#required}}
   256  	localVarFormParams.Add("{{baseName}}", parameterToString(*r.P_{{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
   257  {{/required}}
   258  {{^required}}
   259  {{#isModel}}
   260  	if r.P_{{paramName}} != nil {
   261  		paramJson, err := parameterToJson(*r.P_{{paramName}})
   262  		if err != nil {
   263  			return {{#returnType}}localVarReturnValue, {{/returnType}}nil, err
   264  		}
   265  		localVarFormParams.Add("{{baseName}}", paramJson)
   266  	}
   267  {{/isModel}}
   268  {{^isModel}}
   269  	if r.P_{{paramName}} != nil {
   270  		localVarFormParams.Add("{{baseName}}", parameterToString(*r.P_{{paramName}}, "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"))
   271  	}
   272  {{/isModel}}
   273  {{/required}}
   274  {{/isFile}}
   275  {{/formParams}}
   276  {{#bodyParams}}
   277  	// body params
   278  	localVarPostBody = r.P_{{paramName}}
   279  {{/bodyParams}}
   280  {{#authMethods}}
   281  {{#isApiKey}}
   282  {{^isKeyInCookie}}
   283  	if r.Ctx != nil {
   284  		// API Key Authentication
   285  		if auth, ok := r.Ctx.Value(ContextAPIKeys).(map[string]APIKey); ok {
   286  			{{#vendorExtensions.x-auth-id-alias}}
   287  			if apiKey, ok := auth["{{.}}"]; ok {
   288  				var key string
   289  				if prefix, ok := auth["{{name}}"]; ok && prefix.Prefix != "" {
   290  					key = prefix.Prefix + " " + apiKey.Key
   291  				} else {
   292  					key = apiKey.Key
   293  				}
   294  			{{/vendorExtensions.x-auth-id-alias}}
   295  			{{^vendorExtensions.x-auth-id-alias}}
   296  			if apiKey, ok := auth["{{name}}"]; ok {
   297  				var key string
   298  				if apiKey.Prefix != "" {
   299  					key = apiKey.Prefix + " " + apiKey.Key
   300  				} else {
   301  					key = apiKey.Key
   302  				}
   303  			{{/vendorExtensions.x-auth-id-alias}}
   304  				{{#isKeyInHeader}}
   305  				localVarHeaderParams["{{keyParamName}}"] = key
   306  				{{/isKeyInHeader}}
   307  				{{#isKeyInQuery}}
   308  				localVarQueryParams.Add("{{keyParamName}}", key)
   309  				{{/isKeyInQuery}}
   310  			}
   311  		}
   312  	}
   313  {{/isKeyInCookie}}
   314  {{/isApiKey}}
   315  {{/authMethods}}
   316  	req, err := a.client.prepareRequest(r.Ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFormFileName, localVarFileName, localVarFileBytes)
   317  	if err != nil {
   318  		return {{#returnType}}localVarReturnValue, {{/returnType}}nil, err
   319  	}
   320  
   321  	localVarHTTPResponse, err := a.client.callAPI(req)
   322  	if err != nil || localVarHTTPResponse == nil {
   323  		return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, err
   324  	}
   325  
   326  	localVarBody, err := _ioutil.ReadAll(localVarHTTPResponse.Body)
   327  	localVarHTTPResponse.Body.Close()
   328  	if err != nil {
   329  		return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, err
   330  	}
   331  
   332  	if localVarHTTPResponse.StatusCode >= 300 {
   333  		newErr := GenericOpenAPIError{
   334  			body:  localVarBody,
   335  			error: localVarHTTPResponse.Status,
   336  		}
   337  		{{#responses}}
   338  		{{#dataType}}
   339  		{{^is1xx}}
   340  		{{^is2xx}}
   341  		{{^wildcard}}
   342  		if localVarHTTPResponse.StatusCode == {{{code}}} {
   343  		{{/wildcard}}
   344  			var v {{{dataType}}}
   345  			err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
   346  			if err != nil {
   347  				newErr.error = err.Error()
   348  				return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr
   349  			}
   350  			newErr.model = v
   351  			{{#hasMore}}
   352  			return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr
   353  			{{/hasMore}}
   354  		{{^wildcard}}
   355  		}
   356  		{{/wildcard}}
   357  		{{/is2xx}}
   358  		{{/is1xx}}
   359  		{{/dataType}}
   360  		{{/responses}}
   361  		return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr
   362  	}
   363  
   364  	{{#returnType}}
   365  	err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
   366  	if err != nil {
   367  		newErr := GenericOpenAPIError{
   368  			body:  localVarBody,
   369  			error: err.Error(),
   370  		}
   371  		return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, newErr
   372  	}
   373  
   374  	{{/returnType}}
   375  	return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, nil
   376  }
   377  {{/operation}}
   378  {{/operations}}