From 3c484bb816f4e5abb3d6979400d46fad8bd9da3c Mon Sep 17 00:00:00 2001 From: "minhthanh.vu" Date: Thu, 14 Dec 2023 14:27:22 +0700 Subject: [PATCH] add public coin info api --- v2/all_coin_service.go | 47 ++++++++++++++++++++++++++++++++++++++++++ v2/client.go | 4 ++++ v2/go.mod | 4 ++-- v2/go.sum | 3 +++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 v2/all_coin_service.go diff --git a/v2/all_coin_service.go b/v2/all_coin_service.go new file mode 100644 index 00000000..0b4ac137 --- /dev/null +++ b/v2/all_coin_service.go @@ -0,0 +1,47 @@ +package binance + +import ( + "context" + "fmt" + "io" + "net/http" +) + +type AllCoinService struct { + c *Client +} + +type AllCoinResponse struct { + Code string `json:"code"` + Message string `json:"message"` + MessageDetail string `json:"messageDetail"` + Data []CoinInfo `json:"data"` + Success bool `json:"success"` +} + +func (s *AllCoinService) Do(ctx context.Context) ([]CoinInfo, error) { + var endpoint string = "https://www.binance.com/bapi/capital/v1/public/capital/getNetworkCoinAll" + req, err := http.NewRequest(http.MethodGet, endpoint, nil) + if err != nil { + return nil, err + } + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + bb, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + res := AllCoinResponse{} + if err = json.Unmarshal(bb, &res); err != nil { + return nil, err + } + if !res.Success { + return nil, fmt.Errorf("%s", res.MessageDetail) + } + return res.Data, nil +} diff --git a/v2/client.go b/v2/client.go index d4fe1af4..ea084cd2 100644 --- a/v2/client.go +++ b/v2/client.go @@ -1062,3 +1062,7 @@ func (c *Client) NewGetFlexibleLoanBorrowHistoryService() *GetFlexibleLoanBorrow func (c *Client) NewGetFlexibleLoanRepayHistoryService() *GetFlexibleLoanRepayHistoryService { return &GetFlexibleLoanRepayHistoryService{c: c} } + +func (c *Client) NewAllCoinService() *AllCoinService { + return &AllCoinService{c: c} +} diff --git a/v2/go.mod b/v2/go.mod index cb8489e2..742e23c4 100644 --- a/v2/go.mod +++ b/v2/go.mod @@ -11,12 +11,12 @@ require ( ) require ( + github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/kr/text v0.2.0 // indirect + github.com/kr/pretty v0.3.1 // indirect github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/rogpeppe/go-internal v1.9.0 // indirect github.com/stretchr/objx v0.5.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/v2/go.sum b/v2/go.sum index 2f2fc378..b6e48ac5 100644 --- a/v2/go.sum +++ b/v2/go.sum @@ -1,6 +1,7 @@ github.com/bitly/go-simplejson v0.5.0 h1:6IH+V8/tVMab511d5bn4M7EwGXZf9Hj6i2xSwkNEM+Y= github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA= github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 h1:DDGfHa7BWjL4YnC6+E63dPcxHo2sUxDIu8g3QgEJdRY= +github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= @@ -11,12 +12,14 @@ github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=