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")
}
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().

View File

@ -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)

View File

@ -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) {

View File

@ -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()
@ -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()