fixes issue with action

This commit is contained in:
Keagan McClelland
2021-03-01 17:02:47 -07:00
committed by Aiden McClelland
parent 2189c5643d
commit 4fcdf5f832

View File

@@ -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<true> {
testConnection(url: string): Promise<true> {
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<Unit> {
async getCheckAuth(): Promise<Unit> {
return this.http.serverRequest<Unit>({ method: Method.GET, url: '/authenticate' }, { version: '' })
}
async postLogin (password: string): Promise<Unit> {
async postLogin(password: string): Promise<Unit> {
return this.http.serverRequest<Unit>({ method: Method.POST, url: '/auth/login', data: { password } }, { version: '' })
}
async postLogout (): Promise<Unit> {
return this.http.serverRequest<Unit>({ method: Method.POST, url: '/auth/logout' }, { version: '' }).then(() => { this.authenticatedRequestsEnabled = false; return { } })
async postLogout(): Promise<Unit> {
return this.http.serverRequest<Unit>({ method: Method.POST, url: '/auth/logout' }, { version: '' }).then(() => { this.authenticatedRequestsEnabled = false; return {} })
}
async getServer (timeout?: number): Promise<ApiServer> {
async getServer(timeout?: number): Promise<ApiServer> {
return this.authRequest<ReqRes.GetServerRes>({ method: Method.GET, url: '/', readTimeout: timeout })
}
async acknowledgeOSWelcome (version: string): Promise<Unit> {
async acknowledgeOSWelcome(version: string): Promise<Unit> {
return this.authRequest<Unit>({ method: Method.POST, url: `/welcome/${version}` })
}
async getVersionLatest (): Promise<ReqRes.GetVersionLatestRes> {
async getVersionLatest(): Promise<ReqRes.GetVersionLatestRes> {
return this.authRequest<ReqRes.GetVersionLatestRes>({ method: Method.GET, url: '/versionLatest' }, { version: '' })
}
async getServerMetrics (): Promise<ReqRes.GetServerMetricsRes> {
async getServerMetrics(): Promise<ReqRes.GetServerMetricsRes> {
return this.authRequest<ReqRes.GetServerMetricsRes>({ method: Method.GET, url: `/metrics` })
}
async getNotifications (page: number, perPage: number): Promise<S9Notification[]> {
async getNotifications(page: number, perPage: number): Promise<S9Notification[]> {
const params: ReqRes.GetNotificationsReq = {
page: String(page),
perPage: String(perPage),
@@ -65,37 +65,37 @@ export class LiveApiService extends ApiService {
return this.authRequest<ReqRes.GetNotificationsRes>({ method: Method.GET, url: `/notifications`, params })
}
async deleteNotification (id: string): Promise<Unit> {
async deleteNotification(id: string): Promise<Unit> {
return this.authRequest({ method: Method.DELETE, url: `/notifications/${id}` })
}
async getExternalDisks (): Promise<DiskInfo[]> {
async getExternalDisks(): Promise<DiskInfo[]> {
return this.authRequest<ReqRes.GetExternalDisksRes>({ method: Method.GET, url: `/disks` })
}
// TODO: EJECT-DISKS
async ejectExternalDisk (logicalName: string): Promise<Unit> {
async ejectExternalDisk(logicalName: string): Promise<Unit> {
return this.authRequest({ method: Method.POST, url: `/disks/eject`, data: { logicalName } })
}
async updateAgent (version: string): Promise<Unit> {
async updateAgent(version: string): Promise<Unit> {
const data: ReqRes.PostUpdateAgentReq = {
version: `=${version}`,
}
return this.authRequest({ method: Method.POST, url: '/update', data })
}
async getAvailableAppVersionSpecificInfo (appId: string, versionSpec: string): Promise<AppAvailableVersionSpecificInfo> {
async getAvailableAppVersionSpecificInfo(appId: string, versionSpec: string): Promise<AppAvailableVersionSpecificInfo> {
return this
.authRequest<Replace<ReqRes.GetAppAvailableVersionInfoRes, 'versionViewing', 'version'>>( { method: Method.GET, url: `/apps/${appId}/store/${versionSpec}` })
.then( res => ({ ...res, versionViewing: res.version }))
.then( res => {
.authRequest<Replace<ReqRes.GetAppAvailableVersionInfoRes, 'versionViewing', 'version'>>({ method: Method.GET, url: `/apps/${appId}/store/${versionSpec}` })
.then(res => ({ ...res, versionViewing: res.version }))
.then(res => {
delete res['version']
return res
})
})
}
async getAvailableApps (): Promise<AppAvailablePreview[]> {
async getAvailableApps(): Promise<AppAvailablePreview[]> {
const res = await this.authRequest<ReqRes.GetAppsAvailableRes>({ 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<AppAvailableFull> {
async getAvailableApp(appId: string): Promise<AppAvailableFull> {
return this.authRequest<ReqRes.GetAppAvailableRes>({ method: Method.GET, url: `/apps/${appId}/store` })
.then(res => {
return {
@@ -114,33 +114,33 @@ export class LiveApiService extends ApiService {
})
}
async getInstalledApp (appId: string): Promise<AppInstalledFull> {
async getInstalledApp(appId: string): Promise<AppInstalledFull> {
return this.authRequest<ReqRes.GetAppInstalledRes>({ method: Method.GET, url: `/apps/${appId}/installed` })
.then(app => ({ ...app, hasFetchedFull: true }))
}
async getInstalledApps (): Promise<AppInstalledPreview[]> {
async getInstalledApps(): Promise<AppInstalledPreview[]> {
return this.authRequest<ReqRes.GetAppsInstalledRes>({ method: Method.GET, url: `/apps/installed` })
}
async getAppConfig ( appId: string): Promise<ReqRes.GetAppConfigRes> {
async getAppConfig(appId: string): Promise<ReqRes.GetAppConfigRes> {
return this.authRequest<ReqRes.GetAppConfigRes>({ method: Method.GET, url: `/apps/${appId}/config` })
}
async getAppLogs (appId: string, params: ReqRes.GetAppLogsReq = { }): Promise<string[]> {
return this.authRequest<ReqRes.GetAppLogsRes>( { method: Method.GET, url: `/apps/${appId}/logs`, params: params as any })
async getAppLogs(appId: string, params: ReqRes.GetAppLogsReq = {}): Promise<string[]> {
return this.authRequest<ReqRes.GetAppLogsRes>({ method: Method.GET, url: `/apps/${appId}/logs`, params: params as any })
}
async getServerLogs (): Promise<string> {
return this.authRequest<ReqRes.GetServerLogsRes>( { method: Method.GET, url: `/logs` })
async getServerLogs(): Promise<string> {
return this.authRequest<ReqRes.GetServerLogsRes>({ method: Method.GET, url: `/logs` })
}
async getAppMetrics (appId: string): Promise<AppMetrics> {
return this.authRequest<ReqRes.GetAppMetricsRes | string>( { method: Method.GET, url: `/apps/${appId}/metrics` })
async getAppMetrics(appId: string): Promise<AppMetrics> {
return this.authRequest<ReqRes.GetAppMetricsRes | string>({ method: Method.GET, url: `/apps/${appId}/metrics` })
.then(parseMetricsPermissive)
}
async installApp (appId: string, version: string, dryRun: boolean = false): Promise<AppInstalledFull & { breakages: DependentBreakage[] }> {
async installApp(appId: string, version: string, dryRun: boolean = false): Promise<AppInstalledFull & { breakages: DependentBreakage[] }> {
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<Unit> {
async startApp(appId: string): Promise<Unit> {
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<Unit> {
async restartApp(appId: string): Promise<Unit> {
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<Unit> {
async createAppBackup(appId: string, logicalname: string, password?: string): Promise<Unit> {
const data: ReqRes.PostAppBackupCreateReq = {
password: password || undefined,
logicalname,
}
return this.authRequest<ReqRes.PostAppBackupCreateRes>({ 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<Unit> {
async stopAppBackup(appId: string): Promise<Unit> {
return this.authRequest<ReqRes.PostAppBackupStopRes>({ 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<Unit> {
async restoreAppBackup(appId: string, logicalname: string, password?: string): Promise<Unit> {
const data: ReqRes.PostAppBackupRestoreReq = {
password: password || undefined,
logicalname,
}
return this.authRequest<ReqRes.PostAppBackupRestoreRes>({ 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<Unit> {
async patchServerConfig(attr: string, value: any): Promise<Unit> {
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<Unit> {
async wipeAppData(app: AppInstalledPreview): Promise<Unit> {
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<Unit> {
async toggleAppLAN(appId: string, toggle: 'enable' | 'disable'): Promise<Unit> {
return this.authRequest({ method: Method.POST, url: `/apps/${appId}/lan/${toggle}` })
}
async addSSHKey (sshKey: string): Promise<Unit> {
async addSSHKey(sshKey: string): Promise<Unit> {
const data: ReqRes.PostAddSSHKeyReq = {
sshKey,
}
const fingerprint = await this.authRequest<ReqRes.PostAddSSHKeyRes>({ 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<Unit> {
async addWifi(ssid: string, password: string, country: string, connect: boolean): Promise<Unit> {
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<Unit> {
async connectWifi(ssid: string): Promise<Unit> {
return this.authRequest({ method: Method.POST, url: encodeURI(`/wifi/${ssid}`) })
}
async deleteWifi (ssid: string): Promise<Unit> {
async deleteWifi(ssid: string): Promise<Unit> {
return this.authRequest({ method: Method.DELETE, url: encodeURI(`/wifi/${ssid}`) })
}
async deleteSSHKey (fingerprint: SSHFingerprint): Promise<Unit> {
async deleteSSHKey(fingerprint: SSHFingerprint): Promise<Unit> {
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<Unit> {
async restartServer(): Promise<Unit> {
return this.authRequest({ method: Method.POST, url: '/restart', readTimeout: 60000 })
}
async shutdownServer (): Promise<Unit> {
async shutdownServer(): Promise<Unit> {
return this.authRequest({ method: Method.POST, url: '/shutdown', readTimeout: 60000 })
}
async serviceAction (appId: string, s: ServiceAction): Promise<ReqRes.ServiceActionResponse> {
async serviceAction(appId: string, s: ServiceAction): Promise<ReqRes.ServiceActionResponse> {
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<Unit> {
async refreshLAN(): Promise<Unit> {
return this.authRequest({ method: Method.POST, url: '/network/lan/reset' })
}
private async authRequest<T> (opts: HttpOptions, overrides: Partial<{ version: string }> = { }): Promise<T> {
private async authRequest<T>(opts: HttpOptions, overrides: Partial<{ version: string }> = {}): Promise<T> {
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<true> {
function catchHttpStatusError(error: HttpErrorResponse): Observable<true> {
if (error.error instanceof ErrorEvent) {
// A client-side or network error occurred. Handle it accordingly.
return throwError('Not Connected')