Add "before request" and "after response" hooks to client

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-08-05 23:19:35 -04:00
parent 356979a7a4
commit 76d92c5885

View File

@ -38,15 +38,19 @@ type PortainerClient interface {
GetStackFileContent(stackId uint32) (content string, err error)
GetEndpointDockerInfo(endpointId string) (info map[string]interface{}, err error)
GetStatus() (Status, error)
BeforeRequest(hook func(req *http.Request) (err error))
AfterResponse(hook func(resp *http.Response) (err error))
}
type portainerClientImp struct {
httpClient *http.Client
url *url.URL
user string
password string
token string
doNotUseToken bool
httpClient *http.Client
url *url.URL
user string
password string
token string
doNotUseToken bool
beforeRequestHooks []func(req *http.Request) (err error)
afterResponseHooks []func(resp *http.Response) (err error)
}
// Check if an http.Response object has errors
@ -101,6 +105,14 @@ func (n *portainerClientImp) do(uri, method string, request io.Reader, requestTy
req.Header.Set("Authorization", "Bearer "+n.token)
}
// Run all "before request" hooks
for i := 0; i < len(n.beforeRequestHooks); i++ {
err = n.beforeRequestHooks[i](req)
if err != nil {
return
}
}
util.PrintDebugRequest("Request", req)
resp, err = n.httpClient.Do(req)
@ -108,6 +120,14 @@ func (n *portainerClientImp) do(uri, method string, request io.Reader, requestTy
return
}
// Run all "after response" hooks
for i := 0; i < len(n.afterResponseHooks); i++ {
err = n.afterResponseHooks[i](resp)
if err != nil {
return
}
}
err = checkResponseForErrors(resp)
if err != nil {
return
@ -146,6 +166,14 @@ func (n *portainerClientImp) doJSON(uri, method string, request interface{}, res
return nil
}
func (n *portainerClientImp) BeforeRequest(hook func(req *http.Request) (err error)) {
n.beforeRequestHooks = append(n.beforeRequestHooks, hook)
}
func (n *portainerClientImp) AfterResponse(hook func(resp *http.Response) (err error)) {
n.afterResponseHooks = append(n.afterResponseHooks, hook)
}
// Authenticate a user to get an auth token
func (n *portainerClientImp) Authenticate() (token string, err error) {
util.PrintVerbose("Getting auth token...")