1
0
Fork 0

API client: Do not return body for response with no content

This commit is contained in:
Frédéric Guillot 2020-07-27 18:41:27 -07:00
parent 82b3ea31e6
commit 74c95ed34b
2 changed files with 22 additions and 47 deletions

View file

@ -130,12 +130,7 @@ func (c *Client) UpdateUser(userID int64, userChanges *UserModification) (*User,
// DeleteUser removes a user from the system. // DeleteUser removes a user from the system.
func (c *Client) DeleteUser(userID int64) error { func (c *Client) DeleteUser(userID int64) error {
body, err := c.request.Delete(fmt.Sprintf("/v1/users/%d", userID)) return c.request.Delete(fmt.Sprintf("/v1/users/%d", userID))
if err != nil {
return err
}
body.Close()
return nil
} }
// Discover try to find subscriptions from a website. // 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. // DeleteCategory removes a category.
func (c *Client) DeleteCategory(categoryID int64) error { func (c *Client) DeleteCategory(categoryID int64) error {
body, err := c.request.Delete(fmt.Sprintf("/v1/categories/%d", categoryID)) return c.request.Delete(fmt.Sprintf("/v1/categories/%d", categoryID))
if err != nil {
return err
}
defer body.Close()
return nil
} }
// Feeds gets all feeds. // Feeds gets all feeds.
@ -322,32 +311,19 @@ func (c *Client) UpdateFeed(feedID int64, feedChanges *FeedModification) (*Feed,
// RefreshAllFeeds refreshes all feeds. // RefreshAllFeeds refreshes all feeds.
func (c *Client) RefreshAllFeeds() error { func (c *Client) RefreshAllFeeds() error {
body, err := c.request.Put(fmt.Sprintf("/v1/feeds/refresh"), nil) _, err := c.request.Put(fmt.Sprintf("/v1/feeds/refresh"), nil)
if err != nil {
return err return err
}
body.Close()
return nil
} }
// RefreshFeed refreshes a feed. // RefreshFeed refreshes a feed.
func (c *Client) RefreshFeed(feedID int64) error { func (c *Client) RefreshFeed(feedID int64) error {
body, err := c.request.Put(fmt.Sprintf("/v1/feeds/%d/refresh", feedID), nil) _, err := c.request.Put(fmt.Sprintf("/v1/feeds/%d/refresh", feedID), nil)
if err != nil {
return err return err
}
body.Close()
return nil
} }
// DeleteFeed removes a feed. // DeleteFeed removes a feed.
func (c *Client) DeleteFeed(feedID int64) error { func (c *Client) DeleteFeed(feedID int64) error {
body, err := c.request.Delete(fmt.Sprintf("/v1/feeds/%d", feedID)) return c.request.Delete(fmt.Sprintf("/v1/feeds/%d", feedID))
if err != nil {
return err
}
body.Close()
return nil
} }
// FeedIcon gets a feed icon. // FeedIcon gets a feed icon.
@ -446,24 +422,14 @@ func (c *Client) UpdateEntries(entryIDs []int64, status string) error {
Status string `json:"status"` Status string `json:"status"`
} }
body, err := c.request.Put("/v1/entries", &payload{EntryIDs: entryIDs, Status: status}) _, err := c.request.Put("/v1/entries", &payload{EntryIDs: entryIDs, Status: status})
if err != nil {
return err return err
}
body.Close()
return nil
} }
// ToggleBookmark toggles entry bookmark value. // ToggleBookmark toggles entry bookmark value.
func (c *Client) ToggleBookmark(entryID int64) error { func (c *Client) ToggleBookmark(entryID int64) error {
body, err := c.request.Put(fmt.Sprintf("/v1/entries/%d/bookmark", entryID), nil) _, err := c.request.Put(fmt.Sprintf("/v1/entries/%d/bookmark", entryID), nil)
if err != nil {
return err return err
}
body.Close()
return nil
} }
func buildFilterQueryString(path string, filter *Filter) string { func buildFilterQueryString(path string, filter *Filter) string {

View file

@ -57,8 +57,9 @@ func (r *request) Put(path string, data interface{}) (io.ReadCloser, error) {
return r.execute(http.MethodPut, path, data) return r.execute(http.MethodPut, path, data)
} }
func (r *request) Delete(path string) (io.ReadCloser, error) { func (r *request) Delete(path string) error {
return r.execute(http.MethodDelete, path, nil) _, err := r.execute(http.MethodDelete, path, nil)
return err
} }
func (r *request) execute(method, path string, data interface{}) (io.ReadCloser, error) { 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 { switch response.StatusCode {
case http.StatusUnauthorized: case http.StatusUnauthorized:
response.Body.Close()
return nil, ErrNotAuthorized return nil, ErrNotAuthorized
case http.StatusForbidden: case http.StatusForbidden:
response.Body.Close()
return nil, ErrForbidden return nil, ErrForbidden
case http.StatusInternalServerError: case http.StatusInternalServerError:
response.Body.Close()
return nil, ErrServerError return nil, ErrServerError
case http.StatusNotFound: case http.StatusNotFound:
response.Body.Close()
return nil, ErrNotFound return nil, ErrNotFound
case http.StatusNoContent:
response.Body.Close()
return nil, nil
case http.StatusBadRequest: case http.StatusBadRequest:
defer response.Body.Close() defer response.Body.Close()
@ -118,6 +126,7 @@ func (r *request) execute(method, path string, data interface{}) (io.ReadCloser,
} }
if response.StatusCode > 400 { if response.StatusCode > 400 {
response.Body.Close()
return nil, fmt.Errorf("miniflux: status code=%d", response.StatusCode) return nil, fmt.Errorf("miniflux: status code=%d", response.StatusCode)
} }