-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (49 loc) · 1.71 KB
/
main.go
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
package kalshi
import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/fsctl/go-kalshi/swagger"
)
// This constant represents the Kalshi API endpoint URL to use.
const (
BaseURLV1 = "https://trading-api.kalshi.com/v1"
)
// KalshiClient is a higher level API client than the Swagger generated client.
// It exposes methods to do complex actions that involve multiple underlying
// API calls.
type KalshiClient struct {
authToken authToken
swaggerApiClient *swagger.APIClient
}
// NewKalshiClient() constructs a new KalshiClient struct that is initialized
// and ready for use, using the specified Kalshi credentials.
func NewKalshiClient(ctx context.Context, kalshiUsername string, kalshiPassword string) (*KalshiClient, error) {
authToken, err := getAuthToken(ctx, kalshiUsername, kalshiPassword)
if err != nil {
log.Fatalf("ERROR: getAuthToken failed: '%v'\n", err)
return nil, err
}
cfg := swagger.NewConfiguration()
cfg.AddDefaultHeader("Authorization", fmt.Sprintf("%s %s", authToken.UserId, authToken.Token))
swaggerApiClient := swagger.NewAPIClient(cfg)
return &KalshiClient{
authToken: *authToken,
swaggerApiClient: swaggerApiClient,
}, nil
}
func (kc *KalshiClient) doAuthenticatedRequest(method string, url string, reqBody *strings.Reader) (*http.Response, []byte) {
req, _ := http.NewRequest(method, url, reqBody)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", fmt.Sprintf("%s %s", kc.authToken.UserId, kc.authToken.Token))
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalf("Error: http.DefaultClient.Do failed: '%v'\n", err)
}
data, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
return res, data
}