package api import ( "bytes" "encoding/json" "fmt" "io" "net/http" "code.gitea.io/sdk/gitea" "forgejo.zerova.net/sid/fgj-sid/internal/config" ) type Client struct { *gitea.Client hostname string token string } func NewClient(hostname, token string) (*Client, error) { if hostname == "" { hostname = "codeberg.org" } client, err := gitea.NewClient("https://"+hostname, gitea.SetToken(token)) if err != nil { return nil, err } return &Client{ Client: client, hostname: hostname, token: token, }, nil } func NewClientFromConfig(cfg *config.Config, hostname string, detectedHost string) (*Client, error) { host, err := cfg.GetHost(hostname, detectedHost) if err != nil { return nil, err } return NewClient(host.Hostname, host.Token) } func (c *Client) Hostname() string { return c.hostname } // GetJSON performs a GET request to the specified path and decodes the JSON response func (c *Client) GetJSON(path string, result any) error { baseURL := "https://" + c.hostname url := baseURL + path req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return fmt.Errorf("failed to create request: %w", err) } // Set authentication header if c.token != "" { req.Header.Set("Authorization", "token "+c.token) } req.Header.Set("Accept", "application/json") httpClient := &http.Client{} resp, err := httpClient.Do(req) if err != nil { return fmt.Errorf("failed to perform request: %w", err) } defer func() { if closeErr := resp.Body.Close(); closeErr != nil && err == nil { err = fmt.Errorf("failed to close response body: %w", closeErr) } }() if resp.StatusCode != http.StatusOK { body, _ := io.ReadAll(resp.Body) return &APIError{ StatusCode: resp.StatusCode, Body: string(body), Message: fmt.Sprintf("API request failed with status %d: %s", resp.StatusCode, string(body)), } } if err := json.NewDecoder(resp.Body).Decode(result); err != nil { return fmt.Errorf("failed to decode response: %w", err) } return nil } // PostJSON performs a POST request to the specified path with JSON body func (c *Client) PostJSON(path string, body any, result any) error { _, err := c.DoJSON(http.MethodPost, path, body, result) return err } // DoJSON performs an HTTP request with a JSON body and decodes the JSON response. // Returns the HTTP status code and any error encountered. func (c *Client) DoJSON(method string, path string, body any, result any) (int, error) { baseURL := "https://" + c.hostname url := baseURL + path var bodyReader io.Reader if body != nil { bodyBytes, err := json.Marshal(body) if err != nil { return 0, fmt.Errorf("failed to marshal request body: %w", err) } bodyReader = bytes.NewReader(bodyBytes) } req, err := http.NewRequest(method, url, bodyReader) if err != nil { return 0, fmt.Errorf("failed to create request: %w", err) } // Set authentication header if c.token != "" { req.Header.Set("Authorization", "token "+c.token) } req.Header.Set("Accept", "application/json") if body != nil { req.Header.Set("Content-Type", "application/json") } httpClient := &http.Client{} resp, err := httpClient.Do(req) if err != nil { return 0, fmt.Errorf("failed to perform request: %w", err) } defer func() { if closeErr := resp.Body.Close(); closeErr != nil && err == nil { err = fmt.Errorf("failed to close response body: %w", closeErr) } }() if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusNoContent { bodyBytes, _ := io.ReadAll(resp.Body) return resp.StatusCode, &APIError{ StatusCode: resp.StatusCode, Body: string(bodyBytes), Message: fmt.Sprintf("API request failed with status %d: %s", resp.StatusCode, string(bodyBytes)), } } if result != nil && resp.StatusCode != http.StatusNoContent { if err := json.NewDecoder(resp.Body).Decode(result); err != nil { return resp.StatusCode, fmt.Errorf("failed to decode response: %w", err) } } return resp.StatusCode, nil } // GetRawLog performs a GET request and returns the raw response body as string func (c *Client) GetRawLog(url string) (string, error) { req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return "", fmt.Errorf("failed to create request: %w", err) } // Set authentication header if c.token != "" { req.Header.Set("Authorization", "token "+c.token) } httpClient := &http.Client{} resp, err := httpClient.Do(req) if err != nil { return "", fmt.Errorf("failed to perform request: %w", err) } defer func() { if closeErr := resp.Body.Close(); closeErr != nil && err == nil { err = fmt.Errorf("failed to close response body: %w", closeErr) } }() if resp.StatusCode != http.StatusOK { body, _ := io.ReadAll(resp.Body) return "", &APIError{ StatusCode: resp.StatusCode, Body: string(body), Message: fmt.Sprintf("API request failed with status %d: %s", resp.StatusCode, string(body)), } } bodyBytes, err := io.ReadAll(resp.Body) if err != nil { return "", fmt.Errorf("failed to read response body: %w", err) } return string(bodyBytes), nil }