Skip to content

Commit

Permalink
Merge pull request #11 from ksysoev:refactor_response_parsing
Browse files Browse the repository at this point in the history
Remove API response interface
  • Loading branch information
ksysoev authored Aug 17, 2024
2 parents dd8f9c9 + c9c1621 commit 6c566ed
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 25 deletions.
9 changes: 2 additions & 7 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
15 changes: 3 additions & 12 deletions subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6c566ed

Please sign in to comment.