diff --git a/server/api/api.go b/server/api/api.go index eb73c04b..df8ea444 100644 --- a/server/api/api.go +++ b/server/api/api.go @@ -130,7 +130,7 @@ func (h *Handler) setLink(w http.ResponseWriter, r *http.Request) { func (h *Handler) deleteLink(w http.ResponseWriter, r *http.Request) { autolinkName := r.URL.Query().Get(autolinkclient.AutolinkNameQueryParam) if autolinkName == "" { - h.handleError(w, errors.New("autolink name or pattern should not be empty")) + h.handleError(w, errors.New("autolink name should not be empty")) return } @@ -140,6 +140,7 @@ func (h *Handler) deleteLink(w http.ResponseWriter, r *http.Request) { if links[i].Name == autolinkName { links = append(links[:i], links[i+1:]...) found = true + break } } @@ -164,21 +165,19 @@ func (h *Handler) getLinks(w http.ResponseWriter, r *http.Request) { return } - found := false - var autolinks []autolink.Autolink + var autolink *autolink.Autolink for _, link := range links { if link.Name == autolinkName { - autolinks = append(autolinks, link) - found = true + autolink = &link + break } } - - if !found { + if autolink == nil { h.handleError(w, errors.Errorf("no autolink found with name %s", autolinkName)) return } - h.handleSendingJSONContent(w, autolinks) + h.handleSendingJSONContent(w, autolink) return } diff --git a/server/autolinkclient/client.go b/server/autolinkclient/client.go index 9ee6d34b..b348e886 100644 --- a/server/autolinkclient/client.go +++ b/server/autolinkclient/client.go @@ -53,6 +53,7 @@ func (c *Client) Add(links ...autolink.Autolink) error { if err != nil { return err } + defer resp.Body.Close() if resp.StatusCode != http.StatusOK { respBody, _ := ioutil.ReadAll(resp.Body) @@ -73,6 +74,7 @@ func (c *Client) Delete(links ...string) error { if err != nil { return err } + defer resp.Body.Close() if resp.StatusCode != http.StatusOK { respBody, _ := ioutil.ReadAll(resp.Body) @@ -83,7 +85,7 @@ func (c *Client) Delete(links ...string) error { return nil } -func (c *Client) Get(autolinkName string) ([]*autolink.Autolink, error) { +func (c *Client) Get(autolinkName string) (*autolink.Autolink, error) { queryParams := url.Values{ AutolinkNameQueryParam: {autolinkName}, } @@ -92,17 +94,18 @@ func (c *Client) Get(autolinkName string) ([]*autolink.Autolink, error) { if err != nil { return nil, err } + defer resp.Body.Close() + + respBody, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } - var respBody []byte if resp.StatusCode != http.StatusOK { - respBody, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err - } return nil, fmt.Errorf("unable to get the link %s. Error: %v, %v", autolinkName, resp.StatusCode, string(respBody)) } - var response []*autolink.Autolink + var response *autolink.Autolink if err = json.Unmarshal(respBody, &response); err != nil { return nil, err }