mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-06-11 03:10:52 +00:00
509 lines
16 KiB
Go
509 lines
16 KiB
Go
package swagger
|
|
|
|
import (
|
|
"strings"
|
|
"fmt"
|
|
"encoding/json"
|
|
"errors"
|
|
"github.com/dghubble/sling"
|
|
)
|
|
|
|
type UserApi struct {
|
|
basePath string
|
|
}
|
|
|
|
func NewUserApi() *UserApi{
|
|
return &UserApi {
|
|
basePath: "http://petstore.swagger.io/v2",
|
|
}
|
|
}
|
|
|
|
func NewUserApiWithBasePath(basePath string) *UserApi{
|
|
return &UserApi {
|
|
basePath: basePath,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create user
|
|
* This can only be done by the logged in user.
|
|
* @param body Created user object
|
|
* @return void
|
|
*/
|
|
//func (a UserApi) CreateUser (body User) (error) {
|
|
func (a UserApi) CreateUser (body User) (error) {
|
|
|
|
_sling := sling.New().Post(a.basePath)
|
|
|
|
// create path and map variables
|
|
path := "/v2/users"
|
|
|
|
_sling = _sling.Path(path)
|
|
|
|
// accept header
|
|
accepts := []string { "application/json", "application/xml" }
|
|
for key := range accepts {
|
|
_sling = _sling.Set("Accept", accepts[key])
|
|
break // only use the first Accept
|
|
}
|
|
|
|
// body params
|
|
_sling = _sling.BodyJSON(body)
|
|
|
|
|
|
|
|
// We use this map (below) so that any arbitrary error JSON can be handled.
|
|
// FIXME: This is in the absence of this Go generator honoring the non-2xx
|
|
// response (error) models, which needs to be implemented at some point.
|
|
var failurePayload map[string]interface{}
|
|
|
|
httpResponse, err := _sling.Receive(nil, &failurePayload)
|
|
|
|
if err == nil {
|
|
// err == nil only means that there wasn't a sub-application-layer error (e.g. no network error)
|
|
if failurePayload != nil {
|
|
// If the failurePayload is present, there likely was some kind of non-2xx status
|
|
// returned (and a JSON payload error present)
|
|
var str []byte
|
|
str, err = json.Marshal(failurePayload)
|
|
if err == nil { // For safety, check for an error marshalling... probably superfluous
|
|
// This will return the JSON error body as a string
|
|
err = errors.New(string(str))
|
|
}
|
|
} else {
|
|
// So, there was no network-type error, and nothing in the failure payload,
|
|
// but we should still check the status code
|
|
if httpResponse == nil {
|
|
// This should never happen...
|
|
err = errors.New("No HTTP Response received.")
|
|
} else if code := httpResponse.StatusCode; 200 > code || code > 299 {
|
|
err = errors.New("HTTP Error: " + string(httpResponse.StatusCode))
|
|
}
|
|
}
|
|
}
|
|
|
|
return err
|
|
}
|
|
/**
|
|
* Creates list of users with given input array
|
|
*
|
|
* @param body List of user object
|
|
* @return void
|
|
*/
|
|
//func (a UserApi) CreateUsersWithArrayInput (body []User) (error) {
|
|
func (a UserApi) CreateUsersWithArrayInput (body []User) (error) {
|
|
|
|
_sling := sling.New().Post(a.basePath)
|
|
|
|
// create path and map variables
|
|
path := "/v2/users/createWithArray"
|
|
|
|
_sling = _sling.Path(path)
|
|
|
|
// accept header
|
|
accepts := []string { "application/json", "application/xml" }
|
|
for key := range accepts {
|
|
_sling = _sling.Set("Accept", accepts[key])
|
|
break // only use the first Accept
|
|
}
|
|
|
|
// body params
|
|
_sling = _sling.BodyJSON(body)
|
|
|
|
|
|
|
|
// We use this map (below) so that any arbitrary error JSON can be handled.
|
|
// FIXME: This is in the absence of this Go generator honoring the non-2xx
|
|
// response (error) models, which needs to be implemented at some point.
|
|
var failurePayload map[string]interface{}
|
|
|
|
httpResponse, err := _sling.Receive(nil, &failurePayload)
|
|
|
|
if err == nil {
|
|
// err == nil only means that there wasn't a sub-application-layer error (e.g. no network error)
|
|
if failurePayload != nil {
|
|
// If the failurePayload is present, there likely was some kind of non-2xx status
|
|
// returned (and a JSON payload error present)
|
|
var str []byte
|
|
str, err = json.Marshal(failurePayload)
|
|
if err == nil { // For safety, check for an error marshalling... probably superfluous
|
|
// This will return the JSON error body as a string
|
|
err = errors.New(string(str))
|
|
}
|
|
} else {
|
|
// So, there was no network-type error, and nothing in the failure payload,
|
|
// but we should still check the status code
|
|
if httpResponse == nil {
|
|
// This should never happen...
|
|
err = errors.New("No HTTP Response received.")
|
|
} else if code := httpResponse.StatusCode; 200 > code || code > 299 {
|
|
err = errors.New("HTTP Error: " + string(httpResponse.StatusCode))
|
|
}
|
|
}
|
|
}
|
|
|
|
return err
|
|
}
|
|
/**
|
|
* Creates list of users with given input array
|
|
*
|
|
* @param body List of user object
|
|
* @return void
|
|
*/
|
|
//func (a UserApi) CreateUsersWithListInput (body []User) (error) {
|
|
func (a UserApi) CreateUsersWithListInput (body []User) (error) {
|
|
|
|
_sling := sling.New().Post(a.basePath)
|
|
|
|
// create path and map variables
|
|
path := "/v2/users/createWithList"
|
|
|
|
_sling = _sling.Path(path)
|
|
|
|
// accept header
|
|
accepts := []string { "application/json", "application/xml" }
|
|
for key := range accepts {
|
|
_sling = _sling.Set("Accept", accepts[key])
|
|
break // only use the first Accept
|
|
}
|
|
|
|
// body params
|
|
_sling = _sling.BodyJSON(body)
|
|
|
|
|
|
|
|
// We use this map (below) so that any arbitrary error JSON can be handled.
|
|
// FIXME: This is in the absence of this Go generator honoring the non-2xx
|
|
// response (error) models, which needs to be implemented at some point.
|
|
var failurePayload map[string]interface{}
|
|
|
|
httpResponse, err := _sling.Receive(nil, &failurePayload)
|
|
|
|
if err == nil {
|
|
// err == nil only means that there wasn't a sub-application-layer error (e.g. no network error)
|
|
if failurePayload != nil {
|
|
// If the failurePayload is present, there likely was some kind of non-2xx status
|
|
// returned (and a JSON payload error present)
|
|
var str []byte
|
|
str, err = json.Marshal(failurePayload)
|
|
if err == nil { // For safety, check for an error marshalling... probably superfluous
|
|
// This will return the JSON error body as a string
|
|
err = errors.New(string(str))
|
|
}
|
|
} else {
|
|
// So, there was no network-type error, and nothing in the failure payload,
|
|
// but we should still check the status code
|
|
if httpResponse == nil {
|
|
// This should never happen...
|
|
err = errors.New("No HTTP Response received.")
|
|
} else if code := httpResponse.StatusCode; 200 > code || code > 299 {
|
|
err = errors.New("HTTP Error: " + string(httpResponse.StatusCode))
|
|
}
|
|
}
|
|
}
|
|
|
|
return err
|
|
}
|
|
/**
|
|
* Delete user
|
|
* This can only be done by the logged in user.
|
|
* @param username The name that needs to be deleted
|
|
* @return void
|
|
*/
|
|
//func (a UserApi) DeleteUser (username string) (error) {
|
|
func (a UserApi) DeleteUser (username string) (error) {
|
|
|
|
_sling := sling.New().Delete(a.basePath)
|
|
|
|
// create path and map variables
|
|
path := "/v2/users/{username}"
|
|
path = strings.Replace(path, "{" + "username" + "}", fmt.Sprintf("%v", username), -1)
|
|
|
|
_sling = _sling.Path(path)
|
|
|
|
// accept header
|
|
accepts := []string { "application/json", "application/xml" }
|
|
for key := range accepts {
|
|
_sling = _sling.Set("Accept", accepts[key])
|
|
break // only use the first Accept
|
|
}
|
|
|
|
|
|
|
|
|
|
// We use this map (below) so that any arbitrary error JSON can be handled.
|
|
// FIXME: This is in the absence of this Go generator honoring the non-2xx
|
|
// response (error) models, which needs to be implemented at some point.
|
|
var failurePayload map[string]interface{}
|
|
|
|
httpResponse, err := _sling.Receive(nil, &failurePayload)
|
|
|
|
if err == nil {
|
|
// err == nil only means that there wasn't a sub-application-layer error (e.g. no network error)
|
|
if failurePayload != nil {
|
|
// If the failurePayload is present, there likely was some kind of non-2xx status
|
|
// returned (and a JSON payload error present)
|
|
var str []byte
|
|
str, err = json.Marshal(failurePayload)
|
|
if err == nil { // For safety, check for an error marshalling... probably superfluous
|
|
// This will return the JSON error body as a string
|
|
err = errors.New(string(str))
|
|
}
|
|
} else {
|
|
// So, there was no network-type error, and nothing in the failure payload,
|
|
// but we should still check the status code
|
|
if httpResponse == nil {
|
|
// This should never happen...
|
|
err = errors.New("No HTTP Response received.")
|
|
} else if code := httpResponse.StatusCode; 200 > code || code > 299 {
|
|
err = errors.New("HTTP Error: " + string(httpResponse.StatusCode))
|
|
}
|
|
}
|
|
}
|
|
|
|
return err
|
|
}
|
|
/**
|
|
* Get user by user name
|
|
*
|
|
* @param username The name that needs to be fetched. Use user1 for testing.
|
|
* @return User
|
|
*/
|
|
//func (a UserApi) GetUserByName (username string) (User, error) {
|
|
func (a UserApi) GetUserByName (username string) (User, error) {
|
|
|
|
_sling := sling.New().Get(a.basePath)
|
|
|
|
// create path and map variables
|
|
path := "/v2/users/{username}"
|
|
path = strings.Replace(path, "{" + "username" + "}", fmt.Sprintf("%v", username), -1)
|
|
|
|
_sling = _sling.Path(path)
|
|
|
|
// accept header
|
|
accepts := []string { "application/json", "application/xml" }
|
|
for key := range accepts {
|
|
_sling = _sling.Set("Accept", accepts[key])
|
|
break // only use the first Accept
|
|
}
|
|
|
|
|
|
var successPayload = new(User)
|
|
|
|
// We use this map (below) so that any arbitrary error JSON can be handled.
|
|
// FIXME: This is in the absence of this Go generator honoring the non-2xx
|
|
// response (error) models, which needs to be implemented at some point.
|
|
var failurePayload map[string]interface{}
|
|
|
|
httpResponse, err := _sling.Receive(successPayload, &failurePayload)
|
|
|
|
if err == nil {
|
|
// err == nil only means that there wasn't a sub-application-layer error (e.g. no network error)
|
|
if failurePayload != nil {
|
|
// If the failurePayload is present, there likely was some kind of non-2xx status
|
|
// returned (and a JSON payload error present)
|
|
var str []byte
|
|
str, err = json.Marshal(failurePayload)
|
|
if err == nil { // For safety, check for an error marshalling... probably superfluous
|
|
// This will return the JSON error body as a string
|
|
err = errors.New(string(str))
|
|
}
|
|
} else {
|
|
// So, there was no network-type error, and nothing in the failure payload,
|
|
// but we should still check the status code
|
|
if httpResponse == nil {
|
|
// This should never happen...
|
|
err = errors.New("No HTTP Response received.")
|
|
} else if code := httpResponse.StatusCode; 200 > code || code > 299 {
|
|
err = errors.New("HTTP Error: " + string(httpResponse.StatusCode))
|
|
}
|
|
}
|
|
}
|
|
|
|
return *successPayload, err
|
|
}
|
|
/**
|
|
* Logs user into the system
|
|
*
|
|
* @param username The user name for login
|
|
* @param password The password for login in clear text
|
|
* @return string
|
|
*/
|
|
//func (a UserApi) LoginUser (username string, password string) (string, error) {
|
|
func (a UserApi) LoginUser (username string, password string) (string, error) {
|
|
|
|
_sling := sling.New().Get(a.basePath)
|
|
|
|
// create path and map variables
|
|
path := "/v2/users/login"
|
|
|
|
_sling = _sling.Path(path)
|
|
|
|
type QueryParams struct {
|
|
username string `url:"username,omitempty"`
|
|
password string `url:"password,omitempty"`
|
|
|
|
}
|
|
_sling = _sling.QueryStruct(&QueryParams{ username: username,password: password })
|
|
// accept header
|
|
accepts := []string { "application/json", "application/xml" }
|
|
for key := range accepts {
|
|
_sling = _sling.Set("Accept", accepts[key])
|
|
break // only use the first Accept
|
|
}
|
|
|
|
|
|
var successPayload = new(string)
|
|
|
|
// We use this map (below) so that any arbitrary error JSON can be handled.
|
|
// FIXME: This is in the absence of this Go generator honoring the non-2xx
|
|
// response (error) models, which needs to be implemented at some point.
|
|
var failurePayload map[string]interface{}
|
|
|
|
httpResponse, err := _sling.Receive(successPayload, &failurePayload)
|
|
|
|
if err == nil {
|
|
// err == nil only means that there wasn't a sub-application-layer error (e.g. no network error)
|
|
if failurePayload != nil {
|
|
// If the failurePayload is present, there likely was some kind of non-2xx status
|
|
// returned (and a JSON payload error present)
|
|
var str []byte
|
|
str, err = json.Marshal(failurePayload)
|
|
if err == nil { // For safety, check for an error marshalling... probably superfluous
|
|
// This will return the JSON error body as a string
|
|
err = errors.New(string(str))
|
|
}
|
|
} else {
|
|
// So, there was no network-type error, and nothing in the failure payload,
|
|
// but we should still check the status code
|
|
if httpResponse == nil {
|
|
// This should never happen...
|
|
err = errors.New("No HTTP Response received.")
|
|
} else if code := httpResponse.StatusCode; 200 > code || code > 299 {
|
|
err = errors.New("HTTP Error: " + string(httpResponse.StatusCode))
|
|
}
|
|
}
|
|
}
|
|
|
|
return *successPayload, err
|
|
}
|
|
/**
|
|
* Logs out current logged in user session
|
|
*
|
|
* @return void
|
|
*/
|
|
//func (a UserApi) LogoutUser () (error) {
|
|
func (a UserApi) LogoutUser () (error) {
|
|
|
|
_sling := sling.New().Get(a.basePath)
|
|
|
|
// create path and map variables
|
|
path := "/v2/users/logout"
|
|
|
|
_sling = _sling.Path(path)
|
|
|
|
// accept header
|
|
accepts := []string { "application/json", "application/xml" }
|
|
for key := range accepts {
|
|
_sling = _sling.Set("Accept", accepts[key])
|
|
break // only use the first Accept
|
|
}
|
|
|
|
|
|
|
|
|
|
// We use this map (below) so that any arbitrary error JSON can be handled.
|
|
// FIXME: This is in the absence of this Go generator honoring the non-2xx
|
|
// response (error) models, which needs to be implemented at some point.
|
|
var failurePayload map[string]interface{}
|
|
|
|
httpResponse, err := _sling.Receive(nil, &failurePayload)
|
|
|
|
if err == nil {
|
|
// err == nil only means that there wasn't a sub-application-layer error (e.g. no network error)
|
|
if failurePayload != nil {
|
|
// If the failurePayload is present, there likely was some kind of non-2xx status
|
|
// returned (and a JSON payload error present)
|
|
var str []byte
|
|
str, err = json.Marshal(failurePayload)
|
|
if err == nil { // For safety, check for an error marshalling... probably superfluous
|
|
// This will return the JSON error body as a string
|
|
err = errors.New(string(str))
|
|
}
|
|
} else {
|
|
// So, there was no network-type error, and nothing in the failure payload,
|
|
// but we should still check the status code
|
|
if httpResponse == nil {
|
|
// This should never happen...
|
|
err = errors.New("No HTTP Response received.")
|
|
} else if code := httpResponse.StatusCode; 200 > code || code > 299 {
|
|
err = errors.New("HTTP Error: " + string(httpResponse.StatusCode))
|
|
}
|
|
}
|
|
}
|
|
|
|
return err
|
|
}
|
|
/**
|
|
* Updated user
|
|
* This can only be done by the logged in user.
|
|
* @param username name that need to be deleted
|
|
* @param body Updated user object
|
|
* @return void
|
|
*/
|
|
//func (a UserApi) UpdateUser (username string, body User) (error) {
|
|
func (a UserApi) UpdateUser (username string, body User) (error) {
|
|
|
|
_sling := sling.New().Put(a.basePath)
|
|
|
|
// create path and map variables
|
|
path := "/v2/users/{username}"
|
|
path = strings.Replace(path, "{" + "username" + "}", fmt.Sprintf("%v", username), -1)
|
|
|
|
_sling = _sling.Path(path)
|
|
|
|
// accept header
|
|
accepts := []string { "application/json", "application/xml" }
|
|
for key := range accepts {
|
|
_sling = _sling.Set("Accept", accepts[key])
|
|
break // only use the first Accept
|
|
}
|
|
|
|
// body params
|
|
_sling = _sling.BodyJSON(body)
|
|
|
|
|
|
|
|
// We use this map (below) so that any arbitrary error JSON can be handled.
|
|
// FIXME: This is in the absence of this Go generator honoring the non-2xx
|
|
// response (error) models, which needs to be implemented at some point.
|
|
var failurePayload map[string]interface{}
|
|
|
|
httpResponse, err := _sling.Receive(nil, &failurePayload)
|
|
|
|
if err == nil {
|
|
// err == nil only means that there wasn't a sub-application-layer error (e.g. no network error)
|
|
if failurePayload != nil {
|
|
// If the failurePayload is present, there likely was some kind of non-2xx status
|
|
// returned (and a JSON payload error present)
|
|
var str []byte
|
|
str, err = json.Marshal(failurePayload)
|
|
if err == nil { // For safety, check for an error marshalling... probably superfluous
|
|
// This will return the JSON error body as a string
|
|
err = errors.New(string(str))
|
|
}
|
|
} else {
|
|
// So, there was no network-type error, and nothing in the failure payload,
|
|
// but we should still check the status code
|
|
if httpResponse == nil {
|
|
// This should never happen...
|
|
err = errors.New("No HTTP Response received.")
|
|
} else if code := httpResponse.StatusCode; 200 > code || code > 299 {
|
|
err = errors.New("HTTP Error: " + string(httpResponse.StatusCode))
|
|
}
|
|
}
|
|
}
|
|
|
|
return err
|
|
}
|