-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ef89d4
commit b1e8ac2
Showing
7 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/MateoGreil/xapi-go | ||
|
||
go 1.22.1 | ||
|
||
require ( | ||
github.com/gorilla/websocket v1.5.1 // indirect | ||
golang.org/x/net v0.17.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= | ||
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= | ||
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= | ||
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package socket | ||
|
||
type Request struct { | ||
Command string `json:"command"` | ||
Arguments RequestArguments `json:"arguments"` | ||
} | ||
|
||
type RequestArguments interface{} | ||
|
||
type LoginRequestArguments struct { | ||
UserId string `json:"userId"` | ||
Password string `json:"password"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package socket | ||
|
||
type LoginResponse struct { | ||
Status bool `json:"status"` | ||
StreamSessionId string `json:"streamSessionId"` | ||
ErrorCode string `json:"errorCode"` | ||
ErrorDescr string `json:"errorDescr"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package stream | ||
|
||
type Request struct { | ||
Command string `json:"command"` | ||
StreamSessionId string `json:"streamSessionId"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package xapi | ||
|
||
import ( | ||
"fmt" | ||
|
||
socket "github.com/MateoGreil/xapi-go/internal/protocols/socket" | ||
stream "github.com/MateoGreil/xapi-go/internal/protocols/stream" | ||
"github.com/gorilla/websocket" | ||
) | ||
|
||
type client struct { | ||
conn *websocket.Conn | ||
streamConn *websocket.Conn | ||
streamSessionId string | ||
} | ||
|
||
const ( | ||
websocketBaseURL = "wss://ws.xtb.com" | ||
) | ||
|
||
func NewClient(userId string, password string, connectionType string) (*client, error) { | ||
var websocketURL string | ||
var websocketStreamURL string | ||
|
||
switch connectionType { | ||
case "demo": | ||
websocketURL = websocketBaseURL + "/demo" | ||
websocketStreamURL = websocketBaseURL + "/demoStream" | ||
case "real": | ||
websocketURL = websocketBaseURL + "/real" | ||
websocketStreamURL = websocketBaseURL + "/realStream" | ||
} | ||
|
||
conn, _, err := websocket.DefaultDialer.Dial(websocketURL, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
streamSessionId, err := login(conn, userId, password) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
streamConn, _, err := websocket.DefaultDialer.Dial(websocketStreamURL, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
getKeepAlive(conn, streamSessionId) | ||
|
||
c := &client{ | ||
conn: conn, | ||
streamConn: streamConn, | ||
streamSessionId: streamSessionId, | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
func login(conn *websocket.Conn, userId string, password string) (string, error) { | ||
request := socket.Request{ | ||
Command: "login", | ||
Arguments: socket.LoginRequestArguments{ | ||
UserId: userId, | ||
Password: password, | ||
}, | ||
} | ||
|
||
fmt.Printf("%+v\n", request) | ||
conn.WriteJSON(request) | ||
response := socket.LoginResponse{} | ||
err := conn.ReadJSON(&response) | ||
if err != nil { | ||
return "", err | ||
} | ||
if response.Status == false { | ||
return "", fmt.Errorf("%+v\n", response.ErrorDescr) | ||
} | ||
return response.StreamSessionId, nil | ||
} | ||
|
||
func getKeepAlive(conn *websocket.Conn, streamSessionId string) { | ||
keepAliveReq := stream.Request{ | ||
Command: "getKeepAlive", | ||
StreamSessionId: streamSessionId, | ||
} | ||
conn.WriteJSON(keepAliveReq) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package xapi | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestNewClient(t *testing.T) { | ||
xapiClient, err := NewClient(os.Getenv("XAPI_USER_ID"), os.Getenv("XAPI_PASSWORD"), "demo") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} |