package swagger import ( "strings" "fmt" "encoding/json" "errors" "github.com/dghubble/sling" "os" ) type PetApi struct { basePath string } func NewPetApi() *PetApi{ return &PetApi { basePath: "http://petstore.swagger.io/v2", } } func NewPetApiWithBasePath(basePath string) *PetApi{ return &PetApi { basePath: basePath, } } /** * Add a new pet to the store * * @param body Pet object that needs to be added to the store * @return void */ //func (a PetApi) AddPet (body Pet) (error) { func (a PetApi) AddPet (body Pet) (error) { _sling := sling.New().Post(a.basePath) // create path and map variables path := "/v2/pet" _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 } /** * Fake endpoint to test byte array in body parameter for adding a new pet to the store * * @param body Pet object in the form of byte array * @return void */ //func (a PetApi) AddPetUsingByteArray (body string) (error) { func (a PetApi) AddPetUsingByteArray (body string) (error) { _sling := sling.New().Post(a.basePath) // create path and map variables path := "/v2/pet?testing_byte_array=true" _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 } /** * Deletes a pet * * @param petId Pet id to delete * @param apiKey * @return void */ //func (a PetApi) DeletePet (petId int64, apiKey string) (error) { func (a PetApi) DeletePet (petId int64, apiKey string) (error) { _sling := sling.New().Delete(a.basePath) // create path and map variables path := "/v2/pet/{petId}" path = strings.Replace(path, "{" + "petId" + "}", fmt.Sprintf("%v", petId), -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 } // header params "api_key" _sling = _sling.Set("api_key", apiKey) // 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 } /** * Finds Pets by status * Multiple status values can be provided with comma separated strings * @param status Status values that need to be considered for query * @return []Pet */ //func (a PetApi) FindPetsByStatus (status []string) ([]Pet, error) { func (a PetApi) FindPetsByStatus (status []string) ([]Pet, error) { _sling := sling.New().Get(a.basePath) // create path and map variables path := "/v2/pet/findByStatus" _sling = _sling.Path(path) type QueryParams struct { status []string `url:"status,omitempty"` } _sling = _sling.QueryStruct(&QueryParams{ status: status }) // 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([]Pet) // 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 } /** * Finds Pets by tags * Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing. * @param tags Tags to filter by * @return []Pet */ //func (a PetApi) FindPetsByTags (tags []string) ([]Pet, error) { func (a PetApi) FindPetsByTags (tags []string) ([]Pet, error) { _sling := sling.New().Get(a.basePath) // create path and map variables path := "/v2/pet/findByTags" _sling = _sling.Path(path) type QueryParams struct { tags []string `url:"tags,omitempty"` } _sling = _sling.QueryStruct(&QueryParams{ tags: tags }) // 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([]Pet) // 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 } /** * Find pet by ID * Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions * @param petId ID of pet that needs to be fetched * @return Pet */ //func (a PetApi) GetPetById (petId int64) (Pet, error) { func (a PetApi) GetPetById (petId int64) (Pet, error) { _sling := sling.New().Get(a.basePath) // create path and map variables path := "/v2/pet/{petId}" path = strings.Replace(path, "{" + "petId" + "}", fmt.Sprintf("%v", petId), -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(Pet) // 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 } /** * Fake endpoint to test inline arbitrary object return by 'Find pet by ID' * Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions * @param petId ID of pet that needs to be fetched * @return InlineResponse200 */ //func (a PetApi) GetPetByIdInObject (petId int64) (InlineResponse200, error) { func (a PetApi) GetPetByIdInObject (petId int64) (InlineResponse200, error) { _sling := sling.New().Get(a.basePath) // create path and map variables path := "/v2/pet/{petId}?response=inline_arbitrary_object" path = strings.Replace(path, "{" + "petId" + "}", fmt.Sprintf("%v", petId), -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(InlineResponse200) // 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 } /** * Fake endpoint to test byte array return by 'Find pet by ID' * Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions * @param petId ID of pet that needs to be fetched * @return string */ //func (a PetApi) PetPetIdtestingByteArraytrueGet (petId int64) (string, error) { func (a PetApi) PetPetIdtestingByteArraytrueGet (petId int64) (string, error) { _sling := sling.New().Get(a.basePath) // create path and map variables path := "/v2/pet/{petId}?testing_byte_array=true" path = strings.Replace(path, "{" + "petId" + "}", fmt.Sprintf("%v", petId), -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(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 } /** * Update an existing pet * * @param body Pet object that needs to be added to the store * @return void */ //func (a PetApi) UpdatePet (body Pet) (error) { func (a PetApi) UpdatePet (body Pet) (error) { _sling := sling.New().Put(a.basePath) // create path and map variables path := "/v2/pet" _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 } /** * Updates a pet in the store with form data * * @param petId ID of pet that needs to be updated * @param name Updated name of the pet * @param status Updated status of the pet * @return void */ //func (a PetApi) UpdatePetWithForm (petId string, name string, status string) (error) { func (a PetApi) UpdatePetWithForm (petId string, name string, status string) (error) { _sling := sling.New().Post(a.basePath) // create path and map variables path := "/v2/pet/{petId}" path = strings.Replace(path, "{" + "petId" + "}", fmt.Sprintf("%v", petId), -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 } type FormParams struct { name string `url:"name,omitempty"` status string `url:"status,omitempty"` } _sling = _sling.BodyForm(&FormParams{ name: name,status: status }) // 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 } /** * uploads an image * * @param petId ID of pet to update * @param additionalMetadata Additional data to pass to server * @param file file to upload * @return void */ //func (a PetApi) UploadFile (petId int64, additionalMetadata string, file *os.File) (error) { func (a PetApi) UploadFile (petId int64, additionalMetadata string, file *os.File) (error) { _sling := sling.New().Post(a.basePath) // create path and map variables path := "/v2/pet/{petId}/uploadImage" path = strings.Replace(path, "{" + "petId" + "}", fmt.Sprintf("%v", petId), -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 } type FormParams struct { additionalMetadata string `url:"additionalMetadata,omitempty"` file *os.File `url:"file,omitempty"` } _sling = _sling.BodyForm(&FormParams{ additionalMetadata: additionalMetadata,file: file }) // 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 }