From c9c16211f7303c83edabaaf08c9d2b9860d30e7e Mon Sep 17 00:00:00 2001 From: Kirill Sysoev Date: Sat, 17 Aug 2024 15:48:02 +0800 Subject: [PATCH] Removes API response interface --- api.go | 9 ++------- go.mod | 6 ++---- go.sum | 2 -- subscriptions.go | 15 +++------------ 4 files changed, 7 insertions(+), 25 deletions(-) diff --git a/api.go b/api.go index 2f43678..dba1354 100644 --- a/api.go +++ b/api.go @@ -41,11 +41,6 @@ type ApiReqest struct { respChan chan []byte } -// ApiObjectRequest is an interface for all API requests that return an object. -type ApiResponse interface { - UnmarshalJSON([]byte) error -} - type APIResponseReqID struct { ReqID int `json:"req_id"` } @@ -350,7 +345,7 @@ func (api *DerivAPI) Send(reqID int, request any) (chan []byte, error) { } // SendRequest sends a request to the Deriv API and returns the response -func (api *DerivAPI) SendRequest(reqID int, request any, response ApiResponse) (err error) { +func (api *DerivAPI) SendRequest(reqID int, request any, response any) (err error) { respChan, err := api.Send(reqID, request) if err != nil { @@ -373,7 +368,7 @@ func (api *DerivAPI) SendRequest(reqID int, request any, response ApiResponse) ( return err } - err = response.UnmarshalJSON([]byte(responseJSON)) + err = json.Unmarshal(responseJSON, response) if err != nil { api.logDebugf("Failed to parse response for request %d: %s", reqID, err.Error()) return err diff --git a/go.mod b/go.mod index f01c449..9f673e6 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,7 @@ module github.com/ksysoev/deriv-api go 1.20 -require golang.org/x/net v0.24.0 - require ( - github.com/coder/websocket v1.8.12 // indirect - nhooyr.io/websocket v1.8.17 // indirect + github.com/coder/websocket v1.8.12 + golang.org/x/net v0.24.0 ) diff --git a/go.sum b/go.sum index 3409867..561da7c 100644 --- a/go.sum +++ b/go.sum @@ -2,5 +2,3 @@ github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NA github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs= golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= -nhooyr.io/websocket v1.8.17 h1:KEVeLJkUywCKVsnLIDlD/5gtayKp8VoCkksHCGGfT9Y= -nhooyr.io/websocket v1.8.17/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c= diff --git a/subscriptions.go b/subscriptions.go index 31c8df4..cd1d0f4 100644 --- a/subscriptions.go +++ b/subscriptions.go @@ -96,7 +96,7 @@ func (s *Subsciption[initResp, Resp]) Forget() error { // the method returns the error. If deserialization is successful, the SubsciptionID field of the Subscription instance // is set to the subscription ID returned by the API, and the IsActive field is set to true. The method then sends the // initial subscription update to the Stream channel, and starts a new goroutine to handle subsequent updates received -// on the channel. If the response object does not implement the ApiResponse interface, the method returns an error +// on the channel. func (s *Subsciption[initResp, Resp]) Start(reqID int, request any) (initResp, error) { var resp initResp @@ -129,12 +129,7 @@ func (s *Subsciption[initResp, Resp]) Start(reqID int, request any) (initResp, e } s.SubsciptionID = subResp.Subscription.ID - apiResp, ok := any(&resp).(ApiResponse) - if !ok { - panic("Response object must implement ApiResponse interface") - } - - err = apiResp.UnmarshalJSON(initResponse) + err = json.Unmarshal(initResponse, &resp) if err != nil { s.API.logDebugf("Failed to parse response for request %d: %s", reqID, err.Error()) s.API.closeRequestChannel(reqID) @@ -168,12 +163,8 @@ func (s *Subsciption[initResp, Resp]) messageHandler(inChan chan []byte) { } var response Resp - apiResp, ok := any(&response).(ApiResponse) - if !ok { - panic("Response object must implement ApiResponse interface") - } - err = apiResp.UnmarshalJSON([]byte(rawResponse)) + err = json.Unmarshal(rawResponse, &response) if err != nil { s.API.logDebugf("Failed to parse response in subscription: %s", err.Error()) continue