forked from loafle/openapi-generator-original
issue #2535, added authentication header for GO
This commit is contained in:
@@ -43,6 +43,21 @@ func (a {{classname}}) {{nickname}} ({{#allParams}}{{paramName}} {{{dataType}}}{
|
||||
|
||||
_sling := sling.New().{{httpMethod}}(a.Configuration.BasePath)
|
||||
|
||||
{{#authMethods}}// authentication ({{name}}) required
|
||||
{{#isBasic}}
|
||||
// http basic authentication required
|
||||
if a.Configuration.Username != "" || a.Configuration.Password != ""{
|
||||
_sling.Set("Authorization", "Basic " + a.Configuration.GetBasicAuthEncodedString())
|
||||
}
|
||||
{{/isBasic}}
|
||||
{{#isOAuth}}
|
||||
// oauth required
|
||||
if a.Configuration.AccessToken != ""{
|
||||
_sling.Set("Authorization", "Bearer " + a.Configuration.AccessToken)
|
||||
}
|
||||
{{/isOAuth}}
|
||||
{{/authMethods}}
|
||||
|
||||
// create path and map variables
|
||||
path := "{{basePathWithoutHost}}{{path}}"
|
||||
{{#pathParams}} path = strings.Replace(path, "{" + "{{baseName}}" + "}", fmt.Sprintf("%v", {{paramName}}), -1)
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package {{packageName}}
|
||||
|
||||
import (
|
||||
|
||||
"encoding/base64"
|
||||
)
|
||||
|
||||
type Configuration struct {
|
||||
UserName string `json:"userName,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
ApiKey string `json:"apiKey,omitempty"`
|
||||
Debug bool `json:"debug,omitempty"`
|
||||
DebugFile string `json:"debugFile,omitempty"`
|
||||
@@ -14,6 +15,7 @@ type Configuration struct {
|
||||
BasePath string `json:"basePath,omitempty"`
|
||||
Host string `json:"host,omitempty"`
|
||||
Scheme string `json:"scheme,omitempty"`
|
||||
AccessToken string `json:"accessToken,omitempty"`
|
||||
}
|
||||
|
||||
func NewConfiguration() *Configuration {
|
||||
@@ -22,4 +24,8 @@ func NewConfiguration() *Configuration {
|
||||
UserName: "",
|
||||
Debug: false,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Configuration) GetBasicAuthEncodedString() string {
|
||||
return base64.StdEncoding.EncodeToString([]byte(c.UserName + ":" + c.Password))
|
||||
}
|
||||
Reference in New Issue
Block a user