diff options
author | Maciej Wereski <m.wereski@partner.samsung.com> | 2018-06-26 14:50:20 +0200 |
---|---|---|
committer | Maciej Wereski <m.wereski@partner.samsung.com> | 2018-07-10 10:05:44 +0200 |
commit | 60817fcf2d0e46ed557cb0ffa9e905035dbcdc73 (patch) | |
tree | a98d3303d978f4fe9b39719bb9c6603f6e96ab75 /http/client/client.go | |
parent | bf1fee720ae596664d8f7c36337f8617db47842c (diff) | |
download | boruta-60817fcf2d0e46ed557cb0ffa9e905035dbcdc73.tar.gz boruta-60817fcf2d0e46ed557cb0ffa9e905035dbcdc73.tar.bz2 boruta-60817fcf2d0e46ed557cb0ffa9e905035dbcdc73.zip |
HTTP API Client: Add method to check worker state
GetWorkerState() is convenient method for checking status of worker. It
uses HEAD HTTP method, so it's faster than calling GetWorkerInfo and
checking boruta.WorkerInfo.State. As HEAD method is used it may be
harder to debug when an issue occurs, only HTTP status code is returned.
Change-Id: Ie58934e0c520c6033f0ff28e82ea43eb675353a2
Signed-off-by: Maciej Wereski <m.wereski@partner.samsung.com>
Diffstat (limited to 'http/client/client.go')
-rw-r--r-- | http/client/client.go | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/http/client/client.go b/http/client/client.go index a6e2f45..55dd690 100644 --- a/http/client/client.go +++ b/http/client/client.go @@ -145,6 +145,15 @@ func processResponse(resp *http.Response, val interface{}) error { } } +// checkStatus is a helper function that returns an error when HTTP response +// status is different than expected. +func checkStatus(shouldBe int, resp *http.Response) (err error) { + if resp.StatusCode != shouldBe { + err = errors.New("bad HTTP status: " + resp.Status) + } + return +} + // NewRequest creates new Boruta request. func (client *BorutaClient) NewRequest(caps boruta.Capabilities, priority boruta.Priority, owner boruta.UserInfo, validAfter time.Time, @@ -363,8 +372,21 @@ func (client *BorutaClient) GetRequestState(reqID boruta.ReqID) (boruta.ReqState if err != nil { return boruta.FAILED, err } - if resp.StatusCode != http.StatusNoContent { - return boruta.FAILED, errors.New("bad HTTP status: " + resp.Status) + if err = checkStatus(http.StatusNoContent, resp); err != nil { + return boruta.FAILED, err } return boruta.ReqState(resp.Header.Get("Boruta-Request-State")), nil } + +// GetWorkerState is convenient way to check state of a worker with given UUID. +func (client *BorutaClient) GetWorkerState(uuid boruta.WorkerUUID) (boruta.WorkerState, error) { + path := client.url + "workers/" + string(uuid) + resp, err := http.Head(path) + if err != nil { + return boruta.FAIL, err + } + if err = checkStatus(http.StatusNoContent, resp); err != nil { + return boruta.FAIL, err + } + return boruta.WorkerState(resp.Header.Get("Boruta-Worker-State")), nil +} |