add constructor to check url error

This commit is contained in:
Alan.sung 2023-09-06 21:21:13 +08:00
parent 7550ea2be1
commit dbf29f8cd2
4 changed files with 18 additions and 21 deletions

View File

@ -44,10 +44,8 @@ var rootCmd = &cobra.Command{
return errors.New("empty key, secret or passphrase") return errors.New("empty key, secret or passphrase")
} }
client, err := okexapi.NewClient() client := okexapi.NewClient()
if err != nil {
return errors.New("init client error: please check url")
}
client.Auth(key, secret, passphrase) client.Auth(key, secret, passphrase)
instruments, err := client.NewGetInstrumentsRequest(). instruments, err := client.NewGetInstrumentsRequest().

View File

@ -35,10 +35,7 @@ type Exchange struct {
} }
func New(key, secret, passphrase string) (*Exchange, error) { func New(key, secret, passphrase string) (*Exchange, error) {
client, err := okexapi.NewClient() client := okexapi.NewClient()
if err != nil {
return nil, err
}
if len(key) > 0 && len(secret) > 0 { if len(key) > 0 && len(secret) > 0 {
client.Auth(key, secret, passphrase) client.Auth(key, secret, passphrase)

View File

@ -66,21 +66,26 @@ type RestClient struct {
Key, Secret, Passphrase string Key, Secret, Passphrase string
} }
func NewClient() (*RestClient, error) { var parsedBaseURL *url.URL
u, err := url.Parse(RestBaseURL)
func init() {
url, err := url.Parse(RestBaseURL)
if err != nil { if err != nil {
return nil, err panic(err)
}
parsedBaseURL = url
} }
func NewClient() *RestClient {
client := &RestClient{ client := &RestClient{
BaseAPIClient: requestgen.BaseAPIClient{ BaseAPIClient: requestgen.BaseAPIClient{
BaseURL: u, BaseURL: parsedBaseURL,
HttpClient: &http.Client{ HttpClient: &http.Client{
Timeout: defaultHTTPTimeout, Timeout: defaultHTTPTimeout,
}, },
}, },
} }
return client, nil return client
} }
func (c *RestClient) Auth(key, secret, passphrase string) { func (c *RestClient) Auth(key, secret, passphrase string) {

View File

@ -22,15 +22,13 @@ func getTestClientOrSkip(t *testing.T) *RestClient {
return nil return nil
} }
client, err := NewClient() client := NewClient()
assert.NoError(t, err)
client.Auth(key, secret, passphrase) client.Auth(key, secret, passphrase)
return client return client
} }
func TestClient_GetInstrumentsRequest(t *testing.T) { func TestClient_GetInstrumentsRequest(t *testing.T) {
client, err := NewClient() client := NewClient()
assert.NoError(t, err)
ctx := context.Background() ctx := context.Background()
req := client.NewGetInstrumentsRequest() req := client.NewGetInstrumentsRequest()
@ -43,8 +41,7 @@ func TestClient_GetInstrumentsRequest(t *testing.T) {
} }
func TestClient_GetFundingRateRequest(t *testing.T) { func TestClient_GetFundingRateRequest(t *testing.T) {
client, err := NewClient() client := NewClient()
assert.NoError(t, err)
ctx := context.Background() ctx := context.Background()
req := client.NewGetFundingRate() req := client.NewGetFundingRate()