Skip to content

improvement: Use interface for the HTTP Client#85

Merged
deepmap-marcinr merged 4 commits intooapi-codegen:masterfrom
sebnow:http-client-interface
Nov 15, 2019
Merged

improvement: Use interface for the HTTP Client#85
deepmap-marcinr merged 4 commits intooapi-codegen:masterfrom
sebnow:http-client-interface

Conversation

@sebnow
Copy link
Copy Markdown
Contributor

@sebnow sebnow commented Oct 24, 2019

Depend on an interface, rather than a concrete implementation, so as to
allow for alternate implementations to be used, such as decorated
clients.

Depend on an interface, rather than a concrete implementation, so as to
allow for alternate implementations to be used, such as decorated
clients.
@weitzj
Copy link
Copy Markdown
Contributor

weitzj commented Oct 24, 2019

For this to work, you would need to remove https://github.com/deepmap/oapi-codegen/blob/master/pkg/codegen/templates/client-with-responses.tmpl#L49-L89

This would be fine with me. I would suggest to at least add a ClientOption like so:

// WithDoer allows overriding the default httpClient, which is
// automatically created. The Doer interface is typically implemented
// by http.Client. 
func WithDoer(doer Doer) ClientOption {
	return func(c *Client) error {
		c.Client = doer
		return nil
	}
}

@deepmap-marcinr wdyt?


// HTTP client with any customized settings, such as certificate chains.
Client *http.Client
Client Doer
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please fix the code-comment to match the Doer interface.

// RequestEditorFn is the function signature for the RequestEditor callback function
type RequestEditorFn func(req *http.Request, ctx context.Context) error

// Doer performs HTTP requests
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it would be nice to have an additional example comment and possibly an example where one mentions that http.Client implements this interface

@sebnow
Copy link
Copy Markdown
Contributor Author

sebnow commented Oct 25, 2019

For this to work, you would need to remove https://github.com/deepmap/oapi-codegen/blob/master/pkg/codegen/templates/client-with-responses.tmpl#L49-L89

Oh, right. I was working off an earlier version (current release) and didn't notice that was added. It appears that the options would still be compatible, albeit maybe a bit confusing, considering the options are only used when a client is not provided.

Edit:

I would suggest to at least add a ClientOption like so:

Sure. How about keeping the name WithHTTPClient? It doesn't mirror the type, but it seems like a more intuitive name.

@weitzj
Copy link
Copy Markdown
Contributor

weitzj commented Oct 30, 2019

Sorry for the late reply. The other options would fail on you either way. Therefore it would be better to remove them.

To customize the HTTP client, the WithHTTPClient option can be used.
@weitzj
Copy link
Copy Markdown
Contributor

weitzj commented Nov 1, 2019

Looks good to me 👍
Could you please check if you have removed the passage in the Readme which mentions how to make a new open api client since some “With...” options are gone

I don’t have permissions to merge. @deepmap-marcinr can help

golang.org/x/tools v0.0.0-20190724185037-8aa4eac1a7c1 // indirect
)

go 1.13
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you use anything here which requires go 1.13? I'd like to avoid shutting out users of earlier versions of Go - since there's a compile time dependency on our runtime package.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my bad. I must have committed it by mistake - go mod is quite intrusive. No reason to add a requirement on 1.13.

@deepmap-marcinr deepmap-marcinr merged commit 6965e18 into oapi-codegen:master Nov 15, 2019
@sebnow sebnow deleted the http-client-interface branch November 18, 2019 09:20
adrianpk pushed a commit to foorester/oapi-codegen that referenced this pull request Jan 16, 2024
* improvement: Use interface for the HTTP Client

Depend on an interface, rather than a concrete implementation, so as to
allow for alternate implementations to be used, such as decorated
clients.

* Update comments regarding the Doer interface

* Update WithHTTPClient to accept a Doer implementation

* Remove HTTP client configuration options

To customize the HTTP client, the WithHTTPClient option can be used.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants