Skip to content

Commit 3de3dca

Browse files
committed
Move config to separate package, update tests and add TGX-CLIENT header, update package version.
1 parent 4bea947 commit 3de3dca

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

config.go renamed to config/config.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package go_sdk
1+
package config
22

33
import (
44
"github.com/joho/godotenv"
@@ -7,7 +7,7 @@ import (
77
)
88

99
const (
10-
Version = "v0.1.2"
10+
Version = "0.2.0"
1111
)
1212

1313
type TestConfig struct {
@@ -17,12 +17,16 @@ type TestConfig struct {
1717

1818
var TestCfg *TestConfig
1919

20-
func GetTestConfig() *TestConfig {
20+
func GetTestConfig(envPath ...string) *TestConfig {
2121
if TestCfg != nil {
2222
return TestCfg
2323
}
2424

25-
InitEnv()
25+
if len(envPath) > 0 {
26+
InitEnv(envPath[0])
27+
} else {
28+
InitEnv()
29+
}
2630

2731
TestCfg = &TestConfig{
2832
SandboxURL: getEnvString("TEST_SANDBOX_URL", ""),
@@ -32,9 +36,15 @@ func GetTestConfig() *TestConfig {
3236
return TestCfg
3337
}
3438

35-
func InitEnv() {
36-
if err := godotenv.Load("../.env"); err != nil {
37-
log.Print(".env file not found")
39+
func InitEnv(envPath ...string) {
40+
if len(envPath) > 0 {
41+
if err := godotenv.Load(envPath[0]); err != nil {
42+
log.Print(".env file not found")
43+
}
44+
} else {
45+
if err := godotenv.Load(); err != nil {
46+
log.Print(".env file not found")
47+
}
3848
}
3949
}
4050

net/http/http.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"github.com/tradologics/go-sdk/backtest"
7+
"github.com/tradologics/go-sdk/config"
78
"io"
89
"log"
910
_http "net/http"
@@ -95,13 +96,17 @@ func (c *Client) processRequest(method, url, contentType string, body io.Reader,
9596
return nil, err
9697
}
9798

99+
// Set auth header
98100
if _, ok := req.Header["Authorization"]; !ok {
99101
if Token == "" && !IsBacktest {
100102
return nil, errors.New("please use `SetToken(...)` first")
101103
}
102104
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", Token))
103105
}
104106

107+
// Set client version header
108+
req.Header.Set("TGX-CLIENT", fmt.Sprintf("go-sdk/%s", config.Version))
109+
105110
if strings.HasSuffix(req.URL.Path, "/") {
106111
req.URL.Path = req.URL.Path[:len(req.URL.Path)-1]
107112
}

net/http/http_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"github.com/stretchr/testify/assert"
88
"github.com/tradologics/go-sdk/backtest"
9+
"github.com/tradologics/go-sdk/config"
910
"io"
1011
"log"
1112
"reflect"
@@ -15,7 +16,10 @@ import (
1516

1617
const invalidErrorMsg = "invalid response"
1718

18-
const token = ""
19+
func authInit() {
20+
cfg := config.GetTestConfig("../../.env")
21+
SetToken(cfg.SandboxToken)
22+
}
1923

2024
func turnOnBacktestModel() {
2125
err := SetBacktestMode("2021-01-01 21:00:00.000000", "2021-01-08 21:00:00.000000")
@@ -39,7 +43,7 @@ func removeBacktestMode() {
3943
}
4044

4145
func removeToken() {
42-
Token = ""
46+
SetToken("")
4347
}
4448

4549
func cls(b io.ReadCloser) {
@@ -130,7 +134,7 @@ func TestTradologicsGetWithEmptyToken(t *testing.T) {
130134
}
131135

132136
func TestTradologicsGetWithToken(t *testing.T) {
133-
SetToken(token)
137+
authInit()
134138
defer removeToken()
135139

136140
res, err := Get("/me")
@@ -192,7 +196,7 @@ func TestTradologicsPostWithEmptyToken(t *testing.T) {
192196
}
193197

194198
func TestTradologicsPostWithToken(t *testing.T) {
195-
SetToken(token)
199+
authInit()
196200
defer removeToken()
197201

198202
payload, err := json.Marshal(map[string]interface{}{
@@ -277,7 +281,7 @@ func TestTradologicsNewRequestWithEmptyToken(t *testing.T) {
277281
}
278282

279283
func TestTradologicsNewRequestWithToken(t *testing.T) {
280-
SetToken(token)
284+
authInit()
281285
defer removeToken()
282286

283287
payload, err := json.Marshal(map[string]interface{}{

sandbox/sandbox.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package sandbox
22

33
import (
44
"fmt"
5-
go_sdk "github.com/tradologics/go-sdk"
5+
"github.com/tradologics/go-sdk/config"
66
"io/ioutil"
77
"log"
88
"net/http"
@@ -36,7 +36,7 @@ func Tradehook(kind string, strategy func(string, []byte), args map[string]inter
3636

3737
// Add user auth token and client version
3838
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", Token))
39-
req.Header.Set("TGX-CLIENT", fmt.Sprintf("go-sdk/%s", go_sdk.Version))
39+
req.Header.Set("TGX-CLIENT", fmt.Sprintf("go-sdk/%s", config.Version))
4040

4141
if len(args) > 0 {
4242
values := req.URL.Query()

sandbox/sandbox_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package sandbox
33
import (
44
"encoding/json"
55
"github.com/stretchr/testify/assert"
6-
go_sdk "github.com/tradologics/go-sdk"
6+
"github.com/tradologics/go-sdk/config"
77
"testing"
88
"time"
99
)
@@ -63,7 +63,7 @@ type orderPayload struct {
6363
}
6464

6565
func authInit() {
66-
cfg := go_sdk.GetTestConfig()
66+
cfg := config.GetTestConfig("../.env")
6767
setSandboxURL(cfg.SandboxURL)
6868
SetToken(cfg.SandboxToken)
6969
}
@@ -106,7 +106,7 @@ func TestTradehookValidToken(t *testing.T) {
106106
}
107107

108108
func TestTradehookInvalidToken(t *testing.T) {
109-
cfg := go_sdk.GetTestConfig()
109+
cfg := config.GetTestConfig()
110110
setSandboxURL(cfg.SandboxURL)
111111
SetToken("")
112112

0 commit comments

Comments
 (0)