|
| 1 | +package sim |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +const defaultBaseURL = "https://api.sim.dune.com" |
| 14 | + |
| 15 | +// SimClient is a lightweight HTTP client for the Sim API. |
| 16 | +type SimClient struct { |
| 17 | + baseURL string |
| 18 | + apiKey string |
| 19 | + httpClient *http.Client |
| 20 | +} |
| 21 | + |
| 22 | +// NewSimClient creates a new Sim API client with the given API key. |
| 23 | +func NewSimClient(apiKey string) *SimClient { |
| 24 | + return &SimClient{ |
| 25 | + baseURL: defaultBaseURL, |
| 26 | + apiKey: apiKey, |
| 27 | + httpClient: &http.Client{ |
| 28 | + Timeout: 30 * time.Second, |
| 29 | + }, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +// Get performs a GET request to the Sim API and returns the raw JSON response body. |
| 34 | +// The path should include the leading slash (e.g. "/v1/evm/supported-chains"). |
| 35 | +// Query parameters are appended from params. |
| 36 | +func (c *SimClient) Get(ctx context.Context, path string, params url.Values) ([]byte, error) { |
| 37 | + u, err := url.Parse(c.baseURL + path) |
| 38 | + if err != nil { |
| 39 | + return nil, fmt.Errorf("invalid URL: %w", err) |
| 40 | + } |
| 41 | + if params != nil { |
| 42 | + u.RawQuery = params.Encode() |
| 43 | + } |
| 44 | + |
| 45 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil) |
| 46 | + if err != nil { |
| 47 | + return nil, fmt.Errorf("creating request: %w", err) |
| 48 | + } |
| 49 | + req.Header.Set("X-Sim-Api-Key", c.apiKey) |
| 50 | + req.Header.Set("Accept", "application/json") |
| 51 | + |
| 52 | + resp, err := c.httpClient.Do(req) |
| 53 | + if err != nil { |
| 54 | + return nil, fmt.Errorf("request failed: %w", err) |
| 55 | + } |
| 56 | + defer resp.Body.Close() |
| 57 | + |
| 58 | + body, err := io.ReadAll(resp.Body) |
| 59 | + if err != nil { |
| 60 | + return nil, fmt.Errorf("reading response: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + if resp.StatusCode >= 400 { |
| 64 | + return nil, httpError(resp.StatusCode, body) |
| 65 | + } |
| 66 | + |
| 67 | + return body, nil |
| 68 | +} |
| 69 | + |
| 70 | +// httpError returns a user-friendly error for HTTP error status codes. |
| 71 | +func httpError(status int, body []byte) error { |
| 72 | + // Try to extract a message from the JSON error response. |
| 73 | + var errResp struct { |
| 74 | + Error string `json:"error"` |
| 75 | + Message string `json:"message"` |
| 76 | + } |
| 77 | + msg := "" |
| 78 | + if json.Unmarshal(body, &errResp) == nil { |
| 79 | + if errResp.Error != "" { |
| 80 | + msg = errResp.Error |
| 81 | + } else if errResp.Message != "" { |
| 82 | + msg = errResp.Message |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + switch status { |
| 87 | + case http.StatusBadRequest: |
| 88 | + if msg != "" { |
| 89 | + return fmt.Errorf("bad request: %s", msg) |
| 90 | + } |
| 91 | + return fmt.Errorf("bad request") |
| 92 | + case http.StatusUnauthorized: |
| 93 | + return fmt.Errorf("authentication failed: check your Sim API key") |
| 94 | + case http.StatusNotFound: |
| 95 | + if msg != "" { |
| 96 | + return fmt.Errorf("not found: %s", msg) |
| 97 | + } |
| 98 | + return fmt.Errorf("not found") |
| 99 | + case http.StatusTooManyRequests: |
| 100 | + return fmt.Errorf("rate limit exceeded: try again later") |
| 101 | + default: |
| 102 | + if status >= 500 { |
| 103 | + return fmt.Errorf("Sim API server error (HTTP %d): try again later", status) |
| 104 | + } |
| 105 | + if msg != "" { |
| 106 | + return fmt.Errorf("Sim API error (HTTP %d): %s", status, msg) |
| 107 | + } |
| 108 | + return fmt.Errorf("Sim API error (HTTP %d)", status) |
| 109 | + } |
| 110 | +} |
0 commit comments