Skip to content

Commit 87da727

Browse files
authored
Merge pull request #84 from binance/rc-common-v1.1.0
2 parents 014dcf3 + b850666 commit 87da727

File tree

7 files changed

+166
-48
lines changed

7 files changed

+166
-48
lines changed

.github/workflows/release.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ jobs:
172172
PATCH=${VERSION_PARTS[2]}
173173
174174
# Increment patch version
175-
NEW_PATCH=$((PATCH + 1))
176-
NEW_VERSION="v${MAJOR}.${MINOR}.${NEW_PATCH}"
175+
NEW_MINOR=$((MINOR + 1))
176+
NEW_VERSION="v${MAJOR}.${NEW_MINOR}.${PATCH}"
177177
fi
178178
179179
# Set the full tag name

common/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
### Changelog
22

3+
## 1.1.0 - 2026-01-13
4+
5+
- Updated `WebsocketStreams` `Subscribe` method to support `int32` format for `id` parameter
6+
- Support Derivatives Trading Options different WS Streams URL paths.
7+
38
## 1.0.1 - 2025-12-17
49

5-
- Fix `common` tags issue
10+
- Fixed `common` tags issue
611

712
## 1.0.0 - 2025-12-16
813

common/common/constants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const DerivativesTradingUsdsFuturesWebsocketStreamsTestnetUrl = "wss://stream.bi
6969

7070
// Derivatives Trading Options API URLs
7171
const DerivativesTradingOptionsRestApiProdUrl = "https://eapi.binance.com"
72-
const DerivativesTradingOptionsWebsocketStreamsProdUrl = "wss://nbstream.binance.com/eoptions/stream"
72+
const DerivativesTradingOptionsWebsocketStreamsProdUrl = "wss://fstream.binance.com"
7373

7474
// Derivatives Trading Portfolio Margin API URLs
7575
const DerivativesTradingPortfolioMarginRestApiProdUrl = "https://papi.binance.com"

common/common/utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"errors"
2323
"fmt"
2424
"io"
25+
randpkg "math/rand/v2"
2526
"net"
2627
"net/http"
2728
"net/url"
@@ -1003,6 +1004,13 @@ var GenerateUUID = func() string {
10031004
return uuid.New().String()
10041005
}
10051006

1007+
// GenerateIntUUID is a function that generates a new random int32 UUID.
1008+
//
1009+
// @return An int32 representing the generated UUID.
1010+
var GenerateIntUUID = func() int32 {
1011+
return randpkg.Int32()
1012+
}
1013+
10061014
// Pretty converts a value to its pretty-printed JSON string representation.
10071015
//
10081016
// @param v The value to be pretty-printed.

common/common/websocket.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ func (w *WebsocketStreams) Connect(userAgent string) error {
11301130
// @param streams A slice of stream names to subscribe to.
11311131
// @param id A slice of IDs corresponding to each stream. If empty, new UUIDs will be generated.
11321132
// @return An error if any subscription fails, otherwise nil.
1133-
func (w *WebsocketStreams) Subscribe(streams []string, id []string) error {
1133+
func (w *WebsocketStreams) Subscribe(streams []string, id []any, strictInt bool) error {
11341134
if len(streams) == 0 {
11351135
return errors.New("no streams to subscribe")
11361136
}
@@ -1145,7 +1145,13 @@ func (w *WebsocketStreams) Subscribe(streams []string, id []string) error {
11451145
return err
11461146
}
11471147

1148-
streamID := GenerateUUID()
1148+
var streamID interface{}
1149+
if strictInt {
1150+
streamID = GenerateIntUUID()
1151+
} else {
1152+
streamID = GenerateUUID()
1153+
}
1154+
11491155
if len(id) > num {
11501156
streamID = id[num]
11511157
}
@@ -1337,9 +1343,9 @@ func (w *WebsocketStreams) CloseWebSocketStreamConnection() error {
13371343
// @param stream The name of the stream to handle.
13381344
// @param id An optional slice of IDs associated with the stream.
13391345
// @return A pointer to the created StreamHandler or an error if subscription fails.
1340-
func CreateStreamHandler[T any](wrapper *StreamHandlerWrapper, stream string, id []string) (*StreamHandler[T], error) {
1346+
func CreateStreamHandler[T any](wrapper *StreamHandlerWrapper, stream string, id []any, strictInt bool) (*StreamHandler[T], error) {
13411347
if wrapper.WebsocketStreams != nil {
1342-
if err := wrapper.WebsocketStreams.Subscribe([]string{stream}, id); err != nil {
1348+
if err := wrapper.WebsocketStreams.Subscribe([]string{stream}, id, strictInt); err != nil {
13431349
return nil, err
13441350
}
13451351
} else if wrapper.WebsocketAPI != nil {

common/tests/unit/utils_test.go

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"strings"
1616
"testing"
1717
"time"
18+
"unicode/utf8"
1819

1920
"github.com/binance/binance-connector-go/common/common"
2021
)
@@ -31,10 +32,10 @@ func (m mockMappedNullable) ToMap() (map[string]interface{}, error) {
3132

3233
func TestParameterAddToHeaderOrQuery_BasicTypes(t *testing.T) {
3334
tests := []struct {
34-
name string
35-
obj interface{}
35+
name string
36+
obj interface{}
3637
expectedValue string
37-
expectedKey string
38+
expectedKey string
3839
collectionType string
3940
}{
4041
{"int", 42, "42", "intKey", ""},
@@ -588,7 +589,7 @@ func TestPrepareRequest_WithJSONBody(t *testing.T) {
588589
cfg := &common.ConfigurationRestAPI{}
589590
body := struct {
590591
Key1 string `json:"key1"`
591-
Key2 int `json:"key2"`
592+
Key2 int `json:"key2"`
592593
}{
593594
Key1: "value1",
594595
Key2: 123,
@@ -640,3 +641,58 @@ func TestPrepareRequest_WithQueryAndBody(t *testing.T) {
640641
t.Errorf("Expected Content-Type header to be set")
641642
}
642643
}
644+
645+
func TestGenerateUUID(t *testing.T) {
646+
t.Run("returns non-empty string", func(t *testing.T) {
647+
uuid := common.GenerateUUID()
648+
if len(uuid) == 0 {
649+
t.Error("GenerateUUID() returned empty string")
650+
}
651+
})
652+
653+
t.Run("returns valid UTF-8 string", func(t *testing.T) {
654+
uuid := common.GenerateUUID()
655+
if !utf8.ValidString(uuid) {
656+
t.Error("GenerateUUID() returned invalid UTF-8 string")
657+
}
658+
})
659+
660+
t.Run("returns string of expected length", func(t *testing.T) {
661+
uuid := common.GenerateUUID()
662+
663+
if len(uuid) != 36 {
664+
t.Errorf("GenerateUUID() returned string of length %d, expected 36", len(uuid))
665+
}
666+
})
667+
668+
t.Run("returns different values on subsequent calls", func(t *testing.T) {
669+
first := common.GenerateUUID()
670+
second := common.GenerateUUID()
671+
if first == second {
672+
t.Error("GenerateUUID() returned same value on subsequent calls")
673+
}
674+
})
675+
}
676+
677+
func TestGenerateIntUUID(t *testing.T) {
678+
t.Run("returns a value", func(t *testing.T) {
679+
uuid := common.GenerateIntUUID()
680+
_ = uuid
681+
})
682+
683+
t.Run("returns different values on subsequent calls", func(t *testing.T) {
684+
first := common.GenerateIntUUID()
685+
second := common.GenerateIntUUID()
686+
if first == second {
687+
t.Error("GenerateIntUUID() returned same value on subsequent calls")
688+
}
689+
})
690+
691+
t.Run("returns value within int32 range", func(t *testing.T) {
692+
uuid := common.GenerateIntUUID()
693+
694+
if uuid < -2147483648 || uuid > 2147483647 {
695+
t.Errorf("GenerateIntUUID() returned value %d outside int32 range", uuid)
696+
}
697+
})
698+
}

0 commit comments

Comments
 (0)