-
Notifications
You must be signed in to change notification settings - Fork 1
/
longbridge_test.go
114 lines (105 loc) · 4.6 KB
/
longbridge_test.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
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
// Copyright 2022 Deep Learning Limited. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package longbridge
import (
"log"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCreateSignedRequest(t *testing.T) {
assert := assert.New(t)
c, err := newClient(&Config{
BaseURL: "https://openapi.longbridgeapp.com",
AccessToken: "fake access token",
AppKey: "fake App key",
AppSecret: "fake secret",
})
if err != nil {
t.Fatalf("Error creating longbridge client: %v", err)
}
hkt, _ := time.LoadLocation("Asia/Hong_Kong")
req, err := c.createSignedRequest(httpGET, "/v1/trade/order/history",
map[string][]string{"order id": {"0001"}, "type": {"limited"}}, nil, time.Date(2022, time.January, 2, 0, 0, 1, 2, hkt))
assert.Nil(err, "Cheating signed GET request")
assert.Equal("https://openapi.longbridgeapp.com/v1/trade/order/history?order+id=0001&type=limited", req.URL.String(), "Checking url")
assert.Equal("HMAC-SHA256 SignedHeaders=authorization;x-api-key;x-timestamp, Signature=7971ae0992cc6509fbb9f9dff14686c07ed316e3366479ed84ec00e47d1c8515",
req.Header.Get("X-Api-Signature"), "Checking GET signature")
body := []byte(`{"symbol":"AAPL.US", "order_type":"LO", "trigger_price":"123.0","side":"Buy","submitted_quantity":"20"}`)
req, err = c.createSignedRequest(httpPOST, "/v1/trade/order", nil, body,
time.Date(2022, time.January, 2, 0, 0, 0, 0, hkt))
assert.Nil(err, "Cheating signed POST request")
assert.NotNil(req.Body, "Checking POST body")
assert.Equal("https://openapi.longbridgeapp.com/v1/trade/order", req.URL.String(), "Checking url")
assert.Equal("HMAC-SHA256 SignedHeaders=authorization;x-api-key;x-timestamp, Signature=6fb79bfc9384428ae8136882917001032c03094b0048a858616318004c3e6b4d",
req.Header.Get("X-Api-Signature"), "Checking POST signature")
}
func ExampleTradeClient_Enable() {
// Before running the example, set the access key, app key and app secret in the env. i.e.,
// export LB_ACCESS_TOKEN="<your access token>"
// export LB_APP_KEY="<your app key>"
// export LB_APP_SECRET="<your app secret>"
// Then run with command to see the detailed log.
// go test -v -run ExampleTradeClient_Enable -args -logtostderr -v=4
c, err := NewTradeClient(&Config{
AccessToken: os.Getenv("LB_ACCESS_TOKEN"),
AppKey: os.Getenv("LB_APP_KEY"),
AppSecret: os.Getenv("LB_APP_SECRET"),
})
if err != nil {
log.Fatalf("Error creating longbridge client: %v", err)
}
c.OnOrderChange = func(order *Order) {
log.Printf("Got order: %#v\n", order)
}
c.Enable(true)
time.Sleep(11 * time.Second)
log.Println("Start to close subscription")
c.Enable(false)
time.Sleep(10 * time.Second)
log.Println("Start to re-enable subscription")
c.Enable(true)
time.Sleep(15 * time.Second)
log.Println("Closing")
c.Close()
// Output:
}
func Exampleclient_RefreshAccessToken() {
// Before running the example, set the access key, app key and app secret in the env. i.e.,
// export LB_ACCESS_TOKEN="<your access token>"
// export LB_APP_KEY="<your app key>"
// export LB_APP_SECRET="<your app secret>"
// Then run with command to see the detailed log.
// go test -v -run Exampleclient_RefreshAccessToken -args -logtostderr -v=4
c, err := newClient(&Config{
AccessToken: os.Getenv("LB_ACCESS_TOKEN"),
AppKey: os.Getenv("LB_APP_KEY"),
AppSecret: os.Getenv("LB_APP_SECRET"),
})
if err != nil {
log.Fatalf("Error creating longbridge client: %v", err)
}
defer c.Close()
token, err := c.RefreshAccessToken(time.Now().AddDate(0, 1, 0))
if err != nil {
log.Fatalf("Error refreshing access token: %v", err)
}
log.Printf("Access token: %#v", token)
// Output:
}