mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-06-29 20:20:53 +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
|
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 {
|
if localVarHTTPResponse.StatusCode >= 300 {
|
||||||
newErr := &GenericOpenAPIError{
|
newErr := &GenericOpenAPIError{
|
||||||
body: localVarBody,
|
body: localVarBody,
|
||||||
|
@ -487,6 +487,13 @@ func (c *APIClient) prepareRequest(
|
|||||||
c.cfg.Middleware(localVarRequest)
|
c.cfg.Middleware(localVarRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.cfg.MiddlewareWithError != nil {
|
||||||
|
err = c.cfg.MiddlewareWithError(localVarRequest)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
{{/withCustomMiddlewareFunction}}
|
{{/withCustomMiddlewareFunction}}
|
||||||
{{#hasHttpSignatureMethods}}
|
{{#hasHttpSignatureMethods}}
|
||||||
if ctx != nil {
|
if ctx != nil {
|
||||||
|
@ -104,9 +104,15 @@ type ServerConfiguration struct {
|
|||||||
type ServerConfigurations []ServerConfiguration
|
type ServerConfigurations []ServerConfiguration
|
||||||
|
|
||||||
{{#withCustomMiddlewareFunction}}
|
{{#withCustomMiddlewareFunction}}
|
||||||
// MiddlewareFunction provides way to implement custom middleware
|
// MiddlewareFunction provides way to implement custom middleware in the prepareRequest
|
||||||
type MiddlewareFunction func(*http.Request)
|
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}}
|
{{/withCustomMiddlewareFunction}}
|
||||||
// Configuration stores the configuration of the API client
|
// Configuration stores the configuration of the API client
|
||||||
type Configuration struct {
|
type Configuration struct {
|
||||||
@ -119,7 +125,9 @@ type Configuration struct {
|
|||||||
OperationServers map[string]ServerConfigurations
|
OperationServers map[string]ServerConfigurations
|
||||||
HTTPClient *http.Client
|
HTTPClient *http.Client
|
||||||
{{#withCustomMiddlewareFunction}}
|
{{#withCustomMiddlewareFunction}}
|
||||||
Middleware MiddlewareFunction
|
Middleware MiddlewareFunction
|
||||||
|
MiddlewareWithError MiddlewareFunctionWithError
|
||||||
|
ResponseMiddleware ResponseMiddlewareFunction
|
||||||
{{/withCustomMiddlewareFunction}}
|
{{/withCustomMiddlewareFunction}}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user