-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
211 lines (186 loc) · 5.5 KB
/
Copy pathclient.go
File metadata and controls
211 lines (186 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Package claw402 provides a typed SDK for claw402.ai — pay-per-call data APIs via x402.
//
// All API calls cost micro-amounts of USDC on Base mainnet (Coinbase L2).
// No API key, no account, no subscription needed — just a wallet with USDC.
//
// Usage:
//
// client, err := claw402.New("0xYourPrivateKey")
// if err != nil {
// log.Fatal(err)
// }
//
// // Crypto market data
// data, err := client.Coinank.Fund.Realtime(ctx, &claw402.CoinankFundRealtimeParams{
// ProductType: "SWAP",
// })
//
// // US stocks
// quote, err := client.Alphavantage.StocksUs.Quote(ctx, &claw402.AlphavantageStocksUsQuoteParams{
// Symbol: "AAPL",
// })
//
// // AI models (POST endpoints)
// resp, err := client.Openai.Openai.Chat(ctx, map[string]interface{}{
// "model": "gpt-4o",
// "messages": []map[string]string{{"role": "user", "content": "Hello!"}},
// })
//
// // Forex & metals
// price, err := client.Twelvedata.Price.Price(ctx, &claw402.TwelvedataPricePriceParams{
// Symbol: "EUR/USD",
// })
package claw402
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
x402 "github.com/coinbase/x402/go"
x402http "github.com/coinbase/x402/go/http"
evmclient "github.com/coinbase/x402/go/mechanisms/evm/exact/client"
evmsigner "github.com/coinbase/x402/go/signers/evm"
)
// Client is the top-level claw402 SDK client.
type Client struct {
http *http.Client
baseURL string
// Crypto data providers
Coinank *CoinankResource
Nofxos *NofxosResource
Coinmarketcap *CoinmarketcapResource
// US stock market data
Alphavantage *AlphavantageResource
Polygon *PolygonResource
Alpaca *AlpacaResource
// Chinese A-share market data
Tushare *TushareResource
// Forex, metals, indices
Twelvedata *TwelvedataResource
// AI model providers
Openai *OpenaiResource
Anthropic *AnthropicResource
Deepseek *DeepseekResource
Qwen *QwenResource
Gemini *GeminiResource
Grok *GrokResource
Kimi *KimiResource
// Web3 intelligence
Rootdata *RootdataResource
}
// New creates a new claw402 client with the given private key.
func New(privateKey string, opts ...Option) (*Client, error) {
cfg := &config{baseURL: "https://claw402.ai"}
for _, o := range opts {
o(cfg)
}
signer, err := evmsigner.NewClientSignerFromPrivateKey(privateKey)
if err != nil {
return nil, fmt.Errorf("claw402: invalid private key: %w", err)
}
x402Client := x402.Newx402Client().
Register(x402.Network("eip155:8453"), evmclient.NewExactEvmScheme(signer))
httpClient := x402http.WrapHTTPClientWithPayment(
&http.Client{Timeout: 30 * time.Second},
x402http.Newx402HTTPClient(x402Client),
)
c := &Client{
http: httpClient,
baseURL: cfg.baseURL,
}
// Crypto data
c.Coinank = newCoinankResource(c)
c.Nofxos = newNofxosResource(c)
c.Coinmarketcap = newCoinmarketcapResource(c)
// US stocks
c.Alphavantage = newAlphavantageResource(c)
c.Polygon = newPolygonResource(c)
c.Alpaca = newAlpacaResource(c)
// Chinese A-shares
c.Tushare = newTushareResource(c)
// Forex, metals, indices
c.Twelvedata = newTwelvedataResource(c)
// AI models
c.Openai = newOpenaiResource(c)
c.Anthropic = newAnthropicResource(c)
c.Deepseek = newDeepseekResource(c)
c.Qwen = newQwenResource(c)
c.Gemini = newGeminiResource(c)
c.Grok = newGrokResource(c)
c.Kimi = newKimiResource(c)
// Web3 intelligence
c.Rootdata = newRootdataResource(c)
return c, nil
}
func (c *Client) post(ctx context.Context, path string, body map[string]interface{}) (json.RawMessage, error) {
urlStr := c.baseURL + path
bodyBytes, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("claw402: marshal body: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, urlStr, bytes.NewReader(bodyBytes))
if err != nil {
return nil, fmt.Errorf("claw402: new request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.http.Do(req)
if err != nil {
return nil, fmt.Errorf("claw402: request: %w", err)
}
defer resp.Body.Close()
const maxBodySize = 10 * 1024 * 1024 // 10 MB
respBody, err := io.ReadAll(io.LimitReader(resp.Body, maxBodySize))
if err != nil {
return nil, fmt.Errorf("claw402: read body: %w", err)
}
if resp.StatusCode >= 400 {
return nil, &Error{Status: resp.StatusCode, Body: string(respBody)}
}
return json.RawMessage(respBody), nil
}
func (c *Client) get(ctx context.Context, path string, params map[string]string) (json.RawMessage, error) {
u, err := url.Parse(c.baseURL + path)
if err != nil {
return nil, fmt.Errorf("claw402: bad url: %w", err)
}
if params != nil {
q := u.Query()
for k, v := range params {
if v != "" {
q.Set(k, v)
}
}
u.RawQuery = q.Encode()
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, fmt.Errorf("claw402: new request: %w", err)
}
resp, err := c.http.Do(req)
if err != nil {
return nil, fmt.Errorf("claw402: request: %w", err)
}
defer resp.Body.Close()
const maxBodySize = 10 * 1024 * 1024 // 10 MB
body, err := io.ReadAll(io.LimitReader(resp.Body, maxBodySize))
if err != nil {
return nil, fmt.Errorf("claw402: read body: %w", err)
}
if resp.StatusCode >= 400 {
return nil, &Error{Status: resp.StatusCode, Body: string(body)}
}
return json.RawMessage(body), nil
}
// Option configures the claw402 client.
type Option func(*config)
type config struct {
baseURL string
}
// WithBaseURL sets a custom base URL for the claw402 gateway.
func WithBaseURL(u string) Option {
return func(c *config) { c.baseURL = u }
}