mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-06-29 12:10:54 +00:00
[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:
parent
039c1698b0
commit
2eca00f87d
@ -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,
|
||||
|
@ -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 {
|
||||
|
@ -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}}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user