forked from loafle/openapi-generator-original
Merge pull request #2567 from guohuang/issue2535
issue #2535, added authentication header for GO
This commit is contained in:
@@ -43,6 +43,32 @@ func (a {{classname}}) {{nickname}} ({{#allParams}}{{paramName}} {{{dataType}}}{
|
||||
|
||||
_sling := sling.New().{{httpMethod}}(a.Configuration.BasePath)
|
||||
|
||||
{{#authMethods}}// authentication ({{name}}) required
|
||||
{{#isApiKey}}{{#isKeyInHeader}}
|
||||
// set key with prefix in header
|
||||
_sling.Set("{{keyParamName}}", a.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"))
|
||||
{{/isKeyInHeader}}{{#isKeyInQuery}}
|
||||
// set key with prefix in querystring
|
||||
{{#hasKeyParamName}} type KeyQueryParams struct {
|
||||
{{keyParamName}} string `url:"{{keyParamName}},omitempty"`
|
||||
}
|
||||
_sling = _sling.QueryStruct(&KeyQueryParams{ {{keyParamName}}: a.Configuration.GetApiKeyWithPrefix("{{keyParamName}}") })
|
||||
{{/hasKeyParamName}}
|
||||
{{/isKeyInQuery}}{{/isApiKey}}
|
||||
{{#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,12 +1,14 @@
|
||||
package {{packageName}}
|
||||
|
||||
import (
|
||||
|
||||
"encoding/base64"
|
||||
)
|
||||
|
||||
type Configuration struct {
|
||||
UserName string `json:"userName,omitempty"`
|
||||
ApiKey string `json:"apiKey,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
ApiKeyPrefix map[string] string `json:"apiKeyPrefix,omitempty"`
|
||||
ApiKey map[string] string `json:"apiKey,omitempty"`
|
||||
Debug bool `json:"debug,omitempty"`
|
||||
DebugFile string `json:"debugFile,omitempty"`
|
||||
OAuthToken string `json:"oAuthToken,omitempty"`
|
||||
@@ -14,19 +16,38 @@ type Configuration struct {
|
||||
BasePath string `json:"basePath,omitempty"`
|
||||
Host string `json:"host,omitempty"`
|
||||
Scheme string `json:"scheme,omitempty"`
|
||||
AccessToken string `json:"accessToken,omitempty"`
|
||||
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
|
||||
}
|
||||
|
||||
func NewConfiguration() *Configuration {
|
||||
defaultHeader := make(map[string]string)
|
||||
apiKey := make(map[string]string)
|
||||
apiKeyPrefix := make(map[string]string)
|
||||
return &Configuration{
|
||||
BasePath: "{{basePath}}",
|
||||
UserName: "",
|
||||
Debug: false,
|
||||
DefaultHeader: defaultHeader,
|
||||
ApiKey: apiKey,
|
||||
ApiKeyPrefix: apiKeyPrefix,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Configuration) GetBasicAuthEncodedString() string {
|
||||
return base64.StdEncoding.EncodeToString([]byte(c.UserName + ":" + c.Password))
|
||||
}
|
||||
|
||||
func (c *Configuration) AddDefaultHeader(key string, value string) {
|
||||
c.DefaultHeader[key] = value
|
||||
}
|
||||
|
||||
func (c *Configuration) GetApiKeyWithPrefix(apiKeyIdentifier string) string {
|
||||
var returnValue = c.ApiKey[apiKeyIdentifier]
|
||||
var apiKeyPrefix = c.ApiKeyPrefix[apiKeyIdentifier]
|
||||
if apiKeyPrefix != ""{
|
||||
returnValue = apiKeyPrefix + " " + returnValue
|
||||
}
|
||||
|
||||
return returnValue
|
||||
}
|
||||
@@ -1,12 +1,14 @@
|
||||
package swagger
|
||||
|
||||
import (
|
||||
|
||||
"encoding/base64"
|
||||
)
|
||||
|
||||
type Configuration struct {
|
||||
UserName string `json:"userName,omitempty"`
|
||||
ApiKey string `json:"apiKey,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
ApiKeyPrefix map[string] string `json:"apiKeyPrefix,omitempty"`
|
||||
ApiKey map[string] string `json:"apiKey,omitempty"`
|
||||
Debug bool `json:"debug,omitempty"`
|
||||
DebugFile string `json:"debugFile,omitempty"`
|
||||
OAuthToken string `json:"oAuthToken,omitempty"`
|
||||
@@ -14,19 +16,38 @@ type Configuration struct {
|
||||
BasePath string `json:"basePath,omitempty"`
|
||||
Host string `json:"host,omitempty"`
|
||||
Scheme string `json:"scheme,omitempty"`
|
||||
AccessToken string `json:"accessToken,omitempty"`
|
||||
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
|
||||
}
|
||||
|
||||
func NewConfiguration() *Configuration {
|
||||
defaultHeader := make(map[string]string)
|
||||
apiKey := make(map[string]string)
|
||||
apiKeyPrefix := make(map[string]string)
|
||||
return &Configuration{
|
||||
BasePath: "http://petstore.swagger.io/v2",
|
||||
UserName: "",
|
||||
Debug: false,
|
||||
DefaultHeader: defaultHeader,
|
||||
ApiKey: apiKey,
|
||||
ApiKeyPrefix: apiKeyPrefix,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Configuration) GetBasicAuthEncodedString() string {
|
||||
return base64.StdEncoding.EncodeToString([]byte(c.UserName + ":" + c.Password))
|
||||
}
|
||||
|
||||
func (c *Configuration) AddDefaultHeader(key string, value string) {
|
||||
c.DefaultHeader[key] = value
|
||||
}
|
||||
|
||||
func (c *Configuration) GetApiKeyWithPrefix(apiKeyIdentifier string) string {
|
||||
var returnValue = c.ApiKey[apiKeyIdentifier]
|
||||
var apiKeyPrefix = c.ApiKeyPrefix[apiKeyIdentifier]
|
||||
if apiKeyPrefix != ""{
|
||||
returnValue = apiKeyPrefix + " " + returnValue
|
||||
}
|
||||
|
||||
return returnValue
|
||||
}
|
||||
@@ -40,6 +40,13 @@ func (a PetApi) AddPet (body Pet) (error) {
|
||||
|
||||
_sling := sling.New().Post(a.Configuration.BasePath)
|
||||
|
||||
// authentication (petstore_auth) required
|
||||
|
||||
// oauth required
|
||||
if a.Configuration.AccessToken != ""{
|
||||
_sling.Set("Authorization", "Bearer " + a.Configuration.AccessToken)
|
||||
}
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/pet"
|
||||
|
||||
@@ -106,6 +113,13 @@ func (a PetApi) DeletePet (petId int64, apiKey string) (error) {
|
||||
|
||||
_sling := sling.New().Delete(a.Configuration.BasePath)
|
||||
|
||||
// authentication (petstore_auth) required
|
||||
|
||||
// oauth required
|
||||
if a.Configuration.AccessToken != ""{
|
||||
_sling.Set("Authorization", "Bearer " + a.Configuration.AccessToken)
|
||||
}
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/pet/{petId}"
|
||||
path = strings.Replace(path, "{" + "petId" + "}", fmt.Sprintf("%v", petId), -1)
|
||||
@@ -172,6 +186,13 @@ func (a PetApi) FindPetsByStatus (status []string) ([]Pet, error) {
|
||||
|
||||
_sling := sling.New().Get(a.Configuration.BasePath)
|
||||
|
||||
// authentication (petstore_auth) required
|
||||
|
||||
// oauth required
|
||||
if a.Configuration.AccessToken != ""{
|
||||
_sling.Set("Authorization", "Bearer " + a.Configuration.AccessToken)
|
||||
}
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/pet/findByStatus"
|
||||
|
||||
@@ -239,6 +260,13 @@ func (a PetApi) FindPetsByTags (tags []string) ([]Pet, error) {
|
||||
|
||||
_sling := sling.New().Get(a.Configuration.BasePath)
|
||||
|
||||
// authentication (petstore_auth) required
|
||||
|
||||
// oauth required
|
||||
if a.Configuration.AccessToken != ""{
|
||||
_sling.Set("Authorization", "Bearer " + a.Configuration.AccessToken)
|
||||
}
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/pet/findByTags"
|
||||
|
||||
@@ -306,6 +334,12 @@ func (a PetApi) GetPetById (petId int64) (Pet, error) {
|
||||
|
||||
_sling := sling.New().Get(a.Configuration.BasePath)
|
||||
|
||||
// authentication (api_key) required
|
||||
|
||||
// set key with prefix in header
|
||||
_sling.Set("api_key", a.Configuration.GetApiKeyWithPrefix("api_key")
|
||||
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/pet/{petId}"
|
||||
path = strings.Replace(path, "{" + "petId" + "}", fmt.Sprintf("%v", petId), -1)
|
||||
@@ -370,6 +404,13 @@ func (a PetApi) UpdatePet (body Pet) (error) {
|
||||
|
||||
_sling := sling.New().Put(a.Configuration.BasePath)
|
||||
|
||||
// authentication (petstore_auth) required
|
||||
|
||||
// oauth required
|
||||
if a.Configuration.AccessToken != ""{
|
||||
_sling.Set("Authorization", "Bearer " + a.Configuration.AccessToken)
|
||||
}
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/pet"
|
||||
|
||||
@@ -437,6 +478,13 @@ func (a PetApi) UpdatePetWithForm (petId int64, name string, status string) (err
|
||||
|
||||
_sling := sling.New().Post(a.Configuration.BasePath)
|
||||
|
||||
// authentication (petstore_auth) required
|
||||
|
||||
// oauth required
|
||||
if a.Configuration.AccessToken != ""{
|
||||
_sling.Set("Authorization", "Bearer " + a.Configuration.AccessToken)
|
||||
}
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/pet/{petId}"
|
||||
path = strings.Replace(path, "{" + "petId" + "}", fmt.Sprintf("%v", petId), -1)
|
||||
@@ -508,6 +556,13 @@ func (a PetApi) UploadFile (petId int64, additionalMetadata string, file *os.Fil
|
||||
|
||||
_sling := sling.New().Post(a.Configuration.BasePath)
|
||||
|
||||
// authentication (petstore_auth) required
|
||||
|
||||
// oauth required
|
||||
if a.Configuration.AccessToken != ""{
|
||||
_sling.Set("Authorization", "Bearer " + a.Configuration.AccessToken)
|
||||
}
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/pet/{petId}/uploadImage"
|
||||
path = strings.Replace(path, "{" + "petId" + "}", fmt.Sprintf("%v", petId), -1)
|
||||
|
||||
@@ -39,6 +39,7 @@ func (a StoreApi) DeleteOrder (orderId string) (error) {
|
||||
|
||||
_sling := sling.New().Delete(a.Configuration.BasePath)
|
||||
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/store/order/{orderId}"
|
||||
path = strings.Replace(path, "{" + "orderId" + "}", fmt.Sprintf("%v", orderId), -1)
|
||||
@@ -102,6 +103,12 @@ func (a StoreApi) GetInventory () (map[string]int32, error) {
|
||||
|
||||
_sling := sling.New().Get(a.Configuration.BasePath)
|
||||
|
||||
// authentication (api_key) required
|
||||
|
||||
// set key with prefix in header
|
||||
_sling.Set("api_key", a.Configuration.GetApiKeyWithPrefix("api_key")
|
||||
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/store/inventory"
|
||||
|
||||
@@ -165,6 +172,7 @@ func (a StoreApi) GetOrderById (orderId int64) (Order, error) {
|
||||
|
||||
_sling := sling.New().Get(a.Configuration.BasePath)
|
||||
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/store/order/{orderId}"
|
||||
path = strings.Replace(path, "{" + "orderId" + "}", fmt.Sprintf("%v", orderId), -1)
|
||||
@@ -229,6 +237,7 @@ func (a StoreApi) PlaceOrder (body Order) (Order, error) {
|
||||
|
||||
_sling := sling.New().Post(a.Configuration.BasePath)
|
||||
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/store/order"
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@ func (a UserApi) CreateUser (body User) (error) {
|
||||
|
||||
_sling := sling.New().Post(a.Configuration.BasePath)
|
||||
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/user"
|
||||
|
||||
@@ -104,6 +105,7 @@ func (a UserApi) CreateUsersWithArrayInput (body []User) (error) {
|
||||
|
||||
_sling := sling.New().Post(a.Configuration.BasePath)
|
||||
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/user/createWithArray"
|
||||
|
||||
@@ -169,6 +171,7 @@ func (a UserApi) CreateUsersWithListInput (body []User) (error) {
|
||||
|
||||
_sling := sling.New().Post(a.Configuration.BasePath)
|
||||
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/user/createWithList"
|
||||
|
||||
@@ -234,6 +237,7 @@ func (a UserApi) DeleteUser (username string) (error) {
|
||||
|
||||
_sling := sling.New().Delete(a.Configuration.BasePath)
|
||||
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/user/{username}"
|
||||
path = strings.Replace(path, "{" + "username" + "}", fmt.Sprintf("%v", username), -1)
|
||||
@@ -298,6 +302,7 @@ func (a UserApi) GetUserByName (username string) (User, error) {
|
||||
|
||||
_sling := sling.New().Get(a.Configuration.BasePath)
|
||||
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/user/{username}"
|
||||
path = strings.Replace(path, "{" + "username" + "}", fmt.Sprintf("%v", username), -1)
|
||||
@@ -363,6 +368,7 @@ func (a UserApi) LoginUser (username string, password string) (string, error) {
|
||||
|
||||
_sling := sling.New().Get(a.Configuration.BasePath)
|
||||
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/user/login"
|
||||
|
||||
@@ -430,6 +436,7 @@ func (a UserApi) LogoutUser () (error) {
|
||||
|
||||
_sling := sling.New().Get(a.Configuration.BasePath)
|
||||
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/user/logout"
|
||||
|
||||
@@ -494,6 +501,7 @@ func (a UserApi) UpdateUser (username string, body User) (error) {
|
||||
|
||||
_sling := sling.New().Put(a.Configuration.BasePath)
|
||||
|
||||
|
||||
// create path and map variables
|
||||
path := "/v2/user/{username}"
|
||||
path = strings.Replace(path, "{" + "username" + "}", fmt.Sprintf("%v", username), -1)
|
||||
|
||||
Reference in New Issue
Block a user