forked from loafle/openapi-generator-original
issue#2543: added api_client to set content-type and accept for http request
This commit is contained in:
@@ -134,6 +134,7 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
|
||||
supportingFiles.add(new SupportingFile("gitignore.mustache", "", ".gitignore"));
|
||||
supportingFiles.add(new SupportingFile("configuration.mustache", packageName, "configuration.go"));
|
||||
supportingFiles.add(new SupportingFile("api_client.mustache", packageName, "api_client.go"));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -86,11 +86,29 @@ func (a {{classname}}) {{nickname}} ({{#allParams}}{{paramName}} {{{dataType}}}{
|
||||
}
|
||||
_sling = _sling.QueryStruct(&QueryParams{ {{#queryParams}}{{vendorExtensions.x-exportParamName}}: {{paramName}}{{#hasMore}},{{/hasMore}}{{/queryParams}} })
|
||||
{{/hasQueryParams}}
|
||||
// accept header
|
||||
accepts := []string { {{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }
|
||||
for key := range accepts {
|
||||
_sling = _sling.Set("Accept", accepts[key])
|
||||
break // only use the first Accept
|
||||
|
||||
// to determine the Content-Type header
|
||||
localVarHttpContentTypes := []string {
|
||||
{{#consumes}}
|
||||
"{{mediaType}}",
|
||||
{{/consumes}}
|
||||
}
|
||||
//set Content-Type header
|
||||
localVarHttpContentType := a.Configuration.ApiClient.SelectHeaderContentType(localVarHttpContentTypes)
|
||||
if localVarHttpContentType != "" {
|
||||
_sling = _sling.Set("Content-Type", localVarHttpContentType)
|
||||
}
|
||||
|
||||
// to determine the Accept header
|
||||
localVarHttpHeaderAccepts := []string {
|
||||
{{#produces}}
|
||||
"{{mediaType}}",
|
||||
{{/produces}}
|
||||
}
|
||||
//set Accept header
|
||||
localVarHttpHeaderAccept := a.Configuration.ApiClient.SelectHeaderAccept(localVarHttpHeaderAccepts)
|
||||
if localVarHttpHeaderAccept != "" {
|
||||
_sling = _sling.Set("Accept", localVarHttpHeaderAccept)
|
||||
}
|
||||
{{#hasHeaderParams}}{{#headerParams}} // header params "{{baseName}}"
|
||||
_sling = _sling.Set("{{baseName}}", {{paramName}})
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package {{packageName}}
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ApiClient struct {
|
||||
|
||||
}
|
||||
|
||||
func (c *ApiClient) 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'
|
||||
}
|
||||
|
||||
func (c *ApiClient) SelectHeaderAccept(accepts []string) string {
|
||||
if (len(accepts) == 0){
|
||||
return ""
|
||||
}
|
||||
|
||||
if contains(accepts,"application/json"){
|
||||
return "application/json"
|
||||
}
|
||||
|
||||
return strings.Join(accepts,",")
|
||||
}
|
||||
|
||||
func contains(source []string, containvalue string) bool {
|
||||
for _, a := range source {
|
||||
if strings.ToLower(a) == strings.ToLower(containvalue) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -19,6 +19,7 @@ type Configuration struct {
|
||||
AccessToken string `json:"accessToken,omitempty"`
|
||||
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
|
||||
UserAgent string `json:"userAgent,omitempty"`
|
||||
ApiClient ApiClient `json:"apiClient,omitempty"`
|
||||
}
|
||||
|
||||
func NewConfiguration() *Configuration {
|
||||
|
||||
Reference in New Issue
Block a user