package termsteelversionapi /** * Generated by API Builder - https://www.apibuilder.io * Service version: 0.0.1 * User agent: apibuilder app.apibuilder.io/cerium/termsteelversionapi/0.0.1/go_1_5_client */ import ( "bytes" "encoding/json" "errors" "fmt" "html" "io" "io/apibuilder/spec" "net/http" "net/url" "strconv" "strings" ) const BaseUrl = "https://api.apibuilder.io" const UserAgent = "apibuilder app.apibuilder.io/cerium/termsteelversionapi/0.0.1/go_1_5_client" const Version = "0.0.1" const VersionMajor = 0 type Client struct { HttpClient *http.Client Username string Password string BaseUrl string Headers http.Header } type ClientRequestBody struct { contentType string bytes io.Reader } /** * Allows generator authors to flag files with special characteristics. It is up to * the client (i.e. the cli) to decide how to interpret them. */ type FileFlag string const ( FileFlagScaffolding FileFlag = "scaffolding" // Indicates files that an end user starts from but should edit. Not intended to be the final product (see: https://stackoverflow.com/questions/235018/what-is-scaffolding-is-it-a-term-for-a-particular-platform). Consider not overwriting these files when code is re-generated. FileFlagUNDEFINED = "UNDEFINED" ) func FileFlagFromString(value string) FileFlag { switch strings.TrimSpace(strings.ToLower(value)) { case "scaffolding": return FileFlagScaffolding default: return FileFlagUNDEFINED } } /** * An attribute represents a key/value pair that is optionally used to provide * additional instructions / data to the code generator. An example could be an * attribute to specify the root import path for a go client.. */ type Attribute struct { Name string `json:"name"` Value string `json:"value"` } func AttributeFromMap(data interface{}) Attribute { b, err := json.Marshal(data) if err == nil { return AttributeFromJson(bytes.NewReader(b)) } else { panic(err) } } func AttributeFromJson(bytes io.Reader) Attribute { var attribute Attribute json.NewDecoder(bytes).Decode(&attribute) return attribute } type Error struct { Code string `json:"code"` Message string `json:"message"` } func ErrorFromMap(data interface{}) Error { b, err := json.Marshal(data) if err == nil { return ErrorFromJson(bytes.NewReader(b)) } else { panic(err) } } func ErrorFromJson(bytes io.Reader) Error { var error_ Error json.NewDecoder(bytes).Decode(&error_) return error_ } /** * Represents a source file */ type File struct { Name string `json:"name"` Dir string `json:"dir,omitempty"` Contents string `json:"contents"` Flags []FileFlag `json:"flags,omitempty"` } func FileFromMap(data interface{}) File { b, err := json.Marshal(data) if err == nil { return FileFromJson(bytes.NewReader(b)) } else { panic(err) } } func FileFromJson(bytes io.Reader) File { var file File json.NewDecoder(bytes).Decode(&file) return file } /** * The generator metadata. */ type Generator struct { Key string `json:"key"` Name string `json:"name"` Language string `json:"language,omitempty"` Description string `json:"description,omitempty"` Attributes []string `json:"attributes"` } func GeneratorFromMap(data interface{}) Generator { b, err := json.Marshal(data) if err == nil { return GeneratorFromJson(bytes.NewReader(b)) } else { panic(err) } } func GeneratorFromJson(bytes io.Reader) Generator { var generator Generator json.NewDecoder(bytes).Decode(&generator) return generator } type Healthcheck struct { Status string `json:"status"` } func HealthcheckFromMap(data interface{}) Healthcheck { b, err := json.Marshal(data) if err == nil { return HealthcheckFromJson(bytes.NewReader(b)) } else { panic(err) } } func HealthcheckFromJson(bytes io.Reader) Healthcheck { var healthcheck Healthcheck json.NewDecoder(bytes).Decode(&healthcheck) return healthcheck } /** * The result of invoking a generator. */ type Invocation struct { Source string `json:"source"` Files []File `json:"files"` } func InvocationFromMap(data interface{}) Invocation { b, err := json.Marshal(data) if err == nil { return InvocationFromJson(bytes.NewReader(b)) } else { panic(err) } } func InvocationFromJson(bytes io.Reader) Invocation { var invocation Invocation json.NewDecoder(bytes).Decode(&invocation) return invocation } /** * The invocation form is the payload send to the code generators when requesting * generation of client code. */ type InvocationForm struct { Service spec.Service `json:"service"` Attributes []Attribute `json:"attributes"` UserAgent string `json:"user_agent,omitempty"` ImportedServices []spec.Service `json:"imported_services,omitempty"` } func InvocationFormFromMap(data interface{}) InvocationForm { b, err := json.Marshal(data) if err == nil { return InvocationFormFromJson(bytes.NewReader(b)) } else { panic(err) } } func InvocationFormFromJson(bytes io.Reader) InvocationForm { var invocationForm InvocationForm json.NewDecoder(bytes).Decode(&invocationForm) return invocationForm } type GeneratorsGetParams struct { Key string Limit int32 Offset int32 } type GeneratorsGetResponse struct { StatusCode int Response *http.Response Error error Generators []Generator } func GeneratorsGet(client Client, params GeneratorsGetParams) GeneratorsGetResponse { requestUrl := fmt.Sprintf("%s/generators", client.BaseUrl) urlValues := url.Values{} if "" != params.Key { urlValues.Add("key", params.Key) } if 0 == params.Limit { urlValues.Add("limit", strconv.FormatInt(100, 10)) } else { urlValues.Add("limit", strconv.FormatInt(params.Limit, 10)) } if 0 == params.Offset { urlValues.Add("offset", strconv.FormatInt(0, 10)) } else { urlValues.Add("offset", strconv.FormatInt(params.Offset, 10)) } encodedValues := urlValues.Encode() if encodedValues != "" { requestUrl += "?" + encodedValues } request, err := buildRequest(client, "GET", requestUrl, ClientRequestBody{}) if err != nil { return GeneratorsGetResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return GeneratorsGetResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return GeneratorsGetResponse{StatusCode: resp.StatusCode, Response: resp, Generators: func() []Generator { var tmp []interface{} json.NewDecoder(resp.Body).Decode(&tmp) var all []Generator for _, el := range tmp { all = append(all, GeneratorFromMap(el)) } return all }()} default: return GeneratorsGetResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type GeneratorsGetByKeyResponse struct { StatusCode int Response *http.Response Error error Generator Generator } func GeneratorsGetByKey(client Client, key string) GeneratorsGetByKeyResponse { requestUrl := fmt.Sprintf("%s/generators/%s", client.BaseUrl, html.EscapeString(key)) request, err := buildRequest(client, "GET", requestUrl, ClientRequestBody{}) if err != nil { return GeneratorsGetByKeyResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return GeneratorsGetByKeyResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return GeneratorsGetByKeyResponse{StatusCode: resp.StatusCode, Response: resp, Generator: GeneratorFromJson(resp.Body)} case 404: return GeneratorsGetByKeyResponse{StatusCode: resp.StatusCode, Response: resp} default: return GeneratorsGetByKeyResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type HealthchecksGetResponse struct { StatusCode int Response *http.Response Error error Healthcheck Healthcheck } func HealthchecksGet(client Client) HealthchecksGetResponse { requestUrl := fmt.Sprintf("%s/_internal_/healthcheck", client.BaseUrl) request, err := buildRequest(client, "GET", requestUrl, ClientRequestBody{}) if err != nil { return HealthchecksGetResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return HealthchecksGetResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return HealthchecksGetResponse{StatusCode: resp.StatusCode, Response: resp, Healthcheck: HealthcheckFromJson(resp.Body)} default: return HealthchecksGetResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type InvocationsPostByKeyResponse struct { StatusCode int Response *http.Response Error error ErrorS []Error Invocation Invocation } func InvocationsPostByKey(client Client, key string, invocationForm InvocationForm) InvocationsPostByKeyResponse { requestUrl := fmt.Sprintf("%s/invocations/%s", client.BaseUrl, html.EscapeString(key)) if nil == invocationForm.Attributes { invocationForm.Attributes = []Attribute{} } bodyDocument, err := json.Marshal(invocationForm) if err != nil { return InvocationsPostByKeyResponse{Error: err} } body := ClientRequestBody{contentType: "application/json", bytes: bytes.NewReader(bodyDocument)} request, err := buildRequest(client, "POST", requestUrl, body) if err != nil { return InvocationsPostByKeyResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return InvocationsPostByKeyResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return InvocationsPostByKeyResponse{StatusCode: resp.StatusCode, Response: resp, Invocation: InvocationFromJson(resp.Body)} case 409: return InvocationsPostByKeyResponse{StatusCode: resp.StatusCode, Response: resp, ErrorS: func() []Error { var tmp []interface{} json.NewDecoder(resp.Body).Decode(&tmp) var all []Error for _, el := range tmp { all = append(all, ErrorFromMap(el)) } return all }()} default: return InvocationsPostByKeyResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } func buildRequest(client Client, method, urlStr string, body ClientRequestBody) (*http.Request, error) { request, err := http.NewRequest(method, urlStr, body.bytes) 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 body.contentType != "" { request.Header.Add("Content-Type", body.contentType) } if client.Username != "" { request.SetBasicAuth(client.Username, client.Password) } return request, nil }