Lint source code

pull/18/head
Aloïs Micard 4 years ago
parent b5b58a8d19
commit 45a9848395
No known key found for this signature in database
GPG Key ID: 1A0EB82F071F5EFE

@ -41,10 +41,12 @@ func execute(ctx *cli.Context) error {
apiURL := fmt.Sprintf("%s/v1/urls", ctx.String("api-uri"))
c := http.Client{}
res, err := c.JsonPost(apiURL, ctx.String("url"), nil)
res, err := c.JSONPost(apiURL, ctx.String("url"), nil)
if err != nil {
logrus.Errorf("Unable to publish URL: %s", err)
logrus.Errorf("Received status code: %d", res.StatusCode)
if res != nil {
logrus.Errorf("Received status code: %d", res.StatusCode)
}
return err
}

@ -71,14 +71,16 @@ func handleMessage(httpClient *http.Client, apiURI string) natsutil.MsgHandler {
logrus.Debugf("Processing resource: %s", resMsg.URL)
url := fmt.Sprintf("%s/v1/resources", apiURI)
r, err := httpClient.JsonPost(url, &proto.ResourceDto{
r, err := httpClient.JSONPost(url, &proto.ResourceDto{
URL: resMsg.URL,
Body: resMsg.Body,
}, nil)
if err != nil || r.StatusCode != http.StatusCreated {
logrus.Errorf("Error while sending resource to the API: %s", err)
logrus.Errorf("Received status code: %d", r.StatusCode)
if r != nil {
logrus.Errorf("Received status code: %d", r.StatusCode)
}
return err
}

@ -100,7 +100,7 @@ func handleMessage(httpClient *http.Client, apiURI string) natsutil.MsgHandler {
apiURL := fmt.Sprintf("%s/v1/resources?url=%s", apiURI, b64URI)
var urls []proto.ResourceDto
r, err := httpClient.JsonGet(apiURL, &urls)
r, err := httpClient.JSONGet(apiURL, &urls)
if err != nil {
logrus.Errorf("Error while searching URL: %s", err)
if r != nil {

@ -7,15 +7,18 @@ import (
"net/http"
)
// StatusCreated HTTP 201
const StatusCreated = 201
const contentTypeJson = "application/json"
const contentTypeJSON = "application/json"
// Client an http client with built-in JSON (de)serialization
type Client struct {
client http.Client
}
func (c *Client) JsonGet(url string, response interface{}) (*http.Response, error) {
// JSONGet perform a GET request and serialize response body into given interface if any
func (c *Client) JSONGet(url string, response interface{}) (*http.Response, error) {
logrus.Tracef("GET %s", url)
r, err := c.client.Get(url)
@ -30,19 +33,20 @@ func (c *Client) JsonGet(url string, response interface{}) (*http.Response, erro
return r, nil
}
func (c *Client) JsonPost(url string, body, response interface{}) (*http.Response, error) {
// JSONPost perform a POST request and serialize request/response body into given interface if any
func (c *Client) JSONPost(url string, request, response interface{}) (*http.Response, error) {
logrus.Tracef("POST %s", url)
var err error
var b []byte
if body != nil {
b, err = json.Marshal(body)
if request != nil {
b, err = json.Marshal(request)
if err != nil {
return nil, err
}
}
r, err := c.client.Post(url, contentTypeJson, bytes.NewBuffer(b))
r, err := c.client.Post(url, contentTypeJSON, bytes.NewBuffer(b))
if err != nil {
return nil, err
}

Loading…
Cancel
Save