|
1 | 1 | package unifiapi |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | 4 | "context" |
6 | | - "encoding/json" |
| 5 | + "encoding/json/v2" |
7 | 6 | "fmt" |
| 7 | + "io" |
8 | 8 | "log/slog" |
9 | 9 | "net/http" |
10 | 10 | "net/netip" |
11 | 11 | "net/url" |
12 | 12 | "time" |
| 13 | + "uuid" |
13 | 14 |
|
14 | 15 | "github.com/database64128/ddns-go/internal/httpreq" |
15 | 16 | ) |
@@ -46,8 +47,8 @@ func NewClient(client *http.Client, baseURL, apiKey string) (*Client, error) { |
46 | 47 | } |
47 | 48 |
|
48 | 49 | // GetDeviceIPAddress returns the IP address of the specified device in the specified site. |
49 | | -func (c *Client) GetDeviceIPAddress(ctx context.Context, siteID, deviceID string) (netip.Addr, error) { |
50 | | - deviceURL, err := url.JoinPath(c.sitesURL, siteID, "devices", deviceID) |
| 50 | +func (c *Client) GetDeviceIPAddress(ctx context.Context, siteID, deviceID uuid.UUID) (netip.Addr, error) { |
| 51 | + deviceURL, err := url.JoinPath(c.sitesURL, siteID.String(), "devices", deviceID.String()) |
51 | 52 | if err != nil { |
52 | 53 | return netip.Addr{}, fmt.Errorf("failed to join device URL: %w", err) |
53 | 54 | } |
@@ -82,21 +83,17 @@ func clientDo(client *http.Client, apiKey string, newRequest func() (*http.Reque |
82 | 83 | defer resp.Body.Close() |
83 | 84 |
|
84 | 85 | const maxResponseBodySize = 128 * 1024 * 1024 // 128 MiB |
85 | | - var buf bytes.Buffer |
86 | | - if err := httpreq.ReadResponseBody(&buf, resp, maxResponseBodySize); err != nil { |
87 | | - return fmt.Errorf("failed to read response: %w", err) |
88 | | - } |
89 | | - bodyBytes := buf.Bytes() |
| 86 | + r := io.LimitReader(resp.Body, maxResponseBodySize) |
90 | 87 |
|
91 | 88 | if resp.StatusCode != http.StatusOK { |
92 | 89 | var apiErr Error |
93 | | - if err := json.Unmarshal(bodyBytes, &apiErr); err != nil { |
| 90 | + if err := json.UnmarshalRead(r, &apiErr); err != nil { |
94 | 91 | return fmt.Errorf("failed to unmarshal API error response: %w", err) |
95 | 92 | } |
96 | 93 | return &apiErr |
97 | 94 | } |
98 | 95 |
|
99 | | - if err := json.Unmarshal(bodyBytes, v); err != nil { |
| 96 | + if err := json.UnmarshalRead(r, v); err != nil { |
100 | 97 | return fmt.Errorf("failed to unmarshal response: %w", err) |
101 | 98 | } |
102 | 99 |
|
|
0 commit comments