Skip to content

Commit

Permalink
chore(iqoption): Add refreshLogIn method to IQOptionProvider and WebS…
Browse files Browse the repository at this point in the history
…ocketClient
  • Loading branch information
andredezzy committed Aug 14, 2024
1 parent ea3a757 commit 10e1964
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
10 changes: 9 additions & 1 deletion packages/iqoption/lib/IQOptionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ export class IQOptionProvider implements BaseIQOptionProvider {

private isCorsBypassEnabled: boolean

private lastLogInCredentials: LogInCredentials

constructor() {
this.api = axios.create({
baseURL: 'https://trade.gomerebroker.com/api',
})

this.webSocket = new WebSocketClient()
this.webSocket = new WebSocketClient(this.refreshLogIn)
}

public async enableCorsBypass(): Promise<void> {
Expand All @@ -43,6 +45,8 @@ export class IQOptionProvider implements BaseIQOptionProvider {
}: LogInCredentials): Promise<BaseIQOptionAccount> {
console.log('Logging in...')

this.lastLogInCredentials = { email, password }

await this.webSocket.subscribe()

const authApi = axios.create({
Expand Down Expand Up @@ -76,4 +80,8 @@ export class IQOptionProvider implements BaseIQOptionProvider {

return account
}

public async refreshLogIn(): Promise<void> {
await this.logIn(this.lastLogInCredentials)
}
}
5 changes: 4 additions & 1 deletion packages/iqoption/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface LogInCredentials {
export interface BaseIQOptionProvider {
enableCorsBypass(): Promise<void>
logIn(credentials: LogInCredentials): Promise<BaseIQOptionAccount>
refreshLogIn(): Promise<void>
}

export interface PlaceDigitalOption {
Expand Down Expand Up @@ -108,12 +109,14 @@ export interface WaitForOptions<Message> {

export interface BaseWebSocketClient {
history: WebSocketEventHistory[]
refreshLogIn: () => Promise<void>

subscribe(): Promise<void>

send<Message, Args = undefined>(
Request: EventRequestConstructor<Message, Args>,
args?: CheckForUnion<Args, never, Args>
args?: CheckForUnion<Args, never, Args>,
attempt?: number
): Promise<WebSocketEvent<Message>>
waitFor<Message>(
Response: EventResponseConstructor<Message>,
Expand Down
19 changes: 15 additions & 4 deletions packages/iqoption/lib/websocket/WebSocketClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class WebSocketClient implements BaseWebSocketClient {

public history: WebSocketEventHistory[]

constructor() {
constructor(public refreshLogIn: () => Promise<void>) {
this.subscribers = [new HeartbeatSubscriber(this)]

this.history = []
Expand Down Expand Up @@ -81,17 +81,26 @@ export class WebSocketClient implements BaseWebSocketClient {

public async send<Message, Args = undefined>(
Request: EventRequestConstructor<Message, Args>,
args?: CheckForUnion<Args, never, Args>
args?: CheckForUnion<Args, never, Args>,
attempts = 0
): Promise<WebSocketEvent<Message>> {
const request = new Request()

if (attempts > 0) {
console.log('Retrying request', request.name, attempts)
}

if (attempts > 5) {
throw new Error('Max attempts reached')
}

while (
this.webSocket.readyState !== WebSocket.OPEN &&
this.webSocket.readyState !== WebSocket.CONNECTING
) {
console.log('Waiting socket to connect to send message...')

await this.subscribe()
await this.refreshLogIn()

await sleep(100)
}
Expand All @@ -117,7 +126,9 @@ export class WebSocketClient implements BaseWebSocketClient {
} catch (err) {
console.error(err)

throw err
await this.refreshLogIn()

return this.send(Request, args, attempts + 1)
}

return event
Expand Down

0 comments on commit 10e1964

Please sign in to comment.