Blame vendor/github.com/deepmap/oapi-codegen/pkg/codegen/templates/client.tmpl

Packit Service 3a6627
// RequestEditorFn  is the function signature for the RequestEditor callback function
Packit Service 3a6627
type RequestEditorFn func(ctx context.Context, req *http.Request) error
Packit Service 3a6627
Packit Service 3a6627
// Doer performs HTTP requests.
Packit Service 3a6627
//
Packit Service 3a6627
// The standard http.Client implements this interface.
Packit Service 3a6627
type HttpRequestDoer interface {
Packit Service 3a6627
	Do(req *http.Request) (*http.Response, error)
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
// Client which conforms to the OpenAPI3 specification for this service.
Packit Service 3a6627
type Client struct {
Packit Service 3a6627
	// The endpoint of the server conforming to this interface, with scheme,
Packit Service 3a6627
	// https://api.deepmap.com for example.
Packit Service 3a6627
	Server string
Packit Service 3a6627
Packit Service 3a6627
	// Doer for performing requests, typically a *http.Client with any
Packit Service 3a6627
	// customized settings, such as certificate chains.
Packit Service 3a6627
	Client HttpRequestDoer
Packit Service 3a6627
Packit Service 3a6627
	// A callback for modifying requests which are generated before sending over
Packit Service 3a6627
	// the network.
Packit Service 3a6627
	RequestEditor RequestEditorFn
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
// ClientOption allows setting custom parameters during construction
Packit Service 3a6627
type ClientOption func(*Client) error
Packit Service 3a6627
Packit Service 3a6627
// Creates a new Client, with reasonable defaults
Packit Service 3a6627
func NewClient(server string, opts ...ClientOption) (*Client, error) {
Packit Service 3a6627
    // create a client with sane default values
Packit Service 3a6627
    client := Client{
Packit Service 3a6627
        Server: server,
Packit Service 3a6627
    }
Packit Service 3a6627
    // mutate client and add all optional params
Packit Service 3a6627
    for _, o := range opts {
Packit Service 3a6627
        if err := o(&client); err != nil {
Packit Service 3a6627
            return nil, err
Packit Service 3a6627
        }
Packit Service 3a6627
    }
Packit Service 3a6627
    // ensure the server URL always has a trailing slash
Packit Service 3a6627
    if !strings.HasSuffix(client.Server, "/") {
Packit Service 3a6627
        client.Server += "/"
Packit Service 3a6627
    }
Packit Service 3a6627
    // create httpClient, if not already present
Packit Service 3a6627
    if client.Client == nil {
Packit Service 3a6627
        client.Client = http.DefaultClient
Packit Service 3a6627
    }
Packit Service 3a6627
    return &client, nil
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
// WithHTTPClient allows overriding the default Doer, which is
Packit Service 3a6627
// automatically created using http.Client. This is useful for tests.
Packit Service 3a6627
func WithHTTPClient(doer HttpRequestDoer) ClientOption {
Packit Service 3a6627
	return func(c *Client) error {
Packit Service 3a6627
		c.Client = doer
Packit Service 3a6627
		return nil
Packit Service 3a6627
	}
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
// WithRequestEditorFn allows setting up a callback function, which will be
Packit Service 3a6627
// called right before sending the request. This can be used to mutate the request.
Packit Service 3a6627
func WithRequestEditorFn(fn RequestEditorFn) ClientOption {
Packit Service 3a6627
	return func(c *Client) error {
Packit Service 3a6627
		c.RequestEditor = fn
Packit Service 3a6627
		return nil
Packit Service 3a6627
	}
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
// The interface specification for the client above.
Packit Service 3a6627
type ClientInterface interface {
Packit Service 3a6627
{{range . -}}
Packit Service 3a6627
{{$hasParams := .RequiresParamObject -}}
Packit Service 3a6627
{{$pathParams := .PathParams -}}
Packit Service 3a6627
{{$opid := .OperationId -}}
Packit Service 3a6627
    // {{$opid}} request {{if .HasBody}} with any body{{end}}
Packit Service 3a6627
    {{$opid}}{{if .HasBody}}WithBody{{end}}(ctx context.Context{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}{{if .HasBody}}, contentType string, body io.Reader{{end}}) (*http.Response, error)
Packit Service 3a6627
{{range .Bodies}}
Packit Service 3a6627
    {{$opid}}{{.Suffix}}(ctx context.Context{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}, body {{$opid}}{{.NameTag}}RequestBody) (*http.Response, error)
Packit Service 3a6627
{{end}}{{/* range .Bodies */}}
Packit Service 3a6627
{{end}}{{/* range . $opid := .OperationId */}}
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
Packit Service 3a6627
{{/* Generate client methods */}}
Packit Service 3a6627
{{range . -}}
Packit Service 3a6627
{{$hasParams := .RequiresParamObject -}}
Packit Service 3a6627
{{$pathParams := .PathParams -}}
Packit Service 3a6627
{{$opid := .OperationId -}}
Packit Service 3a6627
Packit Service 3a6627
func (c *Client) {{$opid}}{{if .HasBody}}WithBody{{end}}(ctx context.Context{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}{{if .HasBody}}, contentType string, body io.Reader{{end}}) (*http.Response, error) {
Packit Service 3a6627
    req, err := New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(c.Server{{genParamNames .PathParams}}{{if $hasParams}}, params{{end}}{{if .HasBody}}, contentType, body{{end}})
Packit Service 3a6627
    if err != nil {
Packit Service 3a6627
        return nil, err
Packit Service 3a6627
    }
Packit Service 3a6627
    req = req.WithContext(ctx)
Packit Service 3a6627
    if c.RequestEditor != nil {
Packit Service 3a6627
        err = c.RequestEditor(ctx, req)
Packit Service 3a6627
        if err != nil {
Packit Service 3a6627
            return nil, err
Packit Service 3a6627
        }
Packit Service 3a6627
    }
Packit Service 3a6627
    return c.Client.Do(req)
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
{{range .Bodies}}
Packit Service 3a6627
func (c *Client) {{$opid}}{{.Suffix}}(ctx context.Context{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}, body {{$opid}}{{.NameTag}}RequestBody) (*http.Response, error) {
Packit Service 3a6627
    req, err := New{{$opid}}{{.Suffix}}Request(c.Server{{genParamNames $pathParams}}{{if $hasParams}}, params{{end}}, body)
Packit Service 3a6627
    if err != nil {
Packit Service 3a6627
        return nil, err
Packit Service 3a6627
    }
Packit Service 3a6627
    req = req.WithContext(ctx)
Packit Service 3a6627
    if c.RequestEditor != nil {
Packit Service 3a6627
        err = c.RequestEditor(ctx, req)
Packit Service 3a6627
        if err != nil {
Packit Service 3a6627
            return nil, err
Packit Service 3a6627
        }
Packit Service 3a6627
    }
Packit Service 3a6627
    return c.Client.Do(req)
Packit Service 3a6627
}
Packit Service 3a6627
{{end}}{{/* range .Bodies */}}
Packit Service 3a6627
{{end}}
Packit Service 3a6627
Packit Service 3a6627
{{/* Generate request builders */}}
Packit Service 3a6627
{{range .}}
Packit Service 3a6627
{{$hasParams := .RequiresParamObject -}}
Packit Service 3a6627
{{$pathParams := .PathParams -}}
Packit Service 3a6627
{{$bodyRequired := .BodyRequired -}}
Packit Service 3a6627
{{$opid := .OperationId -}}
Packit Service 3a6627
Packit Service 3a6627
{{range .Bodies}}
Packit Service 3a6627
// New{{$opid}}Request{{.Suffix}} calls the generic {{$opid}} builder with {{.ContentType}} body
Packit Service 3a6627
func New{{$opid}}Request{{.Suffix}}(server string{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}, body {{$opid}}{{.NameTag}}RequestBody) (*http.Request, error) {
Packit Service 3a6627
    var bodyReader io.Reader
Packit Service 3a6627
    buf, err := json.Marshal(body)
Packit Service 3a6627
    if err != nil {
Packit Service 3a6627
        return nil, err
Packit Service 3a6627
    }
Packit Service 3a6627
    bodyReader = bytes.NewReader(buf)
Packit Service 3a6627
    return New{{$opid}}RequestWithBody(server{{genParamNames $pathParams}}{{if $hasParams}}, params{{end}}, "{{.ContentType}}", bodyReader)
Packit Service 3a6627
}
Packit Service 3a6627
{{end}}
Packit Service 3a6627
Packit Service 3a6627
// New{{$opid}}Request{{if .HasBody}}WithBody{{end}} generates requests for {{$opid}}{{if .HasBody}} with any type of body{{end}}
Packit Service 3a6627
func New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(server string{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}{{if .HasBody}}, contentType string, body io.Reader{{end}}) (*http.Request, error) {
Packit Service 3a6627
    var err error
Packit Service 3a6627
{{range $paramIdx, $param := .PathParams}}
Packit Service 3a6627
    var pathParam{{$paramIdx}} string
Packit Service 3a6627
    {{if .IsPassThrough}}
Packit Service 3a6627
    pathParam{{$paramIdx}} = {{.ParamName}}
Packit Service 3a6627
    {{end}}
Packit Service 3a6627
    {{if .IsJson}}
Packit Service 3a6627
    var pathParamBuf{{$paramIdx}} []byte
Packit Service 3a6627
    pathParamBuf{{$paramIdx}}, err = json.Marshal({{.ParamName}})
Packit Service 3a6627
    if err != nil {
Packit Service 3a6627
        return nil, err
Packit Service 3a6627
    }
Packit Service 3a6627
    pathParam{{$paramIdx}} = string(pathParamBuf{{$paramIdx}})
Packit Service 3a6627
    {{end}}
Packit Service 3a6627
    {{if .IsStyled}}
Packit Service 3a6627
    pathParam{{$paramIdx}}, err = runtime.StyleParam("{{.Style}}", {{.Explode}}, "{{.ParamName}}", {{.GoVariableName}})
Packit Service 3a6627
    if err != nil {
Packit Service 3a6627
        return nil, err
Packit Service 3a6627
    }
Packit Service 3a6627
    {{end}}
Packit Service 3a6627
{{end}}
Packit Service 3a6627
    queryUrl, err := url.Parse(server)
Packit Service 3a6627
    if err != nil {
Packit Service 3a6627
        return nil, err
Packit Service 3a6627
    }
Packit Service 3a6627
Packit Service 3a6627
    basePath := fmt.Sprintf("{{genParamFmtString .Path}}"{{range $paramIdx, $param := .PathParams}}, pathParam{{$paramIdx}}{{end}})
Packit Service 3a6627
    if basePath[0] == '/' {
Packit Service 3a6627
        basePath = basePath[1:]
Packit Service 3a6627
    }
Packit Service 3a6627
Packit Service 3a6627
    queryUrl, err = queryUrl.Parse(basePath)
Packit Service 3a6627
    if err != nil {
Packit Service 3a6627
        return nil, err
Packit Service 3a6627
    }
Packit Service 3a6627
{{if .QueryParams}}
Packit Service 3a6627
    queryValues := queryUrl.Query()
Packit Service 3a6627
{{range $paramIdx, $param := .QueryParams}}
Packit Service 3a6627
    {{if not .Required}} if params.{{.GoName}} != nil { {{end}}
Packit Service 3a6627
    {{if .IsPassThrough}}
Packit Service 3a6627
    queryValues.Add("{{.ParamName}}", {{if not .Required}}*{{end}}params.{{.GoName}})
Packit Service 3a6627
    {{end}}
Packit Service 3a6627
    {{if .IsJson}}
Packit Service 3a6627
    if queryParamBuf, err := json.Marshal({{if not .Required}}*{{end}}params.{{.GoName}}); err != nil {
Packit Service 3a6627
        return nil, err
Packit Service 3a6627
    } else {
Packit Service 3a6627
        queryValues.Add("{{.ParamName}}", string(queryParamBuf))
Packit Service 3a6627
    }
Packit Service 3a6627
Packit Service 3a6627
    {{end}}
Packit Service 3a6627
    {{if .IsStyled}}
Packit Service 3a6627
    if queryFrag, err := runtime.StyleParam("{{.Style}}", {{.Explode}}, "{{.ParamName}}", {{if not .Required}}*{{end}}params.{{.GoName}}); err != nil {
Packit Service 3a6627
        return nil, err
Packit Service 3a6627
    } else if parsed, err := url.ParseQuery(queryFrag); err != nil {
Packit Service 3a6627
       return nil, err
Packit Service 3a6627
    } else {
Packit Service 3a6627
       for k, v := range parsed {
Packit Service 3a6627
           for _, v2 := range v {
Packit Service 3a6627
               queryValues.Add(k, v2)
Packit Service 3a6627
           }
Packit Service 3a6627
       }
Packit Service 3a6627
    }
Packit Service 3a6627
    {{end}}
Packit Service 3a6627
    {{if not .Required}}}{{end}}
Packit Service 3a6627
{{end}}
Packit Service 3a6627
    queryUrl.RawQuery = queryValues.Encode()
Packit Service 3a6627
{{end}}{{/* if .QueryParams */}}
Packit Service 3a6627
    req, err := http.NewRequest("{{.Method}}", queryUrl.String(), {{if .HasBody}}body{{else}}nil{{end}})
Packit Service 3a6627
    if err != nil {
Packit Service 3a6627
        return nil, err
Packit Service 3a6627
    }
Packit Service 3a6627
Packit Service 3a6627
{{range $paramIdx, $param := .HeaderParams}}
Packit Service 3a6627
    {{if not .Required}} if params.{{.GoName}} != nil { {{end}}
Packit Service 3a6627
    var headerParam{{$paramIdx}} string
Packit Service 3a6627
    {{if .IsPassThrough}}
Packit Service 3a6627
    headerParam{{$paramIdx}} = {{if not .Required}}*{{end}}params.{{.GoName}}
Packit Service 3a6627
    {{end}}
Packit Service 3a6627
    {{if .IsJson}}
Packit Service 3a6627
    var headerParamBuf{{$paramIdx}} []byte
Packit Service 3a6627
    headerParamBuf{{$paramIdx}}, err = json.Marshal({{if not .Required}}*{{end}}params.{{.GoName}})
Packit Service 3a6627
    if err != nil {
Packit Service 3a6627
        return nil, err
Packit Service 3a6627
    }
Packit Service 3a6627
    headerParam{{$paramIdx}} = string(headerParamBuf{{$paramIdx}})
Packit Service 3a6627
    {{end}}
Packit Service 3a6627
    {{if .IsStyled}}
Packit Service 3a6627
    headerParam{{$paramIdx}}, err = runtime.StyleParam("{{.Style}}", {{.Explode}}, "{{.ParamName}}", {{if not .Required}}*{{end}}params.{{.GoName}})
Packit Service 3a6627
    if err != nil {
Packit Service 3a6627
        return nil, err
Packit Service 3a6627
    }
Packit Service 3a6627
    {{end}}
Packit Service 3a6627
    req.Header.Add("{{.ParamName}}", headerParam{{$paramIdx}})
Packit Service 3a6627
    {{if not .Required}}}{{end}}
Packit Service 3a6627
{{end}}
Packit Service 3a6627
Packit Service 3a6627
{{range $paramIdx, $param := .CookieParams}}
Packit Service 3a6627
    {{if not .Required}} if params.{{.GoName}} != nil { {{end}}
Packit Service 3a6627
    var cookieParam{{$paramIdx}} string
Packit Service 3a6627
    {{if .IsPassThrough}}
Packit Service 3a6627
    cookieParam{{$paramIdx}} = {{if not .Required}}*{{end}}params.{{.GoName}}
Packit Service 3a6627
    {{end}}
Packit Service 3a6627
    {{if .IsJson}}
Packit Service 3a6627
    var cookieParamBuf{{$paramIdx}} []byte
Packit Service 3a6627
    cookieParamBuf{{$paramIdx}}, err = json.Marshal({{if not .Required}}*{{end}}params.{{.GoName}})
Packit Service 3a6627
    if err != nil {
Packit Service 3a6627
        return nil, err
Packit Service 3a6627
    }
Packit Service 3a6627
    cookieParam{{$paramIdx}} = url.QueryEscape(string(cookieParamBuf{{$paramIdx}}))
Packit Service 3a6627
    {{end}}
Packit Service 3a6627
    {{if .IsStyled}}
Packit Service 3a6627
    cookieParam{{$paramIdx}}, err = runtime.StyleParam("simple", {{.Explode}}, "{{.ParamName}}", {{if not .Required}}*{{end}}params.{{.GoName}})
Packit Service 3a6627
    if err != nil {
Packit Service 3a6627
        return nil, err
Packit Service 3a6627
    }
Packit Service 3a6627
    {{end}}
Packit Service 3a6627
    cookie{{$paramIdx}} := &http.Cookie{
Packit Service 3a6627
        Name:"{{.ParamName}}",
Packit Service 3a6627
        Value:cookieParam{{$paramIdx}},
Packit Service 3a6627
    }
Packit Service 3a6627
    req.AddCookie(cookie{{$paramIdx}})
Packit Service 3a6627
    {{if not .Required}}}{{end}}
Packit Service 3a6627
{{end}}
Packit Service 3a6627
    {{if .HasBody}}req.Header.Add("Content-Type", contentType){{end}}
Packit Service 3a6627
    return req, nil
Packit Service 3a6627
}
Packit Service 3a6627
Packit Service 3a6627
{{end}}{{/* Range */}}