Skip to content

Commit

Permalink
feat: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Jul 12, 2018
1 parent 040ef0c commit 957c750
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 30 deletions.
40 changes: 10 additions & 30 deletions src/alice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fetch from 'node-fetch'

import Scene from './scene'
import Ctx from './ctx'
import ImagesApi from './imagesApi'

import {
selectCommand,
Expand Down Expand Up @@ -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,
}
Expand All @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down
62 changes: 62 additions & 0 deletions src/imagesApi.ts
Original file line number Diff line number Diff line change
@@ -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')
}
}
}

0 comments on commit 957c750

Please sign in to comment.