forked from loafle/openapi-generator-original
205 lines
6.6 KiB
Go
205 lines
6.6 KiB
Go
package swagger
|
|
|
|
import (
|
|
"strings"
|
|
"fmt"
|
|
"encoding/json"
|
|
"errors"
|
|
"github.com/dghubble/sling"
|
|
)
|
|
|
|
type StoreApi struct {
|
|
basePath string
|
|
}
|
|
|
|
func NewStoreApi() *StoreApi{
|
|
return &StoreApi {
|
|
basePath: "http://petstore.swagger.io/v2",
|
|
}
|
|
}
|
|
|
|
func NewStoreApiWithBasePath(basePath string) *StoreApi{
|
|
return &StoreApi {
|
|
basePath: basePath,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete purchase order by ID
|
|
* For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
|
|
* @param orderId ID of the order that needs to be deleted
|
|
* @return void
|
|
*/
|
|
//func (a StoreApi) DeleteOrder (orderId string) (error) {
|
|
func (a StoreApi) DeleteOrder (orderId string) (error) {
|
|
|
|
_sling := sling.New().Delete(a.basePath)
|
|
|
|
// create path and map variables
|
|
path := "/v2/stores/order/{orderId}"
|
|
path = strings.Replace(path, "{" + "orderId" + "}", fmt.Sprintf("%v", orderId), -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
|
|
}
|
|
/**
|
|
* Find purchase order by ID
|
|
* For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
|
|
* @param orderId ID of pet that needs to be fetched
|
|
* @return Order
|
|
*/
|
|
//func (a StoreApi) GetOrderById (orderId string) (Order, error) {
|
|
func (a StoreApi) GetOrderById (orderId string) (Order, error) {
|
|
|
|
_sling := sling.New().Get(a.basePath)
|
|
|
|
// create path and map variables
|
|
path := "/v2/stores/order/{orderId}"
|
|
path = strings.Replace(path, "{" + "orderId" + "}", fmt.Sprintf("%v", orderId), -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(Order)
|
|
|
|
// 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
|
|
}
|
|
/**
|
|
* Place an order for a pet
|
|
*
|
|
* @param body order placed for purchasing the pet
|
|
* @return Order
|
|
*/
|
|
//func (a StoreApi) PlaceOrder (body Order) (Order, error) {
|
|
func (a StoreApi) PlaceOrder (body Order) (Order, error) {
|
|
|
|
_sling := sling.New().Post(a.basePath)
|
|
|
|
// create path and map variables
|
|
path := "/v2/stores/order"
|
|
|
|
_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)
|
|
|
|
var successPayload = new(Order)
|
|
|
|
// 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
|
|
}
|