package common /** * Generated by API Builder - https://www.apibuilder.io * Service version: 0.0.1 * User agent: apibuilder app.apibuilder.io/nccloud/common/0.0.1/go_1_5_client */ import ( "bytes" "encoding/json" "errors" "fmt" "io" "net/http" "strconv" "strings" ) const UserAgent = "apibuilder app.apibuilder.io/nccloud/common/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 of an error source. */ type ErrorSourceType string const ( ErrorSourceTypePointer ErrorSourceType = "pointer" // Indicating a [JSON pointer](https://tools.ietf.org/html/rfc6901) to the value that has caused the error. ErrorSourceTypeParameter = "parameter" // Indicating an URI query parameter causing the error. ErrorSourceTypeHeader = "header" // Indicating a request header causing the error. ErrorSourceTypeUNDEFINED = "UNDEFINED" ) func ErrorSourceTypeFromString(value string) ErrorSourceType { switch strings.TrimSpace(strings.ToLower(value)) { case "pointer": return ErrorSourceTypePointer case "parameter": return ErrorSourceTypeParameter case "header": return ErrorSourceTypeHeader default: return ErrorSourceTypeUNDEFINED } } /** * The status of a service. */ type HealthStatus string const ( HealthStatusRunning HealthStatus = "running" // The service is running and healthy. HealthStatusStarting = "starting" // The service is not yet running. HealthStatusUnhealthy = "unhealthy" // The service has encountered errors and is not running property. HealthStatusUNDEFINED = "UNDEFINED" ) func HealthStatusFromString(value string) HealthStatus { switch strings.TrimSpace(strings.ToLower(value)) { case "running": return HealthStatusRunning case "starting": return HealthStatusStarting case "unhealthy": return HealthStatusUnhealthy default: return HealthStatusUNDEFINED } } /** * The direction in which to sort by a property. */ type SortDirection string const ( SortDirectionAsc SortDirection = "asc" // Sort ascending. SortDirectionDesc = "desc" // Sort descending. SortDirectionUNDEFINED = "UNDEFINED" ) func SortDirectionFromString(value string) SortDirection { switch strings.TrimSpace(strings.ToLower(value)) { case "asc": return SortDirectionAsc case "desc": return SortDirectionDesc default: return SortDirectionUNDEFINED } } /** * Basic audit information for a resource. */ type Audit struct { CreatedAt string `json:"created_at"` CreatedBy string `json:"created_by"` UpdatedAt string `json:"updated_at"` UpdatedBy string `json:"updated_by"` } func AuditFromMap(data interface{}) Audit { b, err := json.Marshal(data) if err == nil { return AuditFromJson(bytes.NewReader(b)) } else { panic(err) } } func AuditFromJson(bytes io.Reader) Audit { var audit Audit json.NewDecoder(bytes).Decode(&audit) return audit } /** * Links to further information about the occurred error. */ type ErrorLinks struct { About Link `json:"about,omitempty"` Type Link `json:"type,omitempty"` } func ErrorLinksFromMap(data interface{}) ErrorLinks { b, err := json.Marshal(data) if err == nil { return ErrorLinksFromJson(bytes.NewReader(b)) } else { panic(err) } } func ErrorLinksFromJson(bytes io.Reader) ErrorLinks { var errorLinks ErrorLinks json.NewDecoder(bytes).Decode(&errorLinks) return errorLinks } /** * Reference to the primary source of an error. */ type ErrorSource struct { Type ErrorSourceType `json:"type"` Source string `json:"source"` } func ErrorSourceFromMap(data interface{}) ErrorSource { b, err := json.Marshal(data) if err == nil { return ErrorSourceFromJson(bytes.NewReader(b)) } else { panic(err) } } func ErrorSourceFromJson(bytes io.Reader) ErrorSource { var errorSource ErrorSource json.NewDecoder(bytes).Decode(&errorSource) return errorSource } /** * An error returned on failed API requests. */ type GenericError struct { Links ErrorLinks `json:"links,omitempty"` Code string `json:"code"` Title string `json:"title"` Detail string `json:"detail"` Source ErrorSource `json:"source,omitempty"` Meta map[string]interface{} `json:"meta,omitempty"` } func GenericErrorFromMap(data interface{}) GenericError { b, err := json.Marshal(data) if err == nil { return GenericErrorFromJson(bytes.NewReader(b)) } else { panic(err) } } func GenericErrorFromJson(bytes io.Reader) GenericError { var genericError GenericError json.NewDecoder(bytes).Decode(&genericError) return genericError } /** * Indicating the health of a service. */ type HealthCheck struct { Status HealthStatus `json:"status"` Message string `json:"message,omitempty"` } 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 } /** * Represents a link with some meta information. */ type LinkObject struct { Href string `json:"href"` Title string `json:"title,omitempty"` Type string `json:"type,omitempty"` } func LinkObjectFromMap(data interface{}) LinkObject { b, err := json.Marshal(data) if err == nil { return LinkObjectFromJson(bytes.NewReader(b)) } else { panic(err) } } func LinkObjectFromJson(bytes io.Reader) LinkObject { var linkObject LinkObject json.NewDecoder(bytes).Decode(&linkObject) return linkObject } /** * Paging information returned from a paged request. */ type PageResponse struct { Offset int32 `json:"offset"` Limit int32 `json:"limit"` Sort []Sort `json:"sort"` TotalResults int64 `json:"total_results"` TotalPages int64 `json:"total_pages"` } func PageResponseFromMap(data interface{}) PageResponse { b, err := json.Marshal(data) if err == nil { return PageResponseFromJson(bytes.NewReader(b)) } else { panic(err) } } func PageResponseFromJson(bytes io.Reader) PageResponse { var pageResponse PageResponse json.NewDecoder(bytes).Decode(&pageResponse) return pageResponse } /** * Form containing paging information. */ type PagingForm struct { Offset int32 `json:"offset"` Limit int32 `json:"limit"` Sort []Sort `json:"sort,omitempty"` } func PagingFormFromMap(data interface{}) PagingForm { b, err := json.Marshal(data) if err == nil { return PagingFormFromJson(bytes.NewReader(b)) } else { panic(err) } } func PagingFormFromJson(bytes io.Reader) PagingForm { var pagingForm PagingForm json.NewDecoder(bytes).Decode(&pagingForm) return pagingForm } /** * Information about a deployed service. */ type ServiceInfo struct { Name string `json:"name"` Version string `json:"version"` Uri string `json:"uri"` Title string `json:"title,omitempty"` Description string `json:"description,omitempty"` } func ServiceInfoFromMap(data interface{}) ServiceInfo { b, err := json.Marshal(data) if err == nil { return ServiceInfoFromJson(bytes.NewReader(b)) } else { panic(err) } } func ServiceInfoFromJson(bytes io.Reader) ServiceInfo { var serviceInfo ServiceInfo json.NewDecoder(bytes).Decode(&serviceInfo) return serviceInfo } /** * Single property to sort by. */ type Sort struct { Property string `json:"property"` Direction SortDirection `json:"direction"` } func SortFromMap(data interface{}) Sort { b, err := json.Marshal(data) if err == nil { return SortFromJson(bytes.NewReader(b)) } else { panic(err) } } func SortFromJson(bytes io.Reader) Sort { var sort Sort json.NewDecoder(bytes).Decode(&sort) return sort } /** * Represents a link to a resource. */ type Link struct { String string LinkObject LinkObject Undefined string } func LinkFromMap(data interface{}) Link { b, err := json.Marshal(data) if err == nil { return LinkFromJson(bytes.NewReader(b)) } else { panic(err) } } func LinkFromJson(bytes io.Reader) Link { // TODO: Not yet supported } type HealthChecksGetHealthcheckResponse struct { StatusCode int Response *http.Response Error error HealthCheck HealthCheck } func HealthChecksGetHealthcheck(client Client) HealthChecksGetHealthcheckResponse { requestUrl := fmt.Sprintf("%s/_internal_/healthcheck", client.BaseUrl) request, err := buildRequest(client, "GET", requestUrl) if err != nil { return HealthChecksGetHealthcheckResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return HealthChecksGetHealthcheckResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return HealthChecksGetHealthcheckResponse{StatusCode: resp.StatusCode, Response: resp, HealthCheck: HealthCheckFromJson(resp.Body)} default: return HealthChecksGetHealthcheckResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type HealthChecksGetProbeAndReadyResponse struct { StatusCode int Response *http.Response Error error } func HealthChecksGetProbeAndReady(client Client) HealthChecksGetProbeAndReadyResponse { requestUrl := fmt.Sprintf("%s/_internal_/probe/ready", client.BaseUrl) request, err := buildRequest(client, "GET", requestUrl) if err != nil { return HealthChecksGetProbeAndReadyResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return HealthChecksGetProbeAndReadyResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return HealthChecksGetProbeAndReadyResponse{StatusCode: resp.StatusCode, Response: resp} case 400: return HealthChecksGetProbeAndReadyResponse{StatusCode: resp.StatusCode, Response: resp} default: return HealthChecksGetProbeAndReadyResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type HealthChecksGetProbeAndLiveResponse struct { StatusCode int Response *http.Response Error error } func HealthChecksGetProbeAndLive(client Client) HealthChecksGetProbeAndLiveResponse { requestUrl := fmt.Sprintf("%s/_internal_/probe/live", client.BaseUrl) request, err := buildRequest(client, "GET", requestUrl) if err != nil { return HealthChecksGetProbeAndLiveResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return HealthChecksGetProbeAndLiveResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return HealthChecksGetProbeAndLiveResponse{StatusCode: resp.StatusCode, Response: resp} default: return HealthChecksGetProbeAndLiveResponse{StatusCode: resp.StatusCode, Response: resp, Error: errors.New(resp.Status)} } } type ServiceInfosGetServiceResponse struct { StatusCode int Response *http.Response Error error ServiceInfo ServiceInfo } func ServiceInfosGetService(client Client) ServiceInfosGetServiceResponse { requestUrl := fmt.Sprintf("%s/_internal_/service", client.BaseUrl) request, err := buildRequest(client, "GET", requestUrl) if err != nil { return ServiceInfosGetServiceResponse{Error: err} } resp, err := client.HttpClient.Do(request) if err != nil { return ServiceInfosGetServiceResponse{Error: err} } defer resp.Body.Close() switch resp.StatusCode { case 200: return ServiceInfosGetServiceResponse{StatusCode: resp.StatusCode, Response: resp, ServiceInfo: ServiceInfoFromJson(resp.Body)} default: return ServiceInfosGetServiceResponse{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 }