From 957c750f34382d4dc5e1e1d27ca47053f7ebeee8 Mon Sep 17 00:00:00 2001 From: fletcherist Date: Thu, 12 Jul 2018 23:19:01 +0300 Subject: [PATCH] feat: refactoring --- src/alice.ts | 40 ++++++++----------------------- src/imagesApi.ts | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 30 deletions(-) create mode 100644 src/imagesApi.ts diff --git a/src/alice.ts b/src/alice.ts index cfa5265..8b36807 100644 --- a/src/alice.ts +++ b/src/alice.ts @@ -6,6 +6,7 @@ import fetch from 'node-fetch' import Scene from './scene' import Ctx from './ctx' +import ImagesApi from './imagesApi' import { selectCommand, @@ -35,6 +36,7 @@ export default class Alice { private scenes: Scene[] private currentScene: Scene | null private sessions: Sessions + private imagesApi: ImagesApi private server: { close: () => void, } @@ -49,6 +51,10 @@ export default class Alice { this.currentScene = null this.sessions = new Sessions() this.config = config + this.imagesApi = new ImagesApi({ + oAuthToken: this.config.oAuthToken, + skillId: this.config.skillId, + }) this._handleEnterScene = this._handleEnterScene.bind(this) this._handleLeaveScene = this._handleLeaveScene.bind(this) @@ -277,38 +283,12 @@ export default class Alice { } } - public uploadImage(imageUrl: string) { - if (!this.config.skillId) { - throw new Error('Please, provide {skillId} to alice constructor') - } - if (!this.config.oAuthToken) { - throw new Error('Please, provide {oAuthToken} to alice constructor') - } - fetch(`${ALICE_API_URL}/${this.config.skillId}/images`, { - method: 'POST', - headers: { - 'Authorization': `OAuth ${this.config.oAuthToken}`, - 'Content-type': 'application/json', - }, - body: JSON.stringify({ - url: imageUrl, - }), - }) - .then((res) => res.text()) - .then(console.log) - .catch((error) => console.error(error)) + public async uploadImage(imageUrl: string) { + return await this.imagesApi.uploadImage(imageUrl) } - public getImages() { - fetch(`${ALICE_API_URL}/${this.config.skillId}/images`, { - method: 'GET', - headers: { - 'Authorization': `OAuth ${this.config.oAuthToken}`, - 'Content-type': 'application/json', - }, - }).then((res) => res.text()) - .then(console.log) - .catch((error) => console.log(error)) + public async getImages() { + return await this.imagesApi.getImages() } public stopListening() { diff --git a/src/imagesApi.ts b/src/imagesApi.ts new file mode 100644 index 0000000..a178ac4 --- /dev/null +++ b/src/imagesApi.ts @@ -0,0 +1,62 @@ +import { ALICE_API_URL } from './constants' + +export function getHeaders({ + oAuthToken, +}) { + return { + 'Authorization': `OAuth ${oAuthToken}`, + 'Content-type': 'application/json', + } +} + +export default class ImagesApi { + public skillId: string + public oAuthToken: string + constructor({ + oAuthToken, + skillId, + }) { + this.oAuthToken = oAuthToken + this.skillId = skillId + } + + public async uploadImage(imageUrl: string) { + try { + this.checkProps() + const res = await fetch(`${ALICE_API_URL}/${this.skillId}/images`, { + method: 'POST', + headers: getHeaders({ oAuthToken: this.oAuthToken }), + body: JSON.stringify({ + url: imageUrl, + }), + }) + const json = await res.json() + return json + } catch (error) { + return error + } + } + + public async getImages() { + this.checkProps() + try { + const res = await fetch(`${ALICE_API_URL}/${this.skillId}/images`, { + method: 'GET', + headers: getHeaders({ oAuthToken: this.oAuthToken }), + }) + const json = await res.json() + return json + } catch (error) { + return error + } + } + + private checkProps() { + if (!this.skillId) { + throw new Error('Please, provide {skillId} to alice constructor') + } + if (!this.oAuthToken) { + throw new Error('Please, provide {oAuthToken} to alice constructor') + } + } +}