refactor: Improve endpoint robustness and client clarity

Refactored client request parameter naming and enhanced endpoint methods for better validation and idiomatic behavior.

- Renamed `URL` parameter to `requestURL` in `Client.Request` for improved clarity and to avoid potential naming conflicts.
- Added validation to `BaseEndpoint.GetByID` to prevent queries with an ID of 0, returning an error.
- Modified `BaseEndpoint.GetByIDs` to return an empty slice (`[]*T{}`) instead of an error when no IDs are provided, aligning with common Go idioms for empty result sets.
- Enhanced `BaseEndpoint.Count` method to return an error if the API reports a count of 0, ensuring that a successful count operation always yields a positive result.
This commit is contained in:
2025-11-03 18:25:53 +11:00
parent 7ef9cb37e6
commit aaf697a005
2 changed files with 13 additions and 5 deletions

View File

@@ -113,7 +113,7 @@ func NewWithFlaresolverr(clientID, clientSecret string, f *flaresolverr.Flaresol
return c
}
func (g *Client) Request(ctx context.Context, method string, URL string, dataBody any) (*resty.Response, error) {
func (g *Client) Request(ctx context.Context, method string, requestURL string, dataBody any) (*resty.Response, error) {
err := g.limiter.Wait(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get rate limiter token: %w", err)
@@ -129,14 +129,14 @@ func (g *Client) Request(ctx context.Context, method string, URL string, dataBod
"Authorization": "Bearer " + t,
"User-Agent": "",
"Content-Type": "text/plain",
}).Execute(strings.ToUpper(method), URL)
}).Execute(strings.ToUpper(method), requestURL)
if resp.StatusCode() != 200 {
return nil, fmt.Errorf("failed to request, expected 200 but got: %v", resp.StatusCode())
}
if err != nil {
return nil, fmt.Errorf("failed to request: %s: %w", URL, err)
return nil, fmt.Errorf("failed to request: %s: %w", requestURL, err)
}
return resp, nil
}