-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
fletcherist
committed
Jul 12, 2018
1 parent
040ef0c
commit 957c750
Showing
2 changed files
with
72 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} | ||
} | ||
} |