mirror of
https://github.com/coder/coder.git
synced 2026-09-22 05:05:20 +08:00
This PR migrates 224 typed JSON response sites across 46 files to `codersdk.ReadBodyAsJSON`, so invalid 2xx bodies return structured errors while preserving URL credential redaction. It intentionally excludes agent-direct HTTP, Azure IMDS, `UseNumber`, and chat paths; stacked on coder/coder#27804, with chat and lint follow-ups in coder/coder#27858 and coder/coder#27859. Refs coder/coder#27044. Reviewed and updated by Coder Agents on behalf of @dylanhuff-at-coder.
82 lines
2.6 KiB
Go
82 lines
2.6 KiB
Go
package codersdk
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"golang.org/x/xerrors"
|
|
)
|
|
|
|
// AIGatewayKey is a shared secret used by a standalone AI Gateway
|
|
// to authenticate into coderd.
|
|
type AIGatewayKey struct {
|
|
ID uuid.UUID `json:"id" table:"id" format:"uuid"`
|
|
Name string `json:"name" table:"name,default_sort"`
|
|
KeyPrefix string `json:"key_prefix" table:"key prefix"`
|
|
CreatedAt time.Time `json:"created_at" table:"created at" format:"date-time"`
|
|
LastHeartbeatAt *time.Time `json:"last_heartbeat_at,omitempty" table:"last heartbeat at" format:"date-time"`
|
|
}
|
|
|
|
// CreateAIGatewayKeyRequest requests a new AI Gateway key.
|
|
type CreateAIGatewayKeyRequest struct {
|
|
Name string `json:"name" validate:"required"`
|
|
}
|
|
|
|
// CreateAIGatewayKeyResponse returns all key information.
|
|
// Key value is only returned here and cannot be recovered afterwards.
|
|
type CreateAIGatewayKeyResponse struct {
|
|
ID uuid.UUID `json:"id" format:"uuid"`
|
|
Name string `json:"name"`
|
|
Key string `json:"key"`
|
|
KeyPrefix string `json:"key_prefix"`
|
|
CreatedAt time.Time `json:"created_at" format:"date-time"`
|
|
}
|
|
|
|
// CreateAIGatewayKey creates a new AI Gateway key.
|
|
func (c *Client) CreateAIGatewayKey(ctx context.Context, req CreateAIGatewayKeyRequest) (CreateAIGatewayKeyResponse, error) {
|
|
res, err := c.Request(ctx, http.MethodPost, "/api/v2/ai-gateway/keys", req)
|
|
if err != nil {
|
|
return CreateAIGatewayKeyResponse{}, xerrors.Errorf("make request: %w", err)
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusCreated {
|
|
return CreateAIGatewayKeyResponse{}, ReadBodyAsError(res)
|
|
}
|
|
var resp CreateAIGatewayKeyResponse
|
|
return resp, ReadBodyAsJSON(res, &resp)
|
|
}
|
|
|
|
// ListAIGatewayKeys lists all AI Gateway keys.
|
|
func (c *Client) ListAIGatewayKeys(ctx context.Context) ([]AIGatewayKey, error) {
|
|
res, err := c.Request(ctx, http.MethodGet, "/api/v2/ai-gateway/keys", nil)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("make request: %w", err)
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
return nil, ReadBodyAsError(res)
|
|
}
|
|
var resp []AIGatewayKey
|
|
return resp, ReadBodyAsJSON(res, &resp)
|
|
}
|
|
|
|
// DeleteAIGatewayKey deletes an AI Gateway key by ID.
|
|
func (c *Client) DeleteAIGatewayKey(ctx context.Context, id uuid.UUID) error {
|
|
res, err := c.Request(ctx, http.MethodDelete,
|
|
fmt.Sprintf("/api/v2/ai-gateway/keys/%s", id.String()), nil)
|
|
if err != nil {
|
|
return xerrors.Errorf("make request: %w", err)
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusNoContent {
|
|
return ReadBodyAsError(res)
|
|
}
|
|
return nil
|
|
}
|