From 4fcdf5f83207107afcdb4adc3312a569952be365 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Mon, 1 Mar 2021 17:02:47 -0700 Subject: [PATCH] fixes issue with action --- ui/src/app/services/api/live-api.service.ts | 130 ++++++++++---------- 1 file changed, 65 insertions(+), 65 deletions(-) diff --git a/ui/src/app/services/api/live-api.service.ts b/ui/src/app/services/api/live-api.service.ts index 4716afa71..c6832e50e 100644 --- a/ui/src/app/services/api/live-api.service.ts +++ b/ui/src/app/services/api/live-api.service.ts @@ -3,7 +3,7 @@ import { HttpService, Method, HttpOptions } from '../http.service' import { AppModel, AppStatus } from '../../models/app-model' import { AppAvailablePreview, AppAvailableFull, AppInstalledFull, AppInstalledPreview, DependentBreakage, AppAvailableVersionSpecificInfo, ServiceAction } from '../../models/app-types' import { S9Notification, SSHFingerprint, ServerModel, DiskInfo } from '../../models/server-model' -import { ApiService, ReqRes } from './api.service' +import { ApiService, ReqRes } from './api.service' import { ApiServer, Unit } from './api-types' import { HttpClient, HttpErrorResponse } from '@angular/common/http' import { isUnauthorized } from 'src/app/util/web.util' @@ -17,47 +17,47 @@ import { METHODS } from 'http' @Injectable() export class LiveApiService extends ApiService { - constructor ( + constructor( private readonly http: HttpService, // TODO remove app + server model from here. updates to state should be done in a separate class wrapping ApiService + App/ServerModel private readonly appModel: AppModel, private readonly serverModel: ServerModel, ) { super() } - testConnection (url: string): Promise { + testConnection(url: string): Promise { return this.http.raw.get(url).pipe(mapTo(true as true), catchError(e => catchHttpStatusError(e))).toPromise() } // Used to check whether password or key is valid. If so, it will be used implicitly by all other calls. - async getCheckAuth (): Promise { + async getCheckAuth(): Promise { return this.http.serverRequest({ method: Method.GET, url: '/authenticate' }, { version: '' }) } - async postLogin (password: string): Promise { + async postLogin(password: string): Promise { return this.http.serverRequest({ method: Method.POST, url: '/auth/login', data: { password } }, { version: '' }) } - async postLogout (): Promise { - return this.http.serverRequest({ method: Method.POST, url: '/auth/logout' }, { version: '' }).then(() => { this.authenticatedRequestsEnabled = false; return { } }) + async postLogout(): Promise { + return this.http.serverRequest({ method: Method.POST, url: '/auth/logout' }, { version: '' }).then(() => { this.authenticatedRequestsEnabled = false; return {} }) } - async getServer (timeout?: number): Promise { + async getServer(timeout?: number): Promise { return this.authRequest({ method: Method.GET, url: '/', readTimeout: timeout }) } - async acknowledgeOSWelcome (version: string): Promise { + async acknowledgeOSWelcome(version: string): Promise { return this.authRequest({ method: Method.POST, url: `/welcome/${version}` }) } - async getVersionLatest (): Promise { + async getVersionLatest(): Promise { return this.authRequest({ method: Method.GET, url: '/versionLatest' }, { version: '' }) } - async getServerMetrics (): Promise { + async getServerMetrics(): Promise { return this.authRequest({ method: Method.GET, url: `/metrics` }) } - async getNotifications (page: number, perPage: number): Promise { + async getNotifications(page: number, perPage: number): Promise { const params: ReqRes.GetNotificationsReq = { page: String(page), perPage: String(perPage), @@ -65,37 +65,37 @@ export class LiveApiService extends ApiService { return this.authRequest({ method: Method.GET, url: `/notifications`, params }) } - async deleteNotification (id: string): Promise { + async deleteNotification(id: string): Promise { return this.authRequest({ method: Method.DELETE, url: `/notifications/${id}` }) } - async getExternalDisks (): Promise { + async getExternalDisks(): Promise { return this.authRequest({ method: Method.GET, url: `/disks` }) } // TODO: EJECT-DISKS - async ejectExternalDisk (logicalName: string): Promise { + async ejectExternalDisk(logicalName: string): Promise { return this.authRequest({ method: Method.POST, url: `/disks/eject`, data: { logicalName } }) } - async updateAgent (version: string): Promise { + async updateAgent(version: string): Promise { const data: ReqRes.PostUpdateAgentReq = { version: `=${version}`, } return this.authRequest({ method: Method.POST, url: '/update', data }) } - async getAvailableAppVersionSpecificInfo (appId: string, versionSpec: string): Promise { + async getAvailableAppVersionSpecificInfo(appId: string, versionSpec: string): Promise { return this - .authRequest>( { method: Method.GET, url: `/apps/${appId}/store/${versionSpec}` }) - .then( res => ({ ...res, versionViewing: res.version })) - .then( res => { + .authRequest>({ method: Method.GET, url: `/apps/${appId}/store/${versionSpec}` }) + .then(res => ({ ...res, versionViewing: res.version })) + .then(res => { delete res['version'] return res - }) + }) } - async getAvailableApps (): Promise { + async getAvailableApps(): Promise { const res = await this.authRequest({ method: Method.GET, url: '/apps/store' }) return res.map(a => { const latestVersionTimestamp = new Date(a.latestVersionTimestamp) @@ -104,7 +104,7 @@ export class LiveApiService extends ApiService { }) } - async getAvailableApp (appId: string): Promise { + async getAvailableApp(appId: string): Promise { return this.authRequest({ method: Method.GET, url: `/apps/${appId}/store` }) .then(res => { return { @@ -114,33 +114,33 @@ export class LiveApiService extends ApiService { }) } - async getInstalledApp (appId: string): Promise { + async getInstalledApp(appId: string): Promise { return this.authRequest({ method: Method.GET, url: `/apps/${appId}/installed` }) .then(app => ({ ...app, hasFetchedFull: true })) } - async getInstalledApps (): Promise { + async getInstalledApps(): Promise { return this.authRequest({ method: Method.GET, url: `/apps/installed` }) } - async getAppConfig ( appId: string): Promise { + async getAppConfig(appId: string): Promise { return this.authRequest({ method: Method.GET, url: `/apps/${appId}/config` }) } - async getAppLogs (appId: string, params: ReqRes.GetAppLogsReq = { }): Promise { - return this.authRequest( { method: Method.GET, url: `/apps/${appId}/logs`, params: params as any }) + async getAppLogs(appId: string, params: ReqRes.GetAppLogsReq = {}): Promise { + return this.authRequest({ method: Method.GET, url: `/apps/${appId}/logs`, params: params as any }) } - async getServerLogs (): Promise { - return this.authRequest( { method: Method.GET, url: `/logs` }) + async getServerLogs(): Promise { + return this.authRequest({ method: Method.GET, url: `/logs` }) } - async getAppMetrics (appId: string): Promise { - return this.authRequest( { method: Method.GET, url: `/apps/${appId}/metrics` }) + async getAppMetrics(appId: string): Promise { + return this.authRequest({ method: Method.GET, url: `/apps/${appId}/metrics` }) .then(parseMetricsPermissive) } - async installApp (appId: string, version: string, dryRun: boolean = false): Promise { + async installApp(appId: string, version: string, dryRun: boolean = false): Promise { const data: ReqRes.PostInstallAppReq = { version, } @@ -148,94 +148,94 @@ export class LiveApiService extends ApiService { .then(res => ({ ...res, hasFetchedFull: false })) } - async uninstallApp (appId: string, dryRun: boolean = false): Promise<{ breakages: DependentBreakage[] }> { + async uninstallApp(appId: string, dryRun: boolean = false): Promise<{ breakages: DependentBreakage[] }> { return this.authRequest({ method: Method.POST, url: `/apps/${appId}/uninstall${dryRunParam(dryRun, true)}`, readTimeout: 60000 }) } - async startApp (appId: string): Promise { + async startApp(appId: string): Promise { return this.authRequest({ method: Method.POST, url: `/apps/${appId}/start`, readTimeout: 60000 }) .then(() => this.appModel.update({ id: appId, status: AppStatus.RUNNING })) - .then(() => ({ })) + .then(() => ({})) } - async stopApp (appId: string, dryRun: boolean = false): Promise<{ breakages: DependentBreakage[] }> { + async stopApp(appId: string, dryRun: boolean = false): Promise<{ breakages: DependentBreakage[] }> { const res = await this.authRequest<{ breakages: DependentBreakage[] }>({ method: Method.POST, url: `/apps/${appId}/stop${dryRunParam(dryRun, true)}`, readTimeout: 60000 }) if (!dryRun) this.appModel.update({ id: appId, status: AppStatus.STOPPING }, modulateTime(new Date(), 5, 'seconds')) return res } - async restartApp (appId: string): Promise { + async restartApp(appId: string): Promise { return this.authRequest({ method: Method.POST, url: `/apps/${appId}/restart`, readTimeout: 60000 }) - .then(() => ({ } as any)) + .then(() => ({} as any)) } - async createAppBackup (appId: string, logicalname: string, password?: string): Promise { + async createAppBackup(appId: string, logicalname: string, password?: string): Promise { const data: ReqRes.PostAppBackupCreateReq = { password: password || undefined, logicalname, } return this.authRequest({ method: Method.POST, url: `/apps/${appId}/backup`, data, readTimeout: 60000 }) .then(() => this.appModel.update({ id: appId, status: AppStatus.CREATING_BACKUP })) - .then(() => ({ })) + .then(() => ({})) } - async stopAppBackup (appId: string): Promise { + async stopAppBackup(appId: string): Promise { return this.authRequest({ method: Method.POST, url: `/apps/${appId}/backup/stop`, readTimeout: 60000 }) .then(() => this.appModel.update({ id: appId, status: AppStatus.STOPPED })) - .then(() => ({ })) + .then(() => ({})) } - async restoreAppBackup (appId: string, logicalname: string, password?: string): Promise { + async restoreAppBackup(appId: string, logicalname: string, password?: string): Promise { const data: ReqRes.PostAppBackupRestoreReq = { password: password || undefined, logicalname, } return this.authRequest({ method: Method.POST, url: `/apps/${appId}/backup/restore`, data, readTimeout: 60000 }) .then(() => this.appModel.update({ id: appId, status: AppStatus.RESTORING_BACKUP })) - .then(() => ({ })) + .then(() => ({})) } - async patchAppConfig (app: AppInstalledPreview, config: object, dryRun = false): Promise<{ breakages: DependentBreakage[] }> { + async patchAppConfig(app: AppInstalledPreview, config: object, dryRun = false): Promise<{ breakages: DependentBreakage[] }> { const data: ReqRes.PatchAppConfigReq = { config, } return this.authRequest({ method: Method.PATCH, url: `/apps/${app.id}/config${dryRunParam(dryRun, true)}`, data, readTimeout: 60000 }) } - async postConfigureDependency (dependencyId: string, dependentId: string, dryRun?: boolean): Promise<{ config: object, breakages: DependentBreakage[] }> { + async postConfigureDependency(dependencyId: string, dependentId: string, dryRun?: boolean): Promise<{ config: object, breakages: DependentBreakage[] }> { return this.authRequest({ method: Method.POST, url: `/apps/${dependencyId}/autoconfig/${dependentId}${dryRunParam(dryRun, true)}`, readTimeout: 60000 }) } - async patchServerConfig (attr: string, value: any): Promise { + async patchServerConfig(attr: string, value: any): Promise { const data: ReqRes.PatchServerConfigReq = { value, } return this.authRequest({ method: Method.PATCH, url: `/${attr}`, data, readTimeout: 60000 }) .then(() => this.serverModel.update({ [attr]: value })) - .then(() => ({ })) + .then(() => ({})) } - async wipeAppData (app: AppInstalledPreview): Promise { + async wipeAppData(app: AppInstalledPreview): Promise { return this.authRequest({ method: Method.POST, url: `/apps/${app.id}/wipe`, readTimeout: 60000 }).then((res) => { this.appModel.update({ id: app.id, status: AppStatus.NEEDS_CONFIG }) return res }) } - async toggleAppLAN (appId: string, toggle: 'enable' | 'disable'): Promise { + async toggleAppLAN(appId: string, toggle: 'enable' | 'disable'): Promise { return this.authRequest({ method: Method.POST, url: `/apps/${appId}/lan/${toggle}` }) } - async addSSHKey (sshKey: string): Promise { + async addSSHKey(sshKey: string): Promise { const data: ReqRes.PostAddSSHKeyReq = { sshKey, } const fingerprint = await this.authRequest({ method: Method.POST, url: `/sshKeys`, data }) this.serverModel.update({ ssh: [...this.serverModel.peek().ssh, fingerprint] }) - return { } + return {} } - async addWifi (ssid: string, password: string, country: string, connect: boolean): Promise { + async addWifi(ssid: string, password: string, country: string, connect: boolean): Promise { const data: ReqRes.PostAddWifiReq = { ssid, password, @@ -245,43 +245,43 @@ export class LiveApiService extends ApiService { return this.authRequest({ method: Method.POST, url: `/wifi`, data }) } - async connectWifi (ssid: string): Promise { + async connectWifi(ssid: string): Promise { return this.authRequest({ method: Method.POST, url: encodeURI(`/wifi/${ssid}`) }) } - async deleteWifi (ssid: string): Promise { + async deleteWifi(ssid: string): Promise { return this.authRequest({ method: Method.DELETE, url: encodeURI(`/wifi/${ssid}`) }) } - async deleteSSHKey (fingerprint: SSHFingerprint): Promise { + async deleteSSHKey(fingerprint: SSHFingerprint): Promise { await this.authRequest({ method: Method.DELETE, url: `/sshKeys/${fingerprint.hash}` }) const ssh = this.serverModel.peek().ssh this.serverModel.update({ ssh: ssh.filter(s => s !== fingerprint) }) - return { } + return {} } - async restartServer (): Promise { + async restartServer(): Promise { return this.authRequest({ method: Method.POST, url: '/restart', readTimeout: 60000 }) } - async shutdownServer (): Promise { + async shutdownServer(): Promise { return this.authRequest({ method: Method.POST, url: '/shutdown', readTimeout: 60000 }) } - async serviceAction (appId: string, s: ServiceAction): Promise { + async serviceAction(appId: string, s: ServiceAction): Promise { const data: ReqRes.ServiceActionRequest = { jsonrpc: '2.0', id: uuid.v4(), method: s.id, } - return this.authRequest({ method: Method.POST, url: `apps/${appId}/actions`, data }) + return this.authRequest({ method: Method.POST, url: `/apps/${appId}/actions`, data }) } - async refreshLAN (): Promise { + async refreshLAN(): Promise { return this.authRequest({ method: Method.POST, url: '/network/lan/reset' }) } - private async authRequest (opts: HttpOptions, overrides: Partial<{ version: string }> = { }): Promise { + private async authRequest(opts: HttpOptions, overrides: Partial<{ version: string }> = {}): Promise { if (!this.authenticatedRequestsEnabled) throw new Error(`Authenticated requests are not enabled. Do you need to login?`) opts.withCredentials = true @@ -300,7 +300,7 @@ const dryRunParam = (dryRun: boolean, first: boolean) => { return first ? `?dryrun` : `&dryrun` } -function catchHttpStatusError (error: HttpErrorResponse): Observable { +function catchHttpStatusError(error: HttpErrorResponse): Observable { if (error.error instanceof ErrorEvent) { // A client-side or network error occurred. Handle it accordingly. return throwError('Not Connected')