Skip to content

Commit

Permalink
Move retry to app
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierZal committed Nov 22, 2023
1 parent 9f54e2e commit be78d31
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
17 changes: 16 additions & 1 deletion app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { App } from 'homey' // eslint-disable-line import/no-extraneous-dependen
import axios from 'axios'
import { wrapper } from 'axios-cookiejar-support'
import { CookieJar } from 'tough-cookie'
import { DateTime } from 'luxon'
import { DateTime, Duration } from 'luxon'
import withAPI from './mixins/withAPI'
import {
loginURL,
Expand All @@ -20,6 +20,10 @@ axios.defaults.jar = new CookieJar()
axios.defaults.baseURL = `https://${domain}`

export = class AristonApp extends withAPI(App) {
public retry = true

readonly #retryTimeout!: NodeJS.Timeout

#loginTimeout!: NodeJS.Timeout

public async onInit(): Promise<void> {
Expand Down Expand Up @@ -70,6 +74,17 @@ export = class AristonApp extends withAPI(App) {
}
}

public handleRetry(): void {
this.retry = false
this.homey.clearTimeout(this.#retryTimeout)
this.homey.setTimeout(
() => {
this.retry = true
},
Duration.fromObject({ minutes: 1 }).as('milliseconds'),
)
}

private refreshLogin(): void {
const expires: string =
(this.homey.settings.get('expires') as HomeySettings['expires']) ?? ''
Expand Down
19 changes: 4 additions & 15 deletions mixins/withAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import axios, {
type AxiosResponse,
type InternalAxiosRequestConfig,
} from 'axios'
import { Duration } from 'luxon'
import type AristonApp from '../app'
import { loginURL, type HomeyClass } from '../types'

Expand All @@ -24,10 +23,6 @@ export default function withAPI<T extends HomeyClass>(base: T): APIClass & T {
return class extends base {
public api: AxiosInstance = axios.create()

#retry = true

readonly #retryTimeout!: NodeJS.Timeout

public constructor(...args: any[]) {
super(...args)
this.setupAxiosInterceptors()
Expand Down Expand Up @@ -65,20 +60,14 @@ export default function withAPI<T extends HomeyClass>(base: T): APIClass & T {
const { config } = response
this.log('Received response:', config.url, response.data)
const contentType = String(response.headers['content-type'])
const app: AristonApp = this.homey.app as AristonApp
if (
contentType.includes('text/html') &&
this.#retry &&
app.retry &&
config.url !== loginURL
) {
this.#retry = false
this.homey.clearTimeout(this.#retryTimeout)
this.homey.setTimeout(
() => {
this.#retry = true
},
Duration.fromObject({ minutes: 1 }).as('milliseconds'),
)
const loggedIn: boolean = await (this.homey.app as AristonApp).login()
app.handleRetry()
const loggedIn: boolean = await app.login()
if (loggedIn) {
return this.api.request(config)
}
Expand Down

0 comments on commit be78d31

Please sign in to comment.