ipfs-cluster/api/rest/client/request.go
Hector Sanjuan 0a8edc17c9 Fix #260: Add REST API client and use it in ipfs-cluster-ctl
This adds the pakage api/rest/client which implements a go-client
for the REST API component. It also update the ipfs-cluster-ctl
tool to rely on it.

Originally, I wanted this to live it in it's own separate repository,
but the api client uses /api/types.go, which is part of cluster.

Therefore it would need to import all of cluster as a dependency.
ipfs-cluster-ctl would also need to import go-ipfs-cluster-api-client
as a dependency, creating circular gx deps which would be a mess to
maintain.

Only the splitting of cluster in multiple repositories (at least for
api, rest, ipfs-cluster-ctl, rest/client and test) would allow better
dependency management by allowing rest/client and the ctl tool
to only import what is needed, but this is something which brings
maintenance costs and can probably wait a bit until cluster is more stable.

License: MIT
Signed-off-by: Hector Sanjuan <code@hector.link>
2017-12-06 20:12:01 +01:00

77 lines
1.7 KiB
Go

package client
import (
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"strings"
"github.com/ipfs/ipfs-cluster/api"
)
func (c *Client) do(method, path string, body io.Reader, obj interface{}) *api.Error {
resp, err := c.doRequest(method, path, body)
if err != nil {
return &api.Error{Code: 0, Message: err.Error()}
}
return c.handleResponse(resp, obj)
}
func (c *Client) doRequest(method, path string, body io.Reader) (*http.Response, error) {
ctx, cancel := context.WithTimeout(c.ctx, c.config.Timeout)
defer cancel()
urlpath := c.urlPrefix + "/" + strings.TrimPrefix(path, "/")
logger.Debugf("%s: %s", method, urlpath)
r, err := http.NewRequest(method, urlpath, body)
if err != nil {
return nil, err
}
if c.config.Username != "" {
r.SetBasicAuth(c.config.Username, c.config.Password)
}
client := &http.Client{Transport: c.transport}
return client.Do(r.WithContext(ctx))
}
func (c *Client) handleResponse(resp *http.Response, obj interface{}) *api.Error {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &api.Error{Code: resp.StatusCode, Message: err.Error()}
}
logger.Debugf("Response body: %s", body)
switch {
case resp.StatusCode == http.StatusAccepted:
logger.Debug("Request accepted")
case resp.StatusCode == http.StatusNoContent:
logger.Debug("Request suceeded. Response has no content")
default:
if resp.StatusCode > 399 {
var apiErr api.Error
err = json.Unmarshal(body, &apiErr)
if err != nil {
return &api.Error{
Code: resp.StatusCode,
Message: err.Error(),
}
}
return &apiErr
}
err = json.Unmarshal(body, obj)
if err != nil {
return &api.Error{
Code: resp.StatusCode,
Message: err.Error(),
}
}
}
return nil
}