Skip to content

Commit

Permalink
add constructor to check url error
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan.sung authored and Alan.sung committed Sep 6, 2023
1 parent 7550ea2 commit dbf29f8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
6 changes: 2 additions & 4 deletions examples/okex-book/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ var rootCmd = &cobra.Command{
return errors.New("empty key, secret or passphrase")
}

client, err := okexapi.NewClient()
if err != nil {
return errors.New("init client error: please check url")
}
client := okexapi.NewClient()

client.Auth(key, secret, passphrase)

instruments, err := client.NewGetInstrumentsRequest().
Expand Down
5 changes: 1 addition & 4 deletions pkg/exchange/okex/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ type Exchange struct {
}

func New(key, secret, passphrase string) (*Exchange, error) {
client, err := okexapi.NewClient()
if err != nil {
return nil, err
}
client := okexapi.NewClient()

if len(key) > 0 && len(secret) > 0 {
client.Auth(key, secret, passphrase)
Expand Down
15 changes: 10 additions & 5 deletions pkg/exchange/okex/okexapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,26 @@ type RestClient struct {
Key, Secret, Passphrase string
}

func NewClient() (*RestClient, error) {
u, err := url.Parse(RestBaseURL)
var parsedBaseURL *url.URL

func init() {
url, err := url.Parse(RestBaseURL)
if err != nil {
return nil, err
panic(err)
}
parsedBaseURL = url
}

func NewClient() *RestClient {
client := &RestClient{
BaseAPIClient: requestgen.BaseAPIClient{
BaseURL: u,
BaseURL: parsedBaseURL,
HttpClient: &http.Client{
Timeout: defaultHTTPTimeout,
},
},
}
return client, nil
return client
}

func (c *RestClient) Auth(key, secret, passphrase string) {
Expand Down
9 changes: 3 additions & 6 deletions pkg/exchange/okex/okexapi/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ func getTestClientOrSkip(t *testing.T) *RestClient {
return nil
}

client, err := NewClient()
assert.NoError(t, err)
client := NewClient()
client.Auth(key, secret, passphrase)
return client
}

func TestClient_GetInstrumentsRequest(t *testing.T) {
client, err := NewClient()
assert.NoError(t, err)
client := NewClient()
ctx := context.Background()
req := client.NewGetInstrumentsRequest()

Expand All @@ -43,8 +41,7 @@ func TestClient_GetInstrumentsRequest(t *testing.T) {
}

func TestClient_GetFundingRateRequest(t *testing.T) {
client, err := NewClient()
assert.NoError(t, err)
client := NewClient()
ctx := context.Background()
req := client.NewGetFundingRate()

Expand Down

0 comments on commit dbf29f8

Please sign in to comment.