1package catwalk
2
3import (
4 "encoding/json"
5 "fmt"
6 "net/http"
7 "os"
8)
9
10const defaultURL = "http://localhost:8080"
11
12// Client represents a client for the catwalk service.
13type Client struct {
14 baseURL string
15 httpClient *http.Client
16}
17
18// New creates a new client instance
19// Uses CATWALK_URL environment variable or falls back to localhost:8080.
20func New() *Client {
21 baseURL := os.Getenv("CATWALK_URL")
22 if baseURL == "" {
23 baseURL = defaultURL
24 }
25
26 return &Client{
27 baseURL: baseURL,
28 httpClient: &http.Client{},
29 }
30}
31
32// NewWithURL creates a new client with a specific URL.
33func NewWithURL(url string) *Client {
34 return &Client{
35 baseURL: url,
36 httpClient: &http.Client{},
37 }
38}
39
40// GetProviders retrieves all available providers from the service.
41func (c *Client) GetProviders() ([]Provider, error) {
42 url := fmt.Sprintf("%s/providers", c.baseURL)
43
44 resp, err := c.httpClient.Get(url) //nolint:noctx
45 if err != nil {
46 return nil, fmt.Errorf("failed to make request: %w", err)
47 }
48 defer resp.Body.Close() //nolint:errcheck
49
50 if resp.StatusCode != http.StatusOK {
51 return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
52 }
53
54 var providers []Provider
55 if err := json.NewDecoder(resp.Body).Decode(&providers); err != nil {
56 return nil, fmt.Errorf("failed to decode response: %w", err)
57 }
58
59 return providers, nil
60}