Skip to content

Commit 6437a3f

Browse files
committed
migrate audit.go and authorization.go
1 parent c89fb87 commit 6437a3f

File tree

2 files changed

+29
-46
lines changed

2 files changed

+29
-46
lines changed

codersdk/audit.go

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -179,45 +179,33 @@ type CreateTestAuditLogRequest struct {
179179

180180
// AuditLogs retrieves audit logs from the given page.
181181
func (c *Client) AuditLogs(ctx context.Context, req AuditLogsRequest) (AuditLogResponse, error) {
182-
res, err := c.Request(ctx, http.MethodGet, "/api/v2/audit", nil, req.Pagination.asRequestOption(), func(r *http.Request) {
183-
q := r.URL.Query()
184-
var params []string
185-
if req.SearchQuery != "" {
186-
params = append(params, req.SearchQuery)
187-
}
188-
q.Set("q", strings.Join(params, " "))
189-
r.URL.RawQuery = q.Encode()
182+
return makeSDKRequest[AuditLogResponse](ctx, c, sdkRequestArgs{
183+
Method: http.MethodGet,
184+
URL: "/api/v2/audit",
185+
ReqOpts: []RequestOption{
186+
req.Pagination.asRequestOption(),
187+
func(r *http.Request) {
188+
q := r.URL.Query()
189+
var params []string
190+
if req.SearchQuery != "" {
191+
params = append(params, req.SearchQuery)
192+
}
193+
q.Set("q", strings.Join(params, " "))
194+
r.URL.RawQuery = q.Encode()
195+
},
196+
},
197+
ExpectCode: http.StatusOK,
190198
})
191-
if err != nil {
192-
return AuditLogResponse{}, err
193-
}
194-
defer res.Body.Close()
195-
196-
if res.StatusCode != http.StatusOK {
197-
return AuditLogResponse{}, ReadBodyAsError(res)
198-
}
199-
200-
var logRes AuditLogResponse
201-
err = json.NewDecoder(res.Body).Decode(&logRes)
202-
if err != nil {
203-
return AuditLogResponse{}, err
204-
}
205-
206-
return logRes, nil
207199
}
208200

209201
// CreateTestAuditLog creates a fake audit log. Only owners of the organization
210202
// can perform this action. It's used for testing purposes.
211203
func (c *Client) CreateTestAuditLog(ctx context.Context, req CreateTestAuditLogRequest) error {
212-
res, err := c.Request(ctx, http.MethodPost, "/api/v2/audit/testgenerate", req)
213-
if err != nil {
214-
return err
215-
}
216-
defer res.Body.Close()
217-
218-
if res.StatusCode != http.StatusNoContent {
219-
return err
220-
}
221-
222-
return nil
204+
_, err := makeSDKRequest[noResponse](ctx, c, sdkRequestArgs{
205+
Method: http.MethodPost,
206+
URL: "/api/v2/audit/testgenerate",
207+
Body: req,
208+
ExpectCode: http.StatusNoContent,
209+
})
210+
return err
223211
}

codersdk/authorization.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package codersdk
22

33
import (
44
"context"
5-
"encoding/json"
65
"net/http"
76
)
87

@@ -62,14 +61,10 @@ type AuthorizationObject struct {
6261
// AuthCheck allows the authenticated user to check if they have the given permissions
6362
// to a set of resources.
6463
func (c *Client) AuthCheck(ctx context.Context, req AuthorizationRequest) (AuthorizationResponse, error) {
65-
res, err := c.Request(ctx, http.MethodPost, "/api/v2/authcheck", req)
66-
if err != nil {
67-
return nil, err
68-
}
69-
defer res.Body.Close()
70-
if res.StatusCode != http.StatusOK {
71-
return AuthorizationResponse{}, ReadBodyAsError(res)
72-
}
73-
var resp AuthorizationResponse
74-
return resp, json.NewDecoder(res.Body).Decode(&resp)
64+
return makeSDKRequest[AuthorizationResponse](ctx, c, sdkRequestArgs{
65+
Method: http.MethodPost,
66+
URL: "/api/v2/authcheck",
67+
Body: req,
68+
ExpectCode: http.StatusOK,
69+
})
7570
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy