-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
759 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Copyright (c) 2023, LinstoHu | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package account | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"log/slog" | ||
"net/http" | ||
|
||
"github.com/go-playground/validator" | ||
"github.com/linstohu/nexapi/kucoin/rest/account/types" | ||
"github.com/linstohu/nexapi/kucoin/rest/utils" | ||
) | ||
|
||
type AccountClient struct { | ||
cli *utils.KucoinClient | ||
|
||
// validate struct fields | ||
validate *validator.Validate | ||
} | ||
|
||
type AccountClientCfg struct { | ||
Debug bool | ||
// Logger | ||
Logger *slog.Logger | ||
|
||
BaseURL string `validate:"required"` | ||
Key string `validate:"required"` | ||
Secret string `validate:"required"` | ||
Passphrase string `validate:"required"` | ||
} | ||
|
||
func NewAccountClient(cfg *AccountClientCfg) (*AccountClient, error) { | ||
validator := validator.New() | ||
|
||
err := validator.Struct(cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cli, err := utils.NewKucoinRestClient(&utils.KucoinClientCfg{ | ||
Debug: cfg.Debug, | ||
Logger: cfg.Logger, | ||
BaseURL: cfg.BaseURL, | ||
Key: cfg.Key, | ||
Secret: cfg.Secret, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &AccountClient{ | ||
cli: cli, | ||
validate: validator, | ||
}, nil | ||
} | ||
|
||
func (a *AccountClient) GetAccountList(ctx context.Context, param types.GetAccountListParam) ([]*types.AccountModel, error) { | ||
req := utils.HTTPRequest{ | ||
BaseURL: a.cli.GetBaseURL(), | ||
Path: "/api/v1/accounts", | ||
Method: http.MethodGet, | ||
Query: param, | ||
} | ||
|
||
{ | ||
headers, err := a.cli.GetHeaders() | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Headers = headers | ||
} | ||
|
||
{ | ||
err := a.validate.Struct(param) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
h, err := a.cli.GenSignature(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for k, v := range h { | ||
req.Headers[k] = v | ||
} | ||
} | ||
|
||
resp, err := a.cli.SendHTTPRequest(ctx, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ar := &utils.ApiResponse{Resp: resp} | ||
if err := resp.ReadJsonBody(ar); err != nil { | ||
return nil, errors.New(resp.Error()) | ||
} | ||
|
||
var ret []*types.AccountModel | ||
if err := json.Unmarshal(ar.RawData, &ret); err != nil { | ||
return nil, err | ||
} | ||
|
||
return ret, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright (c) 2023, LinstoHu | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package account | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
"github.com/linstohu/nexapi/kucoin/rest/account/types" | ||
"github.com/linstohu/nexapi/kucoin/rest/utils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func testNewAccountClient(t *testing.T) *AccountClient { | ||
cli, err := NewAccountClient(&AccountClientCfg{ | ||
BaseURL: utils.SpotBaseURL, | ||
Key: os.Getenv("KUCOIN_KEY"), | ||
Secret: os.Getenv("KUCOIN_SECRET"), | ||
Debug: true, | ||
}) | ||
|
||
if err != nil { | ||
t.Fatalf("Could not create kucoin client, %s", err) | ||
} | ||
|
||
return cli | ||
} | ||
|
||
func TestGetAccountList(t *testing.T) { | ||
cli := testNewAccountClient(t) | ||
|
||
_, err := cli.GetAccountList(context.TODO(), types.GetAccountListParam{}) | ||
|
||
assert.Nil(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright (c) 2023, LinstoHu | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package types | ||
|
||
type GetAccountListParam struct { | ||
Currency string `url:"currency" validate:"omitempty"` | ||
Type string `url:"type" validate:"omitempty"` | ||
} | ||
|
||
// An AccountModel represents an account. | ||
type AccountModel struct { | ||
Id string `json:"id"` | ||
Currency string `json:"currency"` | ||
Type string `json:"type"` | ||
Balance string `json:"balance"` | ||
Available string `json:"available"` | ||
Holds string `json:"holds"` | ||
} |
Oops, something went wrong.