From 326257219d9bf65487003a1f6b32c3deafc390a8 Mon Sep 17 00:00:00 2001 From: Jedri Visser Date: Fri, 14 Oct 2022 10:41:48 +0200 Subject: [PATCH] Add client-type-prefix output option (#785) --- examples/prefixed-client/api.yaml | 23 ++ examples/prefixed-client/cfg.yaml | 7 + examples/prefixed-client/doc.go | 6 + .../prefixed-client/prefixed-client.gen.go | 239 ++++++++++++++++++ pkg/codegen/configuration.go | 1 + .../templates/client-with-responses.tmpl | 4 +- pkg/codegen/templates/client.tmpl | 24 +- 7 files changed, 292 insertions(+), 12 deletions(-) create mode 100644 examples/prefixed-client/api.yaml create mode 100644 examples/prefixed-client/cfg.yaml create mode 100644 examples/prefixed-client/doc.go create mode 100644 examples/prefixed-client/prefixed-client.gen.go diff --git a/examples/prefixed-client/api.yaml b/examples/prefixed-client/api.yaml new file mode 100644 index 000000000..40a35bf91 --- /dev/null +++ b/examples/prefixed-client/api.yaml @@ -0,0 +1,23 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: Prefixed Client Example +paths: + /client: + get: + operationId: getClient + responses: + 200: + content: + application/json: + schema: + $ref: "#/components/schemas/Client" +components: + schemas: + Client: + type: object + required: + - name + properties: + name: + type: string diff --git a/examples/prefixed-client/cfg.yaml b/examples/prefixed-client/cfg.yaml new file mode 100644 index 000000000..6a40799ed --- /dev/null +++ b/examples/prefixed-client/cfg.yaml @@ -0,0 +1,7 @@ +package: prefixedclient +output: prefixed-client.gen.go +generate: + models: true + client: true +output-options: + client-type-prefix: Prefixed diff --git a/examples/prefixed-client/doc.go b/examples/prefixed-client/doc.go new file mode 100644 index 000000000..6f7b89dd3 --- /dev/null +++ b/examples/prefixed-client/doc.go @@ -0,0 +1,6 @@ +package prefixedclient + +// This is an example of how to add a prefix to the name of the generated Client struct +// See https://github.com/deepmap/oapi-codegen/issues/785 for why this might be necessary + +//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen -config cfg.yaml api.yaml diff --git a/examples/prefixed-client/prefixed-client.gen.go b/examples/prefixed-client/prefixed-client.gen.go new file mode 100644 index 000000000..77d9bbd07 --- /dev/null +++ b/examples/prefixed-client/prefixed-client.gen.go @@ -0,0 +1,239 @@ +// Package prefixedclient provides primitives to interact with the openapi HTTP API. +// +// Code generated by github.com/deepmap/oapi-codegen version (devel) DO NOT EDIT. +package prefixedclient + +import ( + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" + "strings" +) + +// Client defines model for Client. +type Client struct { + Name string `json:"name"` +} + +// 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) +} + +// PrefixedClient which conforms to the OpenAPI3 specification for this service. +type PrefixedClient 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(*PrefixedClient) error + +// Creates a new PrefixedClient, with reasonable defaults +func NewClient(server string, opts ...ClientOption) (*PrefixedClient, error) { + // create a client with sane default values + client := PrefixedClient{ + 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 *PrefixedClient) 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 *PrefixedClient) 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) +} + +func (c *PrefixedClient) 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) +} + +// 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 +} + +func (c *PrefixedClient) 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 *PrefixedClient) 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 { + // GetClient request + GetClientWithResponse(ctx context.Context, reqEditors ...RequestEditorFn) (*GetClientResponse, error) +} + +type GetClientResponse struct { + Body []byte + HTTPResponse *http.Response + JSON200 *Client +} + +// 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 +} + +// 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) +} + +// 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 Client + if err := json.Unmarshal(bodyBytes, &dest); err != nil { + return nil, err + } + response.JSON200 = &dest + + } + + return response, nil +} diff --git a/pkg/codegen/configuration.go b/pkg/codegen/configuration.go index 290c139c3..7e4464d42 100644 --- a/pkg/codegen/configuration.go +++ b/pkg/codegen/configuration.go @@ -76,6 +76,7 @@ type OutputOptions struct { ExcludeSchemas []string `yaml:"exclude-schemas,omitempty"` // Exclude from generation schemas with given names. Ignored when empty. ResponseTypeSuffix string `yaml:"response-type-suffix,omitempty"` // The suffix used for responses types + ClientTypePrefix string `yaml:"client-type-prefix,omitempty"` // The prefix used for the client type } // UpdateDefaults sets reasonable default values for unset fields in Configuration diff --git a/pkg/codegen/templates/client-with-responses.tmpl b/pkg/codegen/templates/client-with-responses.tmpl index 4cb85f0ac..ca2c0da1c 100644 --- a/pkg/codegen/templates/client-with-responses.tmpl +++ b/pkg/codegen/templates/client-with-responses.tmpl @@ -13,9 +13,11 @@ func NewClientWithResponses(server string, opts ...ClientOption) (*ClientWithRes return &ClientWithResponses{client}, nil } +{{$clientPrefix := opts.OutputOptions.ClientTypePrefix -}} + // WithBaseURL overrides the baseURL. func WithBaseURL(baseURL string) ClientOption { - return func(c *Client) error { + return func(c *{{ $clientPrefix }}Client) error { newBaseURL, err := url.Parse(baseURL) if err != nil { return err diff --git a/pkg/codegen/templates/client.tmpl b/pkg/codegen/templates/client.tmpl index ffb62bbeb..b54920781 100644 --- a/pkg/codegen/templates/client.tmpl +++ b/pkg/codegen/templates/client.tmpl @@ -8,8 +8,10 @@ type HttpRequestDoer interface { Do(req *http.Request) (*http.Response, error) } -// Client which conforms to the OpenAPI3 specification for this service. -type Client struct { +{{$clientPrefix := opts.OutputOptions.ClientTypePrefix -}} + +// {{ $clientPrefix }}Client which conforms to the OpenAPI3 specification for this service. +type {{ $clientPrefix }}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 @@ -26,12 +28,12 @@ type Client struct { } // ClientOption allows setting custom parameters during construction -type ClientOption func(*Client) error +type ClientOption func(*{{ $clientPrefix }}Client) error -// Creates a new Client, with reasonable defaults -func NewClient(server string, opts ...ClientOption) (*Client, error) { +// Creates a new {{ $clientPrefix }}Client, with reasonable defaults +func NewClient(server string, opts ...ClientOption) (*{{ $clientPrefix }}Client, error) { // create a client with sane default values - client := Client{ + client := {{ $clientPrefix }}Client{ Server: server, } // mutate client and add all optional params @@ -54,7 +56,7 @@ func NewClient(server string, opts ...ClientOption) (*Client, error) { // 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 { + return func(c *{{ $clientPrefix }}Client) error { c.Client = doer return nil } @@ -63,7 +65,7 @@ func WithHTTPClient(doer HttpRequestDoer) ClientOption { // 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 { + return func(c *{{ $clientPrefix }}Client) error { c.RequestEditors = append(c.RequestEditors, fn) return nil } @@ -92,7 +94,7 @@ type ClientInterface interface { {{$pathParams := .PathParams -}} {{$opid := .OperationId -}} -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}}, reqEditors... RequestEditorFn) (*http.Response, error) { +func (c *{{ $clientPrefix }}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}}, reqEditors... RequestEditorFn) (*http.Response, error) { req, err := New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(c.Server{{genParamNames .PathParams}}{{if $hasParams}}, params{{end}}{{if .HasBody}}, contentType, body{{end}}) if err != nil { return nil, err @@ -106,7 +108,7 @@ func (c *Client) {{$opid}}{{if .HasBody}}WithBody{{end}}(ctx context.Context{{ge {{range .Bodies}} {{if .IsSupportedByClient -}} -func (c *Client) {{$opid}}{{.Suffix}}(ctx context.Context{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}, body {{$opid}}{{.NameTag}}RequestBody, reqEditors... RequestEditorFn) (*http.Response, error) { +func (c *{{ $clientPrefix }}Client) {{$opid}}{{.Suffix}}(ctx context.Context{{genParamArgs $pathParams}}{{if $hasParams}}, params *{{$opid}}Params{{end}}, body {{$opid}}{{.NameTag}}RequestBody, reqEditors... RequestEditorFn) (*http.Response, error) { req, err := New{{$opid}}Request{{.Suffix}}(c.Server{{genParamNames $pathParams}}{{if $hasParams}}, params{{end}}, body) if err != nil { return nil, err @@ -285,7 +287,7 @@ func New{{$opid}}Request{{if .HasBody}}WithBody{{end}}(server string{{genParamAr {{end}}{{/* Range */}} -func (c *Client) applyEditors(ctx context.Context, req *http.Request, additionalEditors []RequestEditorFn) error { +func (c *{{ $clientPrefix }}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