Skip to content

Commit

Permalink
✨ Login
Browse files Browse the repository at this point in the history
  • Loading branch information
MateoGreil committed May 7, 2024
1 parent 4ef89d4 commit b1e8ac2
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 0 deletions.
8 changes: 8 additions & 0 deletions go.mod
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
)
4 changes: 4 additions & 0 deletions go.sum
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=
13 changes: 13 additions & 0 deletions internal/protocols/socket/request.go
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"`
}
8 changes: 8 additions & 0 deletions internal/protocols/socket/response.go
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"`
}
6 changes: 6 additions & 0 deletions internal/protocols/stream/request.go
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"`
}
87 changes: 87 additions & 0 deletions xapi.go
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)
}
13 changes: 13 additions & 0 deletions xapi_test.go
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)
}
}

0 comments on commit b1e8ac2

Please sign in to comment.