Skip to content

Commit

Permalink
feat: add kucoin least api codes
Browse files Browse the repository at this point in the history
  • Loading branch information
linstohu committed Nov 13, 2023
1 parent 3d81a6d commit fcd8b65
Show file tree
Hide file tree
Showing 8 changed files with 759 additions and 0 deletions.
122 changes: 122 additions & 0 deletions kucoin/rest/account/client.go
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
}
51 changes: 51 additions & 0 deletions kucoin/rest/account/client_test.go
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)
}
33 changes: 33 additions & 0 deletions kucoin/rest/account/types/accounts.go
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"`
}
Loading

0 comments on commit fcd8b65

Please sign in to comment.