mirror of
https://gitlab.com/psuapp/psu.git
synced 2024-08-30 18:12:34 +00:00
Add "before request" and "after response" hooks to client
This commit is contained in:
parent
356979a7a4
commit
76d92c5885
@ -38,15 +38,19 @@ type PortainerClient interface {
|
|||||||
GetStackFileContent(stackId uint32) (content string, err error)
|
GetStackFileContent(stackId uint32) (content string, err error)
|
||||||
GetEndpointDockerInfo(endpointId string) (info map[string]interface{}, err error)
|
GetEndpointDockerInfo(endpointId string) (info map[string]interface{}, err error)
|
||||||
GetStatus() (Status, error)
|
GetStatus() (Status, error)
|
||||||
|
BeforeRequest(hook func(req *http.Request) (err error))
|
||||||
|
AfterResponse(hook func(resp *http.Response) (err error))
|
||||||
}
|
}
|
||||||
|
|
||||||
type portainerClientImp struct {
|
type portainerClientImp struct {
|
||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
url *url.URL
|
url *url.URL
|
||||||
user string
|
user string
|
||||||
password string
|
password string
|
||||||
token string
|
token string
|
||||||
doNotUseToken bool
|
doNotUseToken bool
|
||||||
|
beforeRequestHooks []func(req *http.Request) (err error)
|
||||||
|
afterResponseHooks []func(resp *http.Response) (err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if an http.Response object has errors
|
// 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)
|
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)
|
util.PrintDebugRequest("Request", req)
|
||||||
|
|
||||||
resp, err = n.httpClient.Do(req)
|
resp, err = n.httpClient.Do(req)
|
||||||
@ -108,6 +120,14 @@ func (n *portainerClientImp) do(uri, method string, request io.Reader, requestTy
|
|||||||
return
|
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)
|
err = checkResponseForErrors(resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -146,6 +166,14 @@ func (n *portainerClientImp) doJSON(uri, method string, request interface{}, res
|
|||||||
return nil
|
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
|
// Authenticate a user to get an auth token
|
||||||
func (n *portainerClientImp) Authenticate() (token string, err error) {
|
func (n *portainerClientImp) Authenticate() (token string, err error) {
|
||||||
util.PrintVerbose("Getting auth token...")
|
util.PrintVerbose("Getting auth token...")
|
||||||
|
Loading…
Reference in New Issue
Block a user