Skip to content

Commit 2c71f0c

Browse files
committed
moar
1 parent 613f0f9 commit 2c71f0c

File tree

1 file changed

+20
-48
lines changed

1 file changed

+20
-48
lines changed

server/lib/nekoclient/client.go

Lines changed: 20 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package nekoclient
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
7-
"io"
86
"net/http"
97
"sync"
108

@@ -14,7 +12,7 @@ import (
1412
// AuthClient wraps the Neko OpenAPI client and handles authentication automatically.
1513
// It manages token caching and refresh on 401 responses.
1614
type AuthClient struct {
17-
client *nekooapi.Client
15+
client *nekooapi.ClientWithResponses
1816
tokenMu sync.Mutex
1917
token string
2018
username string
@@ -23,7 +21,7 @@ type AuthClient struct {
2321

2422
// NewAuthClient creates a new authenticated Neko client.
2523
func NewAuthClient(baseURL, username, password string) (*AuthClient, error) {
26-
client, err := nekooapi.NewClient(baseURL)
24+
client, err := nekooapi.NewClientWithResponses(baseURL)
2725
if err != nil {
2826
return nil, fmt.Errorf("failed to create neko client: %w", err)
2927
}
@@ -49,27 +47,20 @@ func (c *AuthClient) ensureToken(ctx context.Context) error {
4947
Password: &c.password,
5048
}
5149

52-
resp, err := c.client.Login(ctx, loginReq)
50+
resp, err := c.client.LoginWithResponse(ctx, loginReq)
5351
if err != nil {
5452
return fmt.Errorf("failed to call login API: %w", err)
5553
}
56-
defer resp.Body.Close()
5754

58-
if resp.StatusCode != http.StatusOK {
59-
respBody, _ := io.ReadAll(resp.Body)
60-
return fmt.Errorf("login API returned status %d: %s", resp.StatusCode, string(respBody))
55+
if resp.StatusCode() != http.StatusOK {
56+
return fmt.Errorf("login API returned status %d: %s", resp.StatusCode(), string(resp.Body))
6157
}
6258

63-
var loginResp nekooapi.SessionLoginResponse
64-
if err := json.NewDecoder(resp.Body).Decode(&loginResp); err != nil {
65-
return fmt.Errorf("failed to parse login response: %w", err)
66-
}
67-
68-
if loginResp.Token == nil || *loginResp.Token == "" {
59+
if resp.JSON200 == nil || resp.JSON200.Token == nil || *resp.JSON200.Token == "" {
6960
return fmt.Errorf("login response did not contain a token")
7061
}
7162

72-
c.token = *loginResp.Token
63+
c.token = *resp.JSON200.Token
7364
return nil
7465
}
7566

@@ -96,44 +87,34 @@ func (c *AuthClient) SessionsGet(ctx context.Context) ([]nekooapi.SessionData, e
9687
}
9788

9889
// Make the request
99-
resp, err := c.client.SessionsGet(ctx, addAuth)
90+
resp, err := c.client.SessionsGetWithResponse(ctx, addAuth)
10091
if err != nil {
10192
return nil, fmt.Errorf("failed to query sessions: %w", err)
10293
}
103-
defer resp.Body.Close()
10494

10595
// Handle 401 by clearing token and retrying once
106-
if resp.StatusCode == http.StatusUnauthorized {
107-
resp.Body.Close() // Close the first response body before retrying
96+
if resp.StatusCode() == http.StatusUnauthorized {
10897
c.clearToken()
10998
if err := c.ensureToken(ctx); err != nil {
11099
return nil, err
111100
}
112101

113102
// Retry with fresh token
114-
addAuthRetry := func(ctx context.Context, req *http.Request) error {
115-
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
116-
return nil
117-
}
118-
119-
resp, err = c.client.SessionsGet(ctx, addAuthRetry)
103+
resp, err = c.client.SessionsGetWithResponse(ctx, addAuth)
120104
if err != nil {
121105
return nil, fmt.Errorf("failed to retry sessions query: %w", err)
122106
}
123-
defer resp.Body.Close()
124107
}
125108

126-
if resp.StatusCode != http.StatusOK {
127-
respBody, _ := io.ReadAll(resp.Body)
128-
return nil, fmt.Errorf("sessions API returned status %d: %s", resp.StatusCode, string(respBody))
109+
if resp.StatusCode() != http.StatusOK {
110+
return nil, fmt.Errorf("sessions API returned status %d: %s", resp.StatusCode(), string(resp.Body))
129111
}
130112

131-
var sessions []nekooapi.SessionData
132-
if err := json.NewDecoder(resp.Body).Decode(&sessions); err != nil {
133-
return nil, fmt.Errorf("failed to parse sessions response: %w", err)
113+
if resp.JSON200 == nil {
114+
return nil, fmt.Errorf("sessions response did not contain expected data")
134115
}
135116

136-
return sessions, nil
117+
return *resp.JSON200, nil
137118
}
138119

139120
// ScreenConfigurationChange changes the screen resolution via Neko API.
@@ -153,36 +134,27 @@ func (c *AuthClient) ScreenConfigurationChange(ctx context.Context, config nekoo
153134
}
154135

155136
// Make the request
156-
resp, err := c.client.ScreenConfigurationChange(ctx, config, addAuth)
137+
resp, err := c.client.ScreenConfigurationChangeWithResponse(ctx, config, addAuth)
157138
if err != nil {
158139
return fmt.Errorf("failed to change screen configuration: %w", err)
159140
}
160-
defer resp.Body.Close()
161141

162142
// Handle 401 by clearing token and retrying once
163-
if resp.StatusCode == http.StatusUnauthorized {
164-
resp.Body.Close() // Close the first response body before retrying
143+
if resp.StatusCode() == http.StatusUnauthorized {
165144
c.clearToken()
166145
if err := c.ensureToken(ctx); err != nil {
167146
return err
168147
}
169148

170149
// Retry with fresh token
171-
addAuthRetry := func(ctx context.Context, req *http.Request) error {
172-
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))
173-
return nil
174-
}
175-
176-
resp, err = c.client.ScreenConfigurationChange(ctx, config, addAuthRetry)
150+
resp, err = c.client.ScreenConfigurationChangeWithResponse(ctx, config, addAuth)
177151
if err != nil {
178152
return fmt.Errorf("failed to retry screen configuration change: %w", err)
179153
}
180-
defer resp.Body.Close()
181154
}
182155

183-
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
184-
respBody, _ := io.ReadAll(resp.Body)
185-
return fmt.Errorf("screen configuration API returned status %d: %s", resp.StatusCode, string(respBody))
156+
if resp.StatusCode() != http.StatusOK && resp.StatusCode() != http.StatusNoContent {
157+
return fmt.Errorf("screen configuration API returned status %d: %s", resp.StatusCode(), string(resp.Body))
186158
}
187159

188160
return nil

0 commit comments

Comments
 (0)