package main import ( "encoding/json" "fmt" "net/http" "time" ) type API struct { Base string // base endpoint (no trailing slash) Bearer string // API token Models []Model } type Model string func InitAPI(Base, Bearer string) API { a := API{ Base: Base, Bearer: Bearer, } a.GetModels() return a } func (a *API) GetModels() ([]Model, error) { req, err := http.NewRequest("GET", a.Base+"/models", nil) if err != nil { return nil, fmt.Errorf("create request: %w", err) } if a.Bearer != "" { req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", a.Bearer)) } client := &http.Client{Timeout: 10 * time.Second} resp, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("unexpected status: %s", resp.Status) } var body struct { Data []struct { ID string `json:"id"` } `json:"data"` } if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { return nil, fmt.Errorf("decode models: %w", err) } a.Models = make([]Model, 0, len(body.Data)) for _, m := range body.Data { a.Models = append(a.Models, Model(m.ID)) } return a.Models, nil }