-
Notifications
You must be signed in to change notification settings - Fork 20
All client methods in methods.go verify the response http status code #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
finevan
wants to merge
3
commits into
autobrr:main
Choose a base branch
from
finevan:finevan/all_methods_check_status
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,10 @@ func (c *Client) GetBuildInfoCtx(ctx context.Context) (BuildInfo, error) { | |
|
|
||
| defer drainAndClose(resp) | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return bi, errors.Wrap(ErrUnexpectedStatus, "could not get app build info; status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| if err = json.NewDecoder(resp.Body).Decode(&bi); err != nil { | ||
| return bi, errors.Wrap(err, "could not unmarshal body") | ||
| } | ||
|
|
@@ -207,6 +211,10 @@ func (c *Client) GetAppPreferencesCtx(ctx context.Context) (AppPreferences, erro | |
|
|
||
| defer drainAndClose(resp) | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return app, errors.Wrap(ErrUnexpectedStatus, "could not get app preferences; status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| if err := json.NewDecoder(resp.Body).Decode(&app); err != nil { | ||
| return app, errors.Wrap(err, "could not unmarshal body") | ||
| } | ||
|
|
@@ -321,6 +329,10 @@ func (c *Client) GetTorrentsCtx(ctx context.Context, o TorrentFilterOptions) ([] | |
|
|
||
| defer drainAndClose(resp) | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, errors.Wrap(ErrUnexpectedStatus, "could not get torrents; status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| var torrents []Torrent | ||
| if err := json.NewDecoder(resp.Body).Decode(&torrents); err != nil { | ||
| return nil, errors.Wrap(err, "could not unmarshal body") | ||
|
|
@@ -363,11 +375,23 @@ func (c *Client) GetTorrentPropertiesCtx(ctx context.Context, hash string) (Torr | |
| var prop TorrentProperties | ||
| resp, err := c.getCtx(ctx, "torrents/properties", opts) | ||
| if err != nil { | ||
| return prop, errors.Wrap(err, "could not get app preferences") | ||
| return prop, errors.Wrap(err, "could not get torrent properties") | ||
| } | ||
|
|
||
| defer drainAndClose(resp) | ||
|
|
||
| // HTTP Status Code Scenario | ||
| // 404 Torrent hash was not found | ||
| // 200 All other scenarios | ||
| switch resp.StatusCode { | ||
| case http.StatusOK: | ||
| break | ||
| case http.StatusNotFound: | ||
| return prop, errors.Wrap(ErrTorrentNotFound, "could not get torrent properties; torrent hash '%s' was not found", hash) | ||
| default: | ||
| return prop, errors.Wrap(ErrUnexpectedStatus, "could not get torrent properties; status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| if err := json.NewDecoder(resp.Body).Decode(&prop); err != nil { | ||
| return prop, errors.Wrap(err, "could not unmarshal body") | ||
| } | ||
|
|
@@ -387,6 +411,10 @@ func (c *Client) GetTorrentsRawCtx(ctx context.Context) (string, error) { | |
|
|
||
| defer drainAndClose(resp) | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return "", errors.Wrap(ErrUnexpectedStatus, "could not get torrents raw; status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| data, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return "", errors.Wrap(err, "could not get read body torrents raw") | ||
|
|
@@ -412,10 +440,14 @@ func (c *Client) GetTorrentTrackersCtx(ctx context.Context, hash string) ([]Torr | |
| defer drainAndClose(resp) | ||
|
|
||
| switch resp.StatusCode { | ||
| case http.StatusOK: | ||
| break | ||
| case http.StatusNotFound: | ||
| return nil, nil | ||
| case http.StatusForbidden: | ||
| return nil, nil | ||
| default: | ||
| return nil, errors.Wrap(ErrUnexpectedStatus, "could not get torrent trackers; status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| var trackers []TorrentTracker | ||
|
|
@@ -557,6 +589,10 @@ func (c *Client) GetTransferInfoCtx(ctx context.Context) (*TransferInfo, error) | |
|
|
||
| defer drainAndClose(resp) | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, errors.Wrap(ErrUnexpectedStatus, "could not get transfer info; status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| var info TransferInfo | ||
| if err := json.NewDecoder(resp.Body).Decode(&info); err != nil { | ||
| return nil, errors.Wrap(err, "could not unmarshal body") | ||
|
|
@@ -611,6 +647,10 @@ func (c *Client) SyncMainDataCtxWithRaw(ctx context.Context, rid int64) (*MainDa | |
|
|
||
| defer drainAndClose(resp) | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, nil, errors.Wrap(ErrUnexpectedStatus, "could not get main data; status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| rp, wp := io.Pipe() | ||
| var rawData map[string]interface{} | ||
| var mapErr error | ||
|
|
@@ -986,6 +1026,10 @@ func (c *Client) GetCategoriesCtx(ctx context.Context) (map[string]Category, err | |
|
|
||
| defer drainAndClose(resp) | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, errors.Wrap(ErrUnexpectedStatus, "could not get categories; status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| m := make(map[string]Category) | ||
| if err := json.NewDecoder(resp.Body).Decode(&m); err != nil { | ||
| return nil, errors.Wrap(err, "could not unmarshal body") | ||
|
|
@@ -1009,6 +1053,15 @@ func (c *Client) GetFilesInformationCtx(ctx context.Context, hash string) (*Torr | |
|
|
||
| defer drainAndClose(resp) | ||
|
|
||
| switch resp.StatusCode { | ||
| case http.StatusOK: | ||
| break | ||
| case http.StatusNotFound: | ||
| return nil, errors.Wrap(ErrTorrentNotFound, "could not get files info; torrent hash not found: %s", hash) | ||
| default: | ||
| return nil, errors.Wrap(ErrUnexpectedStatus, "could not get files info; torrent hash: %s, status code: %d", hash, resp.StatusCode) | ||
| } | ||
|
|
||
| var info TorrentFiles | ||
| if err := json.NewDecoder(resp.Body).Decode(&info); err != nil { | ||
| return nil, errors.Wrap(err, "could not unmarshal body") | ||
|
|
@@ -1075,6 +1128,15 @@ func (c *Client) ExportTorrentCtx(ctx context.Context, hash string) ([]byte, err | |
|
|
||
| defer drainAndClose(resp) | ||
|
|
||
| switch resp.StatusCode { | ||
| case http.StatusOK: | ||
| break | ||
| case http.StatusNotFound: | ||
| return nil, errors.Wrap(ErrTorrentNotFound, "could not get export; torrent hash not found: %v", hash) | ||
| default: | ||
| return nil, errors.Wrap(ErrUnexpectedStatus, "could not get export; torrent hash: %v | status code: %d", hash, resp.StatusCode) | ||
| } | ||
|
|
||
| return io.ReadAll(resp.Body) | ||
| } | ||
|
|
||
|
|
@@ -1189,6 +1251,10 @@ func (c *Client) GetTagsCtx(ctx context.Context) ([]string, error) { | |
|
|
||
| defer drainAndClose(resp) | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, errors.Wrap(ErrUnexpectedStatus, "could not get tags; status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| var m []string | ||
| if err := json.NewDecoder(resp.Body).Decode(&m); err != nil { | ||
| return nil, errors.Wrap(err, "could not unmarshal body") | ||
|
|
@@ -1654,6 +1720,10 @@ func (c *Client) GetAlternativeSpeedLimitsModeCtx(ctx context.Context) (bool, er | |
|
|
||
| defer drainAndClose(resp) | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return m, errors.Wrap(ErrUnexpectedStatus, "could not get alternative speed limits mode; status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| var d int64 | ||
| if err := json.NewDecoder(resp.Body).Decode(&d); err != nil { | ||
| return m, errors.Wrap(err, "could not unmarshal body") | ||
|
|
@@ -1702,6 +1772,10 @@ func (c *Client) GetGlobalDownloadLimitCtx(ctx context.Context) (int64, error) { | |
|
|
||
| defer drainAndClose(resp) | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return m, errors.Wrap(ErrUnexpectedStatus, "could not get global download limit; status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| if err := json.NewDecoder(resp.Body).Decode(&m); err != nil { | ||
| return m, errors.Wrap(err, "could not unmarshal body") | ||
| } | ||
|
|
@@ -1748,6 +1822,10 @@ func (c *Client) GetGlobalUploadLimitCtx(ctx context.Context) (int64, error) { | |
|
|
||
| defer drainAndClose(resp) | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return m, errors.Wrap(ErrUnexpectedStatus, "could not get global upload limit; status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| if err := json.NewDecoder(resp.Body).Decode(&m); err != nil { | ||
| return m, errors.Wrap(err, "could not unmarshal body") | ||
| } | ||
|
|
@@ -1983,10 +2061,8 @@ func (c *Client) SetTorrentShareLimitCtx(ctx context.Context, hashes []string, r | |
| case http.StatusBadRequest: | ||
| return ErrInvalidShareLimit | ||
| default: | ||
| errors.Wrap(ErrUnexpectedStatus, "could not set share limits; hashes: %v | ratioLimit: %v | seedingTimeLimit: %v | inactiveSeedingTimeLimit %v | status code: %d", hashes, ratioLimit, seedingTimeLimit, inactiveSeedingTimeLimit, resp.StatusCode) | ||
| return errors.Wrap(ErrUnexpectedStatus, "could not set share limits; hashes: %v | ratioLimit: %v | seedingTimeLimit: %v | inactiveSeedingTimeLimit %v | status code: %d", hashes, ratioLimit, seedingTimeLimit, inactiveSeedingTimeLimit, resp.StatusCode) | ||
|
Comment on lines
-1986
to
+2064
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously this created an error that was not returned, and fell through to the |
||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // SetTorrentUploadLimit set upload limit for torrent specified by hashes | ||
|
|
@@ -2027,6 +2103,10 @@ func (c *Client) GetAppVersionCtx(ctx context.Context) (string, error) { | |
|
|
||
| defer drainAndClose(resp) | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return "", errors.Wrap(ErrUnexpectedStatus, "could not get app version; status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return "", errors.Wrap(err, "could not read body") | ||
|
|
@@ -2204,6 +2284,13 @@ func (c *Client) GetWebAPIVersionCtx(ctx context.Context) (string, error) { | |
|
|
||
| defer drainAndClose(resp) | ||
|
|
||
| // HTTP Status Code Scenario | ||
| // 200 All scenarios | ||
| // Non-200 response codes are expected when a reverse proxy is used in front of qBittorrent API. | ||
| if resp.StatusCode != http.StatusOK { | ||
| return "", errors.Wrap(ErrUnexpectedStatus, "could not get webapi version; status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return "", errors.Wrap(err, "could not read body") | ||
|
|
@@ -2226,6 +2313,10 @@ func (c *Client) GetLogsCtx(ctx context.Context) ([]Log, error) { | |
|
|
||
| defer drainAndClose(resp) | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, errors.Wrap(ErrUnexpectedStatus, "could not get main client logs; status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| var m []Log | ||
| if err := json.NewDecoder(resp.Body).Decode(&m); err != nil { | ||
| return nil, errors.Wrap(err, "could not unmarshal body") | ||
|
|
@@ -2247,6 +2338,10 @@ func (c *Client) GetPeerLogsCtx(ctx context.Context) ([]PeerLog, error) { | |
|
|
||
| defer drainAndClose(resp) | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, errors.Wrap(ErrUnexpectedStatus, "could not get peer logs; status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| var m []PeerLog | ||
| if err := json.NewDecoder(resp.Body).Decode(&m); err != nil { | ||
| return m, errors.Wrap(err, "could not unmarshal body") | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed a likely copy/paste error in the message