From 74c95ed34b641ebd53748d5cf20f31e676e6a642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Mon, 27 Jul 2020 18:41:27 -0700 Subject: [PATCH] API client: Do not return body for response with no content --- client/client.go | 56 ++++++++++------------------------------------- client/request.go | 13 +++++++++-- 2 files changed, 22 insertions(+), 47 deletions(-) diff --git a/client/client.go b/client/client.go index a3f22ed0..9e1cffbc 100644 --- a/client/client.go +++ b/client/client.go @@ -130,12 +130,7 @@ func (c *Client) UpdateUser(userID int64, userChanges *UserModification) (*User, // DeleteUser removes a user from the system. func (c *Client) DeleteUser(userID int64) error { - body, err := c.request.Delete(fmt.Sprintf("/v1/users/%d", userID)) - if err != nil { - return err - } - body.Close() - return nil + return c.request.Delete(fmt.Sprintf("/v1/users/%d", userID)) } // Discover try to find subscriptions from a website. @@ -214,13 +209,7 @@ func (c *Client) UpdateCategory(categoryID int64, title string) (*Category, erro // DeleteCategory removes a category. func (c *Client) DeleteCategory(categoryID int64) error { - body, err := c.request.Delete(fmt.Sprintf("/v1/categories/%d", categoryID)) - if err != nil { - return err - } - defer body.Close() - - return nil + return c.request.Delete(fmt.Sprintf("/v1/categories/%d", categoryID)) } // Feeds gets all feeds. @@ -322,32 +311,19 @@ func (c *Client) UpdateFeed(feedID int64, feedChanges *FeedModification) (*Feed, // RefreshAllFeeds refreshes all feeds. func (c *Client) RefreshAllFeeds() error { - body, err := c.request.Put(fmt.Sprintf("/v1/feeds/refresh"), nil) - if err != nil { - return err - } - body.Close() - return nil + _, err := c.request.Put(fmt.Sprintf("/v1/feeds/refresh"), nil) + return err } // RefreshFeed refreshes a feed. func (c *Client) RefreshFeed(feedID int64) error { - body, err := c.request.Put(fmt.Sprintf("/v1/feeds/%d/refresh", feedID), nil) - if err != nil { - return err - } - body.Close() - return nil + _, err := c.request.Put(fmt.Sprintf("/v1/feeds/%d/refresh", feedID), nil) + return err } // DeleteFeed removes a feed. func (c *Client) DeleteFeed(feedID int64) error { - body, err := c.request.Delete(fmt.Sprintf("/v1/feeds/%d", feedID)) - if err != nil { - return err - } - body.Close() - return nil + return c.request.Delete(fmt.Sprintf("/v1/feeds/%d", feedID)) } // FeedIcon gets a feed icon. @@ -446,24 +422,14 @@ func (c *Client) UpdateEntries(entryIDs []int64, status string) error { Status string `json:"status"` } - body, err := c.request.Put("/v1/entries", &payload{EntryIDs: entryIDs, Status: status}) - if err != nil { - return err - } - body.Close() - - return nil + _, err := c.request.Put("/v1/entries", &payload{EntryIDs: entryIDs, Status: status}) + return err } // ToggleBookmark toggles entry bookmark value. func (c *Client) ToggleBookmark(entryID int64) error { - body, err := c.request.Put(fmt.Sprintf("/v1/entries/%d/bookmark", entryID), nil) - if err != nil { - return err - } - body.Close() - - return nil + _, err := c.request.Put(fmt.Sprintf("/v1/entries/%d/bookmark", entryID), nil) + return err } func buildFilterQueryString(path string, filter *Filter) string { diff --git a/client/request.go b/client/request.go index 03372973..bf45f2c7 100644 --- a/client/request.go +++ b/client/request.go @@ -57,8 +57,9 @@ func (r *request) Put(path string, data interface{}) (io.ReadCloser, error) { return r.execute(http.MethodPut, path, data) } -func (r *request) Delete(path string) (io.ReadCloser, error) { - return r.execute(http.MethodDelete, path, nil) +func (r *request) Delete(path string) error { + _, err := r.execute(http.MethodDelete, path, nil) + return err } func (r *request) execute(method, path string, data interface{}) (io.ReadCloser, error) { @@ -98,13 +99,20 @@ func (r *request) execute(method, path string, data interface{}) (io.ReadCloser, switch response.StatusCode { case http.StatusUnauthorized: + response.Body.Close() return nil, ErrNotAuthorized case http.StatusForbidden: + response.Body.Close() return nil, ErrForbidden case http.StatusInternalServerError: + response.Body.Close() return nil, ErrServerError case http.StatusNotFound: + response.Body.Close() return nil, ErrNotFound + case http.StatusNoContent: + response.Body.Close() + return nil, nil case http.StatusBadRequest: defer response.Body.Close() @@ -118,6 +126,7 @@ func (r *request) execute(method, path string, data interface{}) (io.ReadCloser, } if response.StatusCode > 400 { + response.Body.Close() return nil, fmt.Errorf("miniflux: status code=%d", response.StatusCode) }