-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshakr-api.js
114 lines (93 loc) · 3 KB
/
shakr-api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const fetch = require('node-fetch');
const SHAKR_API_BASE_URL = 'https://api.shakr.com';
module.exports = class ShakrAPI {
constructor({ client_id, client_secret }) {
this.client_id = client_id;
this.client_secret = client_secret;
this.access_token = null;
}
get access_token_header() {
return this.access_token ?
{ 'Authorization': `Bearer ${this.access_token}` } :
{};
}
async authorize() {
if (this.access_token) {
return;
}
const res = await this._request({
url: '/oauth/token',
method: 'POST',
body: {
client_id: this.client_id,
client_secret: this.client_secret,
grant_type: 'client_credentials'
}
});
this.access_token = res.access_token;
}
async getRenderSession(render_session_id) {
await this.authorize();
const res = await this._request({
url: `/v2/render_sessions/${render_session_id}`,
method: 'GET'
});
return res.render_session;
}
async createRenderSession(template_style_version_id, { resources, audio_tracks, font_changes }) {
await this.authorize();
const res = await this._request({
url: '/v2/render_sessions',
method: 'POST',
body: {
title: 'TESTING',
render: 'preview',
mapping: {
template_style_version_id,
resources,
audio_tracks,
font_changes
}
}
});
return res.render_session;
}
async generateAspectRatioVariation(base_render_session_id, aspect_ratio_variation_id) {
const { mapping } = await this.getRenderSession(base_render_session_id);
const aspect_ratio_variation_render = await this.createRenderSession(aspect_ratio_variation_id, mapping);
return aspect_ratio_variation_render;
}
async createEditToken(template_style_version_id, resources) {
await this.authorize();
const res = await this._request({
url: '/v2/render_sessions',
method: 'POST',
body: {
title: 'TESTING',
mapping: {
template_style_version_id,
resources
},
edit_token: true
}
});
return {
render_session_id: res.render_session.id,
edit_token: res.edit_token
};
}
async _request({ url, method, body }) {
const res = await fetch(
`${SHAKR_API_BASE_URL}${url}`,
{
method,
headers: {
'Content-Type': 'application/json',
...this.access_token_header
},
body: JSON.stringify(body)
}
);
return await res.json();
}
}