-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
49 lines (43 loc) · 1.23 KB
/
auth.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
package kalshi
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net/http"
)
// Kalshi API authentication token.
type authToken struct {
Token string `json:"token"`
UserId string `json:"user_id"`
AccessLevel string `json:"access_level"`
}
// Debugging function to print out a Kalshi API authentication token in its
// string representation.
func (a authToken) Text() string {
p := fmt.Sprintf(
"Token: %s\nUserId : %s\nAccessLevel: %s\n",
a.Token, a.UserId, a.AccessLevel)
return p
}
// Uses the specified Kalshi credentials to return an authToken struct.
func getAuthToken(ctx context.Context, kalshiUsername string, kalshiPassword string) (*authToken, error) {
postBody, _ := json.Marshal(map[string]string{
"email": kalshiUsername,
"password": kalshiPassword,
})
postBodyBuf := bytes.NewBuffer(postBody)
resp, err := http.Post(fmt.Sprintf("%s/log_in", BaseURLV1), "application/json", postBodyBuf)
if err != nil {
log.Fatalf("Error: '%v'\n", err)
return nil, err
}
defer resp.Body.Close()
var authToken authToken
if err := json.NewDecoder(resp.Body).Decode(&authToken); err != nil {
log.Fatalf("ERROR: decoding auth token response: '%v'\n", err)
return nil, err
}
return &authToken, nil
}