Compare commits

..

4 Commits

Author SHA1 Message Date
yyasha 6f0dec9e93 fix ws closing 2024-06-05 10:38:16 +03:00
yyasha 784d4a6006 fix ws closing 2024-06-05 01:08:40 +03:00
yyasha 978ab02672 switch package name 2024-06-03 23:07:14 +03:00
CommaHunger 8a4f616e98
Merge pull request #29 from wuhewuhe/VictorWuBranche
fix asset endpoints
2024-02-22 18:21:00 +01:00
26 changed files with 80 additions and 106 deletions

View File

@ -1,28 +0,0 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
name: Go
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
- name: Build
run: go build -v ./...
- name: Test
run: go test -v ./...

8
.idea/.gitignore vendored
View File

@ -1,8 +0,0 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/bybit.go.api.iml" filepath="$PROJECT_DIR$/.idea/bybit.go.api.iml" />
</modules>
</component>
</project>

View File

@ -1,7 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -2,8 +2,9 @@ package bybit_connector
import (
"context"
"github.com/wuhewuhe/bybit.go.api/handlers"
"net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
)
type AccountClient struct {

View File

@ -2,8 +2,9 @@ package bybit_connector
import (
"context"
"github.com/wuhewuhe/bybit.go.api/handlers"
"net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
)
type AssetClient struct {
@ -471,6 +472,7 @@ func (s *AssetClient) GetWithdrawalAmount(ctx context.Context, opts ...RequestOp
}
return res, nil
}
func (s *AssetClient) GetWithdrawalRecords(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
r := &request{
method: http.MethodGet,

View File

@ -2,8 +2,9 @@ package bybit_connector
import (
"context"
"github.com/wuhewuhe/bybit.go.api/handlers"
"net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
)
type BrokerServiceClient struct {

View File

@ -14,9 +14,9 @@ import (
"strconv"
"time"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
"github.com/bitly/go-simplejson"
jsoniter "github.com/json-iterator/go"
"github.com/wuhewuhe/bybit.go.api/handlers"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
@ -199,9 +199,7 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
c.debug("response status code: %d", res.StatusCode)
if res.StatusCode >= http.StatusBadRequest {
var (
apiErr = new(handlers.APIError)
)
apiErr := new(handlers.APIError)
e := json.Unmarshal(data, apiErr)
if e != nil {
c.debug("failed to unmarshal json: %s", e)
@ -310,7 +308,6 @@ func (c *Client) NewPositionService(params map[string]interface{}) *PositionClie
c: c,
params: params,
}
}
func (c *Client) NewPreUpgradeService(params map[string]interface{}) *PreUpgradeClient {

View File

@ -5,28 +5,30 @@ import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"time"
"github.com/google/uuid"
"github.com/gorilla/websocket"
"golang.org/x/sync/errgroup"
)
type MessageHandler func(message string) error
func (b *WebSocket) handleIncomingMessages() {
func (b *WebSocket) handleIncomingMessages() error {
for {
_, message, err := b.conn.ReadMessage()
if err != nil {
fmt.Println("Error reading:", err)
return
return err
}
if b.onMessage != nil {
err := b.onMessage(string(message))
if err != nil {
fmt.Println("Error handling message:", err)
return
return err
}
}
}
@ -112,35 +114,49 @@ func (b *WebSocket) Connect(args []string) error {
}
}
go b.handleIncomingMessages()
eg := errgroup.Group{}
b.ctx, b.cancel = context.WithCancel(context.Background())
Ping(b)
return b.sendSubscription(args)
eg.Go(b.Ping)
eg.Go(b.handleIncomingMessages)
if err := b.sendSubscription(args); err != nil {
return fmt.Errorf("failed to send subscription: %w", err)
}
func Ping(b *WebSocket) {
if err := eg.Wait(); err != nil {
b.Disconnect()
return fmt.Errorf("failed to handle message: %w", err)
}
return nil
}
var ErrPingFailed = errors.New("failed to send ping")
func (b *WebSocket) Ping() error {
ticker := time.NewTicker(time.Duration(b.pingInterval) * time.Second)
go func() {
defer ticker.Stop() // Ensure the ticker is stopped when this goroutine ends
for {
select {
case <-ticker.C: // Wait until the ticker sends a signal
if err := b.conn.WriteMessage(websocket.PingMessage, nil); err != nil {
fmt.Println("Failed to send ping:", err)
return ErrPingFailed
}
case <-b.ctx.Done():
fmt.Println("Exit ping")
return
return nil
}
}
}()
}
func (b *WebSocket) Disconnect() error {
b.cancel()
return b.conn.Close()
err := b.conn.Close()
fmt.Println("WebSocket disconnected, err =", err)
return err
}
func (b *WebSocket) Send(message string) error {

View File

@ -3,7 +3,8 @@ package main
import (
"context"
"fmt"
bybit "github.com/wuhewuhe/bybit.go.api"
bybit "gitea.computernetthings.ru/yash/bybit.go.api"
)
func main() {

View File

@ -3,7 +3,8 @@ package main
import (
"context"
"fmt"
bybit "github.com/wuhewuhe/bybit.go.api"
bybit "gitea.computernetthings.ru/yash/bybit.go.api"
)
func main() {

View File

@ -3,7 +3,8 @@ package main
import (
"context"
"fmt"
bybit "github.com/wuhewuhe/bybit.go.api"
bybit "gitea.computernetthings.ru/yash/bybit.go.api"
)
func main() {

View File

@ -3,7 +3,8 @@ package main
import (
"context"
"fmt"
bybit "github.com/wuhewuhe/bybit.go.api"
bybit "gitea.computernetthings.ru/yash/bybit.go.api"
)
func main() {
@ -12,7 +13,8 @@ func main() {
func PlaceBatchTrade() {
client := bybit.NewBybitHttpClient("8wYkmpLsMg10eNQyPm", "Ouxc34myDnXvei54XsBZgoQzfGxO4bkr2Zsj", bybit.WithBaseURL(bybit.TESTNET))
params := map[string]interface{}{"category": "option",
params := map[string]interface{}{
"category": "option",
"request": []map[string]interface{}{
{
"category": "option",

View File

@ -3,7 +3,8 @@ package main
import (
"context"
"fmt"
bybit "github.com/wuhewuhe/bybit.go.api"
bybit "gitea.computernetthings.ru/yash/bybit.go.api"
)
func main() {

3
go.mod
View File

@ -1,4 +1,4 @@
module github.com/wuhewuhe/bybit.go.api
module gitea.computernetthings.ru/yash/bybit.go.api
go 1.21
@ -8,6 +8,7 @@ require (
github.com/gorilla/websocket v1.5.1
github.com/json-iterator/go v1.1.12
github.com/stretchr/testify v1.8.4
golang.org/x/sync v0.7.0
)
require (

3
go.sum
View File

@ -10,7 +10,6 @@ github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
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/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/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@ -31,6 +30,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -2,8 +2,9 @@ package bybit_connector
import (
"context"
"github.com/wuhewuhe/bybit.go.api/handlers"
"net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
)
type LendingServiceClient struct {

View File

@ -5,7 +5,7 @@ import (
"fmt"
"net/http"
"github.com/wuhewuhe/bybit.go.api/models"
"gitea.computernetthings.ru/yash/bybit.go.api/models"
)
// MarketKlinesService Market Kline (GET /v5/market/kline)

View File

@ -4,8 +4,8 @@ import (
"net/http"
"testing"
"gitea.computernetthings.ru/yash/bybit.go.api/models"
"github.com/stretchr/testify/suite"
"github.com/wuhewuhe/bybit.go.api/models"
)
type marketTestSuite struct {
@ -128,6 +128,7 @@ func (s *marketTestSuite) assertMarketKlineEqual(expected, actual *models.Market
r.Equal(len(expected.List), len(actual.List), "List")
r.Equal(expected.List, actual.List)
}
func (s *marketTestSuite) TestMarketMarkPriceKline() {
data := []byte(`{
"retCode": 0,
@ -1296,6 +1297,7 @@ func (s *marketTestSuite) assertMarketLongShortRatioInfoEqual(expected, actual *
r := s.r()
r.Equal(expected.List, actual.List, "List")
}
func (s *marketTestSuite) TestGetServerTime() {
data := []byte(`{
"retCode": 0,

View File

@ -2,8 +2,9 @@ package bybit_connector
import (
"context"
"github.com/wuhewuhe/bybit.go.api/handlers"
"net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
)
type PositionClient struct {

View File

@ -2,8 +2,9 @@ package bybit_connector
import (
"context"
"github.com/wuhewuhe/bybit.go.api/handlers"
"net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
)
type PreUpgradeClient struct {

View File

@ -2,8 +2,9 @@ package bybit_connector
import (
"context"
"github.com/wuhewuhe/bybit.go.api/handlers"
"net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
)
type SpotLeverageClient struct {

View File

@ -3,8 +3,9 @@ package bybit_connector
import (
"context"
"errors"
"github.com/wuhewuhe/bybit.go.api/handlers"
"net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
)
type SpotMarginClient struct {

View File

@ -2,9 +2,10 @@ package bybit_connector
import (
"context"
"github.com/wuhewuhe/bybit.go.api/handlers"
"github.com/wuhewuhe/bybit.go.api/models"
"net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
"gitea.computernetthings.ru/yash/bybit.go.api/models"
)
type TradeClient struct {

View File

@ -2,8 +2,9 @@ package bybit_connector
import (
"context"
"github.com/wuhewuhe/bybit.go.api/handlers"
"net/http"
"gitea.computernetthings.ru/yash/bybit.go.api/handlers"
)
type UserServiceClient struct {