Last active
July 22, 2024 16:32
-
-
Save Streppel/d216b77044c956747cade157f6f4fd46 to your computer and use it in GitHub Desktop.
Sample of auto-generated client from https://github.com/oapi-codegen/oapi-codegen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Package client provides primitives to interact with the openapi HTTP API. | |
// | |
// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.3.0 DO NOT EDIT. | |
package client | |
import ( | |
"context" | |
"encoding/json" | |
"fmt" | |
"io" | |
"net/http" | |
"net/url" | |
"strings" | |
openapi_types "github.com/oapi-codegen/runtime/types" | |
) | |
// ClientType defines model for ClientType. | |
type ClientType struct { | |
Name string `json:"name"` | |
} | |
// Error defines model for Error. | |
type Error struct { | |
Code *int `json:"code,omitempty"` | |
Message *string `json:"message,omitempty"` | |
} | |
// UploadFileMultipartBody defines parameters for UploadFile. | |
type UploadFileMultipartBody struct { | |
Description *string `json:"description,omitempty"` | |
File *openapi_types.File `json:"file,omitempty"` | |
} | |
// UploadFileMultipartRequestBody defines body for UploadFile for multipart/form-data ContentType. | |
type UploadFileMultipartRequestBody UploadFileMultipartBody | |
// 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 { | |
// GetClient request | |
GetClient(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) | |
// UpdateClient request | |
UpdateClient(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) | |
// UploadFileWithBody request with any body | |
UploadFileWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) | |
} | |
func (c *Client) GetClient(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) { | |
req, err := NewGetClientRequest(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) UpdateClient(ctx context.Context, reqEditors ...RequestEditorFn) (*http.Response, error) { | |
req, err := NewUpdateClientRequest(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) UploadFileWithBody(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*http.Response, error) { | |
req, err := NewUploadFileRequestWithBody(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) | |
} | |
// NewGetClientRequest generates requests for GetClient | |
func NewGetClientRequest(server string) (*http.Request, error) { | |
var err error | |
serverURL, err := url.Parse(server) | |
if err != nil { | |
return nil, err | |
} | |
operationPath := fmt.Sprintf("/client") | |
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 | |
} | |
// NewUpdateClientRequest generates requests for UpdateClient | |
func NewUpdateClientRequest(server string) (*http.Request, error) { | |
var err error | |
serverURL, err := url.Parse(server) | |
if err != nil { | |
return nil, err | |
} | |
operationPath := fmt.Sprintf("/client") | |
if operationPath[0] == '/' { | |
operationPath = "." + operationPath | |
} | |
queryURL, err := serverURL.Parse(operationPath) | |
if err != nil { | |
return nil, err | |
} | |
req, err := http.NewRequest("PUT", queryURL.String(), nil) | |
if err != nil { | |
return nil, err | |
} | |
return req, nil | |
} | |
// NewUploadFileRequestWithBody generates requests for UploadFile with any type of body | |
func NewUploadFileRequestWithBody(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("/upload") | |
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 | |
} | |
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 { | |
// GetClientWithResponse request | |
GetClientWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*GetClientResponse, error) | |
// UpdateClientWithResponse request | |
UpdateClientWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*UpdateClientResponse, error) | |
// UploadFileWithBodyWithResponse request with any body | |
UploadFileWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*UploadFileResponse, error) | |
} | |
type GetClientResponse struct { | |
Body []byte | |
HTTPResponse *http.Response | |
JSON200 *ClientType | |
} | |
// Status returns HTTPResponse.Status | |
func (r GetClientResponse) Status() string { | |
if r.HTTPResponse != nil { | |
return r.HTTPResponse.Status | |
} | |
return http.StatusText(0) | |
} | |
// StatusCode returns HTTPResponse.StatusCode | |
func (r GetClientResponse) StatusCode() int { | |
if r.HTTPResponse != nil { | |
return r.HTTPResponse.StatusCode | |
} | |
return 0 | |
} | |
type UpdateClientResponse struct { | |
Body []byte | |
HTTPResponse *http.Response | |
JSON400 *struct { | |
Code string `json:"code"` | |
} | |
} | |
// Status returns HTTPResponse.Status | |
func (r UpdateClientResponse) Status() string { | |
if r.HTTPResponse != nil { | |
return r.HTTPResponse.Status | |
} | |
return http.StatusText(0) | |
} | |
// StatusCode returns HTTPResponse.StatusCode | |
func (r UpdateClientResponse) StatusCode() int { | |
if r.HTTPResponse != nil { | |
return r.HTTPResponse.StatusCode | |
} | |
return 0 | |
} | |
type UploadFileResponse struct { | |
Body []byte | |
HTTPResponse *http.Response | |
JSON200 *struct { | |
FileName *string `json:"fileName,omitempty"` | |
FileSize *int `json:"fileSize,omitempty"` | |
} | |
JSON400 *Error | |
} | |
// Status returns HTTPResponse.Status | |
func (r UploadFileResponse) Status() string { | |
if r.HTTPResponse != nil { | |
return r.HTTPResponse.Status | |
} | |
return http.StatusText(0) | |
} | |
// StatusCode returns HTTPResponse.StatusCode | |
func (r UploadFileResponse) StatusCode() int { | |
if r.HTTPResponse != nil { | |
return r.HTTPResponse.StatusCode | |
} | |
return 0 | |
} | |
// GetClientWithResponse request returning *GetClientResponse | |
func (c *ClientWithResponses) GetClientWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*GetClientResponse, error) { | |
rsp, err := c.GetClient(ctx, reqEditors...) | |
if err != nil { | |
return nil, err | |
} | |
return ParseGetClientResponse(rsp) | |
} | |
// UpdateClientWithResponse request returning *UpdateClientResponse | |
func (c *ClientWithResponses) UpdateClientWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*UpdateClientResponse, error) { | |
rsp, err := c.UpdateClient(ctx, reqEditors...) | |
if err != nil { | |
return nil, err | |
} | |
return ParseUpdateClientResponse(rsp) | |
} | |
// UploadFileWithBodyWithResponse request with arbitrary body returning *UploadFileResponse | |
func (c *ClientWithResponses) UploadFileWithBodyWithResponse(ctx context.Context, contentType string, body io.Reader, reqEditors ...RequestEditorFn) (*UploadFileResponse, error) { | |
rsp, err := c.UploadFileWithBody(ctx, contentType, body, reqEditors...) | |
if err != nil { | |
return nil, err | |
} | |
return ParseUploadFileResponse(rsp) | |
} | |
// ParseGetClientResponse parses an HTTP response from a GetClientWithResponse call | |
func ParseGetClientResponse(rsp *http.Response) (*GetClientResponse, error) { | |
bodyBytes, err := io.ReadAll(rsp.Body) | |
defer func() { _ = rsp.Body.Close() }() | |
if err != nil { | |
return nil, err | |
} | |
response := &GetClientResponse{ | |
Body: bodyBytes, | |
HTTPResponse: rsp, | |
} | |
switch { | |
case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: | |
var dest ClientType | |
if err := json.Unmarshal(bodyBytes, &dest); err != nil { | |
return nil, err | |
} | |
response.JSON200 = &dest | |
} | |
return response, nil | |
} | |
// ParseUpdateClientResponse parses an HTTP response from a UpdateClientWithResponse call | |
func ParseUpdateClientResponse(rsp *http.Response) (*UpdateClientResponse, error) { | |
bodyBytes, err := io.ReadAll(rsp.Body) | |
defer func() { _ = rsp.Body.Close() }() | |
if err != nil { | |
return nil, err | |
} | |
response := &UpdateClientResponse{ | |
Body: bodyBytes, | |
HTTPResponse: rsp, | |
} | |
switch { | |
case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 400: | |
var dest struct { | |
Code string `json:"code"` | |
} | |
if err := json.Unmarshal(bodyBytes, &dest); err != nil { | |
return nil, err | |
} | |
response.JSON400 = &dest | |
} | |
return response, nil | |
} | |
// ParseUploadFileResponse parses an HTTP response from a UploadFileWithResponse call | |
func ParseUploadFileResponse(rsp *http.Response) (*UploadFileResponse, error) { | |
bodyBytes, err := io.ReadAll(rsp.Body) | |
defer func() { _ = rsp.Body.Close() }() | |
if err != nil { | |
return nil, err | |
} | |
response := &UploadFileResponse{ | |
Body: bodyBytes, | |
HTTPResponse: rsp, | |
} | |
switch { | |
case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: | |
var dest struct { | |
FileName *string `json:"fileName,omitempty"` | |
FileSize *int `json:"fileSize,omitempty"` | |
} | |
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 == 400: | |
var dest Error | |
if err := json.Unmarshal(bodyBytes, &dest); err != nil { | |
return nil, err | |
} | |
response.JSON400 = &dest | |
} | |
return response, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment