mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-17 14:09:44 +02:00
This is a large-scale refactoring across the codebase that replaces the custom `gperr.Error` type with Go's standard `error` interface. The changes include: - Replacing `gperr.Error` return types with `error` in function signatures - Using `errors.New()` and `fmt.Errorf()` instead of `gperr.New()` and `gperr.Errorf()` - Using `%w` format verb for error wrapping instead of `.With()` method - Replacing `gperr.Subject()` calls with `gperr.PrependSubject()` - Converting error logging from `gperr.Log*()` functions to zerolog's `.Err().Msg()` pattern - Update NewLogger to handle multiline error message - Updating `goutils` submodule to latest commit This refactoring aligns with Go idioms and removes the dependency on custom error handling abstractions in favor of standard library patterns.
69 lines
1.4 KiB
Go
69 lines
1.4 KiB
Go
package qbittorrent
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/bytedance/sonic"
|
|
"github.com/yusing/godoxy/internal/homepage/widgets"
|
|
)
|
|
|
|
type Client struct {
|
|
URL string
|
|
Username string
|
|
Password string
|
|
}
|
|
|
|
func (c *Client) Initialize(ctx context.Context, url string, cfg map[string]any) error {
|
|
c.URL = url
|
|
c.Username = cfg["username"].(string)
|
|
c.Password = cfg["password"].(string)
|
|
|
|
_, err := c.Version(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *Client) doRequest(ctx context.Context, method, endpoint string, query url.Values, body io.Reader) (*http.Response, error) {
|
|
req, err := http.NewRequestWithContext(ctx, method, c.URL+endpoint+query.Encode(), body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if c.Username != "" && c.Password != "" {
|
|
req.SetBasicAuth(c.Username, c.Password)
|
|
}
|
|
|
|
resp, err := widgets.HTTPClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("%w: %d %s", widgets.ErrHTTPStatus, resp.StatusCode, resp.Status)
|
|
}
|
|
|
|
return resp, nil
|
|
}
|
|
|
|
func jsonRequest[T any](ctx context.Context, client *Client, endpoint string, query url.Values) (result T, err error) {
|
|
resp, err := client.doRequest(ctx, http.MethodGet, endpoint, query, nil)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
err = sonic.ConfigDefault.NewDecoder(resp.Body).Decode(&result)
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
|
|
return result, nil
|
|
}
|