forked from sonirico/go-hyperliquid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
110 lines (92 loc) · 2.35 KB
/
Copy pathclient.go
File metadata and controls
110 lines (92 loc) · 2.35 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
// Package hyperliquid provides a Go client library for the Hyperliquid exchange API.
// It includes support for both REST API and WebSocket connections, allowing users to
// access market data, manage orders, and handle user account operations.
package hyperliquid
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/sonirico/vago/lol"
)
const (
MainnetAPIURL = "https://api.hyperliquid.xyz"
TestnetAPIURL = "https://api.hyperliquid-testnet.xyz"
LocalAPIURL = "http://localhost:3001"
// httpErrorStatusCode is the minimum status code considered an error
httpErrorStatusCode = 400
)
type client struct {
logger lol.Logger
debug bool
baseURL string
httpClient *http.Client
}
func newClient(baseURL string, opts ...ClientOpt) *client {
if baseURL == "" {
baseURL = MainnetAPIURL
}
cli := &client{
baseURL: baseURL,
httpClient: new(http.Client),
}
for _, opt := range opts {
opt.Apply(cli)
}
return cli
}
func (c *client) post(ctx context.Context, path string, payload any) ([]byte, error) {
jsonData, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("failed to marshal payload: %w", err)
}
url := c.baseURL + path
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
url,
bytes.NewBuffer(jsonData),
)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
if c.debug {
c.logger.WithFields(lol.Fields{
"method": "POST",
"url": url,
"body": string(jsonData),
}).Debug("HTTP request")
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
body := make([]byte, 0)
if resp.Body != nil {
body, err = io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
}
if c.debug {
c.logger.WithFields(lol.Fields{
"status": resp.Status,
"body": string(body),
}).Debug("HTTP response")
}
if resp.StatusCode >= httpErrorStatusCode {
if !json.Valid(body) {
return nil, fmt.Errorf("status %d: %s", resp.StatusCode, string(body))
}
var apiErr APIError
if err := json.Unmarshal(body, &apiErr); err != nil {
return nil, fmt.Errorf("status %d: %s", resp.StatusCode, string(body))
}
return nil, apiErr
}
return body, nil
}