From ac9ac7ac69c9bf9211c19f2d3840294bb761c369 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sun, 29 Nov 2020 15:34:52 +0100 Subject: [PATCH] add new api internal class. --- src/component/internal/api.ts | 30 +++++++++++++++++++++++++++++ src/component/internal/websocket.ts | 3 +-- src/component/main.vue | 9 ++++++++- 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 src/component/internal/api.ts diff --git a/src/component/internal/api.ts b/src/component/internal/api.ts new file mode 100644 index 00000000..159058a8 --- /dev/null +++ b/src/component/internal/api.ts @@ -0,0 +1,30 @@ +import * as Api from '../api' + +export class NekoApi { + api_configuration = new Api.Configuration() + + public connect(url: string, id: string, secret: string) { + this.api_configuration = new Api.Configuration({ + basePath: url, + headers: { + Authorization: 'Basic ' + btoa(id + ':' + secret), + }, + }) + } + + public disconnect() { + this.api_configuration = new Api.Configuration() + } + + get admin(): Api.AdminsApi { + return new Api.AdminsApi(this.api_configuration) + } + + get user(): Api.UsersApi { + return new Api.UsersApi(this.api_configuration) + } + + get host(): Api.HostsApi { + return new Api.HostsApi(this.api_configuration) + } +} diff --git a/src/component/internal/websocket.ts b/src/component/internal/websocket.ts index 89f05587..f43e30a4 100644 --- a/src/component/internal/websocket.ts +++ b/src/component/internal/websocket.ts @@ -32,8 +32,7 @@ export class NekoWebSocket extends EventEmitter { this.emit('connecting') - const ws_url = url.replace(/^http/, 'ws').replace(/\/$|\/ws$/, '') - this._ws = new WebSocket(`${ws_url}/ws?id=${encodeURIComponent(id)}&secret=${encodeURIComponent(secret)}`) + this._ws = new WebSocket(`${url}/ws?id=${encodeURIComponent(id)}&secret=${encodeURIComponent(secret)}`) this._log.debug(`connecting to ${this._ws.url}`) this._ws.onopen = this.onConnected.bind(this) diff --git a/src/component/main.vue b/src/component/main.vue index 8e979eaa..057d0636 100644 --- a/src/component/main.vue +++ b/src/component/main.vue @@ -44,6 +44,7 @@ import ResizeObserver from 'resize-observer-polyfill' import EventEmitter from 'eventemitter3' + import { NekoApi } from './internal/api' import { NekoWebSocket } from './internal/websocket' import { NekoWebRTC } from './internal/webrtc' import { NekoMessages } from './internal/messages' @@ -63,6 +64,7 @@ @Ref('container') readonly _container!: HTMLElement @Ref('video') readonly _video!: HTMLVideoElement + api = new NekoApi() websocket = new NekoWebSocket() webrtc = new NekoWebRTC() observer = new ResizeObserver(this.onResize.bind(this)) @@ -138,7 +140,11 @@ throw new Error('client already connected') } - this.websocket.connect(url, id, secret) + const wsURL = url.replace(/^http/, 'ws').replace(/\/$|\/ws\/?$/, '') + this.websocket.connect(wsURL, id, secret) + + const httpURL = url.replace(/^ws/, 'http').replace(/\/$|\/ws\/?$/, '') + this.api.connect(httpURL, id, secret) } public disconnect() { @@ -147,6 +153,7 @@ } this.websocket.disconnect() + this.api.disconnect() } public play() {