package catalystsquadhelloworldexampleapi /** * Generated by API Builder - https://www.apibuilder.io * Service version: 1.0.1 * User agent: apibuilder app.apibuilder.io/catalystsquad/catalyst-squad-hello-world-example-api/1.0.1/go_1_5_client */ import ( "bytes" "encoding/json" "errors" "fmt" "io" "io/ioutil" "net/http" "strconv" ) const UserAgent = "apibuilder app.apibuilder.io/catalystsquad/catalyst-squad-hello-world-example-api/1.0.1/go_1_5_client" const Version = "1.0.1" const VersionMajor = 1 type Client struct { HttpClient *http.Client Username string Password string BaseUrl string Headers http.Header } /** * A Hello with a name and id */ type Hello struct { Id string `json:"id"` Name string `json:"name"` } func HelloFromMap(data interface{}) Hello { b, err := json.Marshal(data) if err == nil { return HelloFromJson(bytes.NewReader(b)) } else { panic(err) } } func HelloFromJson(bytes io.Reader) Hello { var hello Hello json.NewDecoder(bytes).Decode(&hello) return hello } /** * A Hello that has not yet been created */ type Newhello struct { Name string `json:"name"` } func NewhelloFromMap(data interface{}) Newhello { b, err := json.Marshal(data) if err == nil { return NewhelloFromJson(bytes.NewReader(b)) } else { panic(err) } } func NewhelloFromJson(bytes io.Reader) Newhello { var newhello Newhello json.NewDecoder(bytes).Decode(&newhello) return newhello } type HelloesGetResponse struct { StatusCode int Response *http.Response Error error Hello Hello } func HelloesGet(client Client) HelloesGetResponse { requestUrl := fmt.Sprintf("%s/helloes", client.BaseUrl) request, err := buildRequest(client, "GET", requestUrl) if err != nil { return HelloesGetResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return HelloesGetResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return HelloesGetResponse{StatusCode: resp.StatusCode, Response: resp, Hello: HelloFromJson(resp.Body)} case 404: return HelloesGetResponse{StatusCode: resp.StatusCode, Response: resp} default: return HelloesGetResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type HelloesPostResponse struct { StatusCode int Response *http.Response Error error Hello Hello Value string } func HelloesPost(client Client) HelloesPostResponse { requestUrl := fmt.Sprintf("%s/helloes", client.BaseUrl) request, err := buildRequest(client, "POST", requestUrl) if err != nil { return HelloesPostResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return HelloesPostResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return HelloesPostResponse{StatusCode: resp.StatusCode, Response: resp, Hello: HelloFromJson(resp.Body)} case 401: return HelloesPostResponse{StatusCode: resp.StatusCode, Response: resp, Value: func() string { body, _ := ioutil.ReadAll(resp.Body) return string(body) }()} default: return HelloesPostResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } func buildRequest(client Client, method, urlStr string) (*http.Request, error) { request, err := http.NewRequest(method, urlStr, nil) if err != nil { return nil, err } request.Header.Add("User-Agent", UserAgent) request.Header.Add("X-Apidoc-Version", Version) request.Header.Add("X-Apidoc-Version-Major", strconv.Itoa(VersionMajor)) if client.Headers != nil { for key, values := range client.Headers { for _, value := range values { request.Header.Add(key, value) } } } if client.Username != "" { request.SetBasicAuth(client.Username, client.Password) } return request, nil }