package swagger import ( "strings" "fmt" "encoding/json" "errors" "github.com/dghubble/sling" ) type UserApi struct { Configuration Configuration } func NewUserApi() *UserApi{ configuration := NewConfiguration() return &UserApi { Configuration: *configuration, } } func NewUserApiWithBasePath(basePath string) *UserApi{ configuration := NewConfiguration() configuration.BasePath = basePath return &UserApi { Configuration: *configuration, } } /** * 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) { _sling := sling.New().Post(a.Configuration.BasePath) // create path and map variables path := "/v2/user" _sling = _sling.Path(path) // add default headers if any for key := range a.Configuration.DefaultHeader { _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) } // accept header accepts := []string { "application/xml", "application/json" } 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) { _sling := sling.New().Post(a.Configuration.BasePath) // create path and map variables path := "/v2/user/createWithArray" _sling = _sling.Path(path) // add default headers if any for key := range a.Configuration.DefaultHeader { _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) } // accept header accepts := []string { "application/xml", "application/json" } 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) { _sling := sling.New().Post(a.Configuration.BasePath) // create path and map variables path := "/v2/user/createWithList" _sling = _sling.Path(path) // add default headers if any for key := range a.Configuration.DefaultHeader { _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) } // accept header accepts := []string { "application/xml", "application/json" } 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) { _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) _sling = _sling.Path(path) // add default headers if any for key := range a.Configuration.DefaultHeader { _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) } // accept header accepts := []string { "application/xml", "application/json" } 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) { _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) _sling = _sling.Path(path) // add default headers if any for key := range a.Configuration.DefaultHeader { _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) } // accept header accepts := []string { "application/xml", "application/json" } 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) { _sling := sling.New().Get(a.Configuration.BasePath) // create path and map variables path := "/v2/user/login" _sling = _sling.Path(path) // add default headers if any for key := range a.Configuration.DefaultHeader { _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) } 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/xml", "application/json" } 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) { _sling := sling.New().Get(a.Configuration.BasePath) // create path and map variables path := "/v2/user/logout" _sling = _sling.Path(path) // add default headers if any for key := range a.Configuration.DefaultHeader { _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) } // accept header accepts := []string { "application/xml", "application/json" } 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) { _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) _sling = _sling.Path(path) // add default headers if any for key := range a.Configuration.DefaultHeader { _sling = _sling.Set(key, a.Configuration.DefaultHeader[key]) } // accept header accepts := []string { "application/xml", "application/json" } 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 }