// Package api provides primitives to interact with the openapi HTTP API. // // Code generated by github.com/deepmap/oapi-codegen version v1.9.0 DO NOT EDIT. package api import ( "bytes" "context" "encoding/json" "fmt" "io" "io/ioutil" "net/http" "net/url" "strings" ) // RequestEditorFn is the function signature for the RequestEditor callback function type RequestEditorFn func(ctx context.Context, req *http.Request) error // Doer performs HTTP requests. // // The standard http.Client implements this interface. type HttpRequestDoer interface { Do(req *http.Request) (*http.Response, error) } // Client which conforms to the OpenAPI3 specification for this service. type Client struct { // The endpoint of the server conforming to this interface, with scheme, // https://api.deepmap.com for example. This can contain a path relative // to the server, such as https://api.deepmap.com/dev-test, and all the // paths in the swagger spec will be appended to the server. Server string // Doer for performing requests, typically a *http.Client with any // customized settings, such as certificate chains. Client HttpRequestDoer // A list of callbacks for modifying requests which are generated before sending over // the network. RequestEditors []RequestEditorFn } // ClientOption allows setting custom parameters during construction type ClientOption func(*Client) error // Creates a new Client, with reasonable defaults func NewClient(server string, opts ...ClientOption) (*Client, error) { // create a client with sane default values client := Client{ Server: server, } // mutate client and add all optional params for _, o := range opts { if err := o(&client); err != nil { return nil, err } } // ensure the server URL always has a trailing slash if !strings.HasSuffix(client.Server, "/") { client.Server += "/" } // create httpClient, if not already present if client.Client == nil { client.Client = &http.Client{} } return &client, nil } // WithHTTPClient allows overriding the default Doer, which is // automatically created using http.Client. This is useful for tests. func WithHTTPClient(doer HttpRequestDoer) ClientOption { return func(c *Client) error { c.Client = doer return nil } } // WithRequestEditorFn allows setting up a callback function, which will be // called right before sending the request. This can be used to mutate the request. func WithRequestEditorFn(fn RequestEditorFn) ClientOption { return func(c *Client) error { c.RequestEditors = append(c.RequestEditors, fn) return nil } } // The interface specification for the client above. type ClientInterface interface { // SubmitJob request with any body SubmitJobWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) SubmitJob(ctx context.Context, body SubmitJobJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) // GetJobTypes request GetJobTypes(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) // RegisterWorker request with any body RegisterWorkerWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) RegisterWorker(ctx context.Context, body RegisterWorkerJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) // ScheduleTask request ScheduleTask(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) } func (c *Client) SubmitJobWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { req, err := NewSubmitJobRequestWithBody(c.Server, contentType, body) if err != nil { return nil, err } req = req.WithContext(ctx) if err := c.applyEditors(ctx, req, reqEditors); err != nil { return nil, err } return c.Client.Do(req) } func (c *Client) SubmitJob(ctx context.Context, body SubmitJobJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) { req, err := NewSubmitJobRequest(c.Server, body) if err != nil { return nil, err } req = req.WithContext(ctx) if err := c.applyEditors(ctx, req, reqEditors); err != nil { return nil, err } return c.Client.Do(req) } func (c *Client) GetJobTypes(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) { req, err := NewGetJobTypesRequest(c.Server) if err != nil { return nil, err } req = req.WithContext(ctx) if err := c.applyEditors(ctx, req, reqEditors); err != nil { return nil, err } return c.Client.Do(req) } func (c *Client) RegisterWorkerWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { req, err := NewRegisterWorkerRequestWithBody(c.Server, contentType, body) if err != nil { return nil, err } req = req.WithContext(ctx) if err := c.applyEditors(ctx, req, reqEditors); err != nil { return nil, err } return c.Client.Do(req) } func (c *Client) RegisterWorker(ctx context.Context, body RegisterWorkerJSONRequestBody, reqEditors ...RequestEditorFn) (*http.Response, error) { req, err := NewRegisterWorkerRequest(c.Server, body) if err != nil { return nil, err } req = req.WithContext(ctx) if err := c.applyEditors(ctx, req, reqEditors); err != nil { return nil, err } return c.Client.Do(req) } func (c *Client) ScheduleTask(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) { req, err := NewScheduleTaskRequest(c.Server) if err != nil { return nil, err } req = req.WithContext(ctx) if err := c.applyEditors(ctx, req, reqEditors); err != nil { return nil, err } return c.Client.Do(req) } // NewSubmitJobRequest calls the generic SubmitJob builder with application/json body func NewSubmitJobRequest(server string, body SubmitJobJSONRequestBody) (*http.Request, error) { var bodyReader io.Reader buf, err := json.Marshal(body) if err != nil { return nil, err } bodyReader = bytes.NewReader(buf) return NewSubmitJobRequestWithBody(server, "application/json", bodyReader) } // NewSubmitJobRequestWithBody generates requests for SubmitJob with any type of body func NewSubmitJobRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { var err error serverURL, err := url.Parse(server) if err != nil { return nil, err } operationPath := fmt.Sprintf("/api/jobs") if operationPath[0] == '/' { operationPath = "." + operationPath } queryURL, err := serverURL.Parse(operationPath) if err != nil { return nil, err } req, err := http.NewRequest("POST", queryURL.String(), body) if err != nil { return nil, err } req.Header.Add("Content-Type", contentType) return req, nil } // NewGetJobTypesRequest generates requests for GetJobTypes func NewGetJobTypesRequest(server string) (*http.Request, error) { var err error serverURL, err := url.Parse(server) if err != nil { return nil, err } operationPath := fmt.Sprintf("/api/jobs/types") if operationPath[0] == '/' { operationPath = "." + operationPath } queryURL, err := serverURL.Parse(operationPath) if err != nil { return nil, err } req, err := http.NewRequest("GET", queryURL.String(), nil) if err != nil { return nil, err } return req, nil } // NewRegisterWorkerRequest calls the generic RegisterWorker builder with application/json body func NewRegisterWorkerRequest(server string, body RegisterWorkerJSONRequestBody) (*http.Request, error) { var bodyReader io.Reader buf, err := json.Marshal(body) if err != nil { return nil, err } bodyReader = bytes.NewReader(buf) return NewRegisterWorkerRequestWithBody(server, "application/json", bodyReader) } // NewRegisterWorkerRequestWithBody generates requests for RegisterWorker with any type of body func NewRegisterWorkerRequestWithBody(server string, contentType string, body io.Reader) (*http.Request, error) { var err error serverURL, err := url.Parse(server) if err != nil { return nil, err } operationPath := fmt.Sprintf("/api/worker/register-worker") if operationPath[0] == '/' { operationPath = "." + operationPath } queryURL, err := serverURL.Parse(operationPath) if err != nil { return nil, err } req, err := http.NewRequest("POST", queryURL.String(), body) if err != nil { return nil, err } req.Header.Add("Content-Type", contentType) return req, nil } // NewScheduleTaskRequest generates requests for ScheduleTask func NewScheduleTaskRequest(server string) (*http.Request, error) { var err error serverURL, err := url.Parse(server) if err != nil { return nil, err } operationPath := fmt.Sprintf("/api/worker/task") if operationPath[0] == '/' { operationPath = "." + operationPath } queryURL, err := serverURL.Parse(operationPath) if err != nil { return nil, err } req, err := http.NewRequest("POST", queryURL.String(), nil) if err != nil { return nil, err } return req, nil } func (c *Client) applyEditors(ctx context.Context, req *http.Request, additionalEditors []RequestEditorFn) error { for _, r := range c.RequestEditors { if err := r(ctx, req); err != nil { return err } } for _, r := range additionalEditors { if err := r(ctx, req); err != nil { return err } } return nil } // ClientWithResponses builds on ClientInterface to offer response payloads type ClientWithResponses struct { ClientInterface } // NewClientWithResponses creates a new ClientWithResponses, which wraps // Client with return type handling func NewClientWithResponses(server string, opts ...ClientOption) (*ClientWithResponses, error) { client, err := NewClient(server, opts...) if err != nil { return nil, err } return &ClientWithResponses{client}, nil } // WithBaseURL overrides the baseURL. func WithBaseURL(baseURL string) ClientOption { return func(c *Client) error { newBaseURL, err := url.Parse(baseURL) if err != nil { return err } c.Server = newBaseURL.String() return nil } } // ClientWithResponsesInterface is the interface specification for the client with responses above. type ClientWithResponsesInterface interface { // SubmitJob request with any body SubmitJobWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*SubmitJobResponse, error) SubmitJobWithResponse(ctx context.Context, body SubmitJobJSONRequestBody, reqEditors ...RequestEditorFn) (*SubmitJobResponse, error) // GetJobTypes request GetJobTypesWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*GetJobTypesResponse, error) // RegisterWorker request with any body RegisterWorkerWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*RegisterWorkerResponse, error) RegisterWorkerWithResponse(ctx context.Context, body RegisterWorkerJSONRequestBody, reqEditors ...RequestEditorFn) (*RegisterWorkerResponse, error) // ScheduleTask request ScheduleTaskWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*ScheduleTaskResponse, error) } type SubmitJobResponse struct { Body []byte HTTPResponse *http.Response JSON200 *SubmittedJob JSONDefault *Error } // Status returns HTTPResponse.Status func (r SubmitJobResponse) Status() string { if r.HTTPResponse != nil { return r.HTTPResponse.Status } return http.StatusText(0) } // StatusCode returns HTTPResponse.StatusCode func (r SubmitJobResponse) StatusCode() int { if r.HTTPResponse != nil { return r.HTTPResponse.StatusCode } return 0 } type GetJobTypesResponse struct { Body []byte HTTPResponse *http.Response JSON200 *AvailableJobTypes } // Status returns HTTPResponse.Status func (r GetJobTypesResponse) Status() string { if r.HTTPResponse != nil { return r.HTTPResponse.Status } return http.StatusText(0) } // StatusCode returns HTTPResponse.StatusCode func (r GetJobTypesResponse) StatusCode() int { if r.HTTPResponse != nil { return r.HTTPResponse.StatusCode } return 0 } type RegisterWorkerResponse struct { Body []byte HTTPResponse *http.Response JSON200 *RegisteredWorker JSONDefault *Error } // Status returns HTTPResponse.Status func (r RegisterWorkerResponse) Status() string { if r.HTTPResponse != nil { return r.HTTPResponse.Status } return http.StatusText(0) } // StatusCode returns HTTPResponse.StatusCode func (r RegisterWorkerResponse) StatusCode() int { if r.HTTPResponse != nil { return r.HTTPResponse.StatusCode } return 0 } type ScheduleTaskResponse struct { Body []byte HTTPResponse *http.Response JSON200 *AssignedTask JSON403 *SecurityError } // Status returns HTTPResponse.Status func (r ScheduleTaskResponse) Status() string { if r.HTTPResponse != nil { return r.HTTPResponse.Status } return http.StatusText(0) } // StatusCode returns HTTPResponse.StatusCode func (r ScheduleTaskResponse) StatusCode() int { if r.HTTPResponse != nil { return r.HTTPResponse.StatusCode } return 0 } // SubmitJobWithBodyWithResponse request with arbitrary body returning *SubmitJobResponse func (c *ClientWithResponses) SubmitJobWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*SubmitJobResponse, error) { rsp, err := c.SubmitJobWithBody(ctx, contentType, body, reqEditors...) if err != nil { return nil, err } return ParseSubmitJobResponse(rsp) } func (c *ClientWithResponses) SubmitJobWithResponse(ctx context.Context, body SubmitJobJSONRequestBody, reqEditors ...RequestEditorFn) (*SubmitJobResponse, error) { rsp, err := c.SubmitJob(ctx, body, reqEditors...) if err != nil { return nil, err } return ParseSubmitJobResponse(rsp) } // GetJobTypesWithResponse request returning *GetJobTypesResponse func (c *ClientWithResponses) GetJobTypesWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*GetJobTypesResponse, error) { rsp, err := c.GetJobTypes(ctx, reqEditors...) if err != nil { return nil, err } return ParseGetJobTypesResponse(rsp) } // RegisterWorkerWithBodyWithResponse request with arbitrary body returning *RegisterWorkerResponse func (c *ClientWithResponses) RegisterWorkerWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*RegisterWorkerResponse, error) { rsp, err := c.RegisterWorkerWithBody(ctx, contentType, body, reqEditors...) if err != nil { return nil, err } return ParseRegisterWorkerResponse(rsp) } func (c *ClientWithResponses) RegisterWorkerWithResponse(ctx context.Context, body RegisterWorkerJSONRequestBody, reqEditors ...RequestEditorFn) (*RegisterWorkerResponse, error) { rsp, err := c.RegisterWorker(ctx, body, reqEditors...) if err != nil { return nil, err } return ParseRegisterWorkerResponse(rsp) } // ScheduleTaskWithResponse request returning *ScheduleTaskResponse func (c *ClientWithResponses) ScheduleTaskWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*ScheduleTaskResponse, error) { rsp, err := c.ScheduleTask(ctx, reqEditors...) if err != nil { return nil, err } return ParseScheduleTaskResponse(rsp) } // ParseSubmitJobResponse parses an HTTP response from a SubmitJobWithResponse call func ParseSubmitJobResponse(rsp *http.Response) (*SubmitJobResponse, error) { bodyBytes, err := ioutil.ReadAll(rsp.Body) defer func() { _ = rsp.Body.Close() }() if err != nil { return nil, err } response := &SubmitJobResponse{ Body: bodyBytes, HTTPResponse: rsp, } switch { case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: var dest SubmittedJob if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } response.JSON200 = &dest case strings.Contains(rsp.Header.Get("Content-Type"), "json") && true: var dest Error if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } response.JSONDefault = &dest } return response, nil } // ParseGetJobTypesResponse parses an HTTP response from a GetJobTypesWithResponse call func ParseGetJobTypesResponse(rsp *http.Response) (*GetJobTypesResponse, error) { bodyBytes, err := ioutil.ReadAll(rsp.Body) defer func() { _ = rsp.Body.Close() }() if err != nil { return nil, err } response := &GetJobTypesResponse{ Body: bodyBytes, HTTPResponse: rsp, } switch { case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: var dest AvailableJobTypes if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } response.JSON200 = &dest } return response, nil } // ParseRegisterWorkerResponse parses an HTTP response from a RegisterWorkerWithResponse call func ParseRegisterWorkerResponse(rsp *http.Response) (*RegisterWorkerResponse, error) { bodyBytes, err := ioutil.ReadAll(rsp.Body) defer func() { _ = rsp.Body.Close() }() if err != nil { return nil, err } response := &RegisterWorkerResponse{ Body: bodyBytes, HTTPResponse: rsp, } switch { case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: var dest RegisteredWorker if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } response.JSON200 = &dest case strings.Contains(rsp.Header.Get("Content-Type"), "json") && true: var dest Error if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } response.JSONDefault = &dest } return response, nil } // ParseScheduleTaskResponse parses an HTTP response from a ScheduleTaskWithResponse call func ParseScheduleTaskResponse(rsp *http.Response) (*ScheduleTaskResponse, error) { bodyBytes, err := ioutil.ReadAll(rsp.Body) defer func() { _ = rsp.Body.Close() }() if err != nil { return nil, err } response := &ScheduleTaskResponse{ Body: bodyBytes, HTTPResponse: rsp, } switch { case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: var dest AssignedTask if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } response.JSON200 = &dest case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 403: var dest SecurityError if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } response.JSON403 = &dest } return response, nil }