Skip to content

Switch to HTTP client interface to allow wrapped clients#86

Closed
nitishm wants to merge 2 commits intooapi-codegen:masterfrom
nitishm:feature/support-http-client-interface
Closed

Switch to HTTP client interface to allow wrapped clients#86
nitishm wants to merge 2 commits intooapi-codegen:masterfrom
nitishm:feature/support-http-client-interface

Conversation

@nitishm
Copy link
Copy Markdown
Contributor

@nitishm nitishm commented Oct 29, 2019

The standard library doesn't support features like Retry of error and different backoff strategies for the same. The recommended approach is to wrap the standard library HTTP client package with any custom handling logic. (See ianlancetaylor's reponse in golang/go#16047)
I propose adding an HTTPClient interface with the same method signatures as the standard library HTTP Client

    type Client
        func (c *Client) CloseIdleConnections()
        func (c *Client) Do(req *Request) (*Response, error)
        func (c *Client) Get(url string) (resp *Response, err error)
        func (c *Client) Head(url string) (resp *Response, err error)
        func (c *Client) Post(url, contentType string, body io.Reader) (resp *Response, err error)
        func (c *Client) PostForm(url string, data url.Values) (resp *Response, err error)

@weitzj
Copy link
Copy Markdown
Contributor

weitzj commented Nov 1, 2019

@nitishm Just asking: since the generated client here accepts an HTTPClient, the retry mechanisms or any other advanced stuff like oauth2 should already be possible. What would make sense is provide maybe an extension to the Readme how one can create some cool HTTPclients and inject them in the generates client here. But maybe I am misunderstanding something.

@nitishm
Copy link
Copy Markdown
Contributor Author

nitishm commented Nov 1, 2019

@nitishm Just asking: since the generated client here accepts an HTTPClient, the retry mechanisms or any other advanced stuff like oauth2 should already be possible. What would make sense is provide maybe an extension to the Readme how one can create some cool HTTP clientsΩ and inject them in the generates client here. But maybe I am misunderstanding something.

The standard HTTP client is pretty good but there are cases where you might want to wrap it with some extra functionality (think decorator pattern).
Let's say we want the client to automatically log before and after a POST command. The wrapper struct would allow us to add the functionality by implementing the interface, but under the hood continue to use the standard go HTTP library
For instance, the wrapping struct would look like below,

type Wrapper struct {
    logger logrus.Logrus
    client *http.Client
}

In my case, I am looking to add some retry options to the HTTP client using different backoff strategies. Unfortunately, the standard HTTP client library does not provide retry capabilities, and due to the generated code using oapi-codegen client, I cannot modify it to include retry capability.

@weitzj Hope that makes sense. Let me know if you think there is a better alternative.

@nitishm nitishm force-pushed the feature/support-http-client-interface branch from 07121de to cddf7c0 Compare November 3, 2019 01:01
@StevenACoffman
Copy link
Copy Markdown
Contributor

StevenACoffman commented Mar 11, 2020

Hey, How does this compare to #85 ?

@tooolbox
Copy link
Copy Markdown
Contributor

Seems like this was already done in #85 and can be closed.

@jamietanna jamietanna closed this Jul 29, 2022
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.

5 participants