[Go] Adding response middleware (#16641)

* Add optional response middleware to go template

Also adds a request middleware which can return an error

* Re-adding newline to fix tests

* Fixing docs
This commit is contained in:
AlanCitrix 2023-10-01 11:10:17 -04:00 committed by GitHub
parent 039c1698b0
commit 2eca00f87d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 2 deletions

View File

@ -352,6 +352,15 @@ func (a *{{{classname}}}Service) {{nickname}}Execute(r {{#structPrefix}}{{&class
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, err
}
{{#withCustomMiddlewareFunction}}
if a.client.cfg.ResponseMiddleware != nil {
err = a.client.cfg.ResponseMiddleware(localVarHTTPResponse, localVarBody)
if err != nil {
return {{#returnType}}localVarReturnValue, {{/returnType}}localVarHTTPResponse, err
}
}
{{/withCustomMiddlewareFunction}}
if localVarHTTPResponse.StatusCode >= 300 {
newErr := &GenericOpenAPIError{
body: localVarBody,

View File

@ -487,6 +487,13 @@ func (c *APIClient) prepareRequest(
c.cfg.Middleware(localVarRequest)
}
if c.cfg.MiddlewareWithError != nil {
err = c.cfg.MiddlewareWithError(localVarRequest)
if err != nil {
return nil, err
}
}
{{/withCustomMiddlewareFunction}}
{{#hasHttpSignatureMethods}}
if ctx != nil {

View File

@ -104,9 +104,15 @@ type ServerConfiguration struct {
type ServerConfigurations []ServerConfiguration
{{#withCustomMiddlewareFunction}}
// MiddlewareFunction provides way to implement custom middleware
// MiddlewareFunction provides way to implement custom middleware in the prepareRequest
type MiddlewareFunction func(*http.Request)
// MiddlewareFunctionWithError provides way to implement custom middleware with errors in the prepareRequest
type MiddlewareFunctionWithError func(*http.Request) error
// ResponseMiddlewareFunction provides way to implement custom middleware with errors after the response is received
type ResponseMiddlewareFunction func(*http.Response, []byte) error
{{/withCustomMiddlewareFunction}}
// Configuration stores the configuration of the API client
type Configuration struct {
@ -119,7 +125,9 @@ type Configuration struct {
OperationServers map[string]ServerConfigurations
HTTPClient *http.Client
{{#withCustomMiddlewareFunction}}
Middleware MiddlewareFunction
Middleware MiddlewareFunction
MiddlewareWithError MiddlewareFunctionWithError
ResponseMiddleware ResponseMiddlewareFunction
{{/withCustomMiddlewareFunction}}
}