diff options
Diffstat (limited to 'src/api/server/ServerApi.js')
-rw-r--r-- | src/api/server/ServerApi.js | 83 |
1 files changed, 49 insertions, 34 deletions
diff --git a/src/api/server/ServerApi.js b/src/api/server/ServerApi.js index 1f538368d..a5d636b4e 100644 --- a/src/api/server/ServerApi.js +++ b/src/api/server/ServerApi.js | |||
@@ -15,6 +15,8 @@ import OrderModel from '../../models/Order'; | |||
15 | import { sleep } from '../../helpers/async-helpers'; | 15 | import { sleep } from '../../helpers/async-helpers'; |
16 | 16 | ||
17 | import { API } from '../../environment'; | 17 | import { API } from '../../environment'; |
18 | import { RECIPES_PATH } from '../../config'; | ||
19 | import apiBase from '../apiBase'; | ||
18 | import { prepareAuthRequest, sendAuthRequest } from '../utils/auth'; | 20 | import { prepareAuthRequest, sendAuthRequest } from '../utils/auth'; |
19 | 21 | ||
20 | import { | 22 | import { |
@@ -27,7 +29,7 @@ import { | |||
27 | removeServicePartitionDirectory, | 29 | removeServicePartitionDirectory, |
28 | } from '../../helpers/service-helpers.js'; | 30 | } from '../../helpers/service-helpers.js'; |
29 | 31 | ||
30 | const debug = require('debug')('Franz:ServerApi'); | 32 | const debug = require('debug')('Ferdi:ServerApi'); |
31 | 33 | ||
32 | module.paths.unshift( | 34 | module.paths.unshift( |
33 | getDevRecipeDirectory(), | 35 | getDevRecipeDirectory(), |
@@ -38,8 +40,6 @@ const { app } = remote; | |||
38 | const { default: fetch } = remote.require('electron-fetch'); | 40 | const { default: fetch } = remote.require('electron-fetch'); |
39 | 41 | ||
40 | const SERVER_URL = API; | 42 | const SERVER_URL = API; |
41 | const API_VERSION = 'v1'; | ||
42 | const API_URL = `${SERVER_URL}/${API_VERSION}`; | ||
43 | 43 | ||
44 | export default class ServerApi { | 44 | export default class ServerApi { |
45 | recipePreviews = []; | 45 | recipePreviews = []; |
@@ -48,7 +48,7 @@ export default class ServerApi { | |||
48 | 48 | ||
49 | // User | 49 | // User |
50 | async login(email, passwordHash) { | 50 | async login(email, passwordHash) { |
51 | const request = await sendAuthRequest(`${API_URL}/auth/login`, { | 51 | const request = await sendAuthRequest(`${apiBase()}/auth/login`, { |
52 | method: 'POST', | 52 | method: 'POST', |
53 | headers: { | 53 | headers: { |
54 | Authorization: `Basic ${window.btoa(`${email}:${passwordHash}`)}`, | 54 | Authorization: `Basic ${window.btoa(`${email}:${passwordHash}`)}`, |
@@ -64,7 +64,7 @@ export default class ServerApi { | |||
64 | } | 64 | } |
65 | 65 | ||
66 | async signup(data) { | 66 | async signup(data) { |
67 | const request = await sendAuthRequest(`${API_URL}/auth/signup`, { | 67 | const request = await sendAuthRequest(`${apiBase()}/auth/signup`, { |
68 | method: 'POST', | 68 | method: 'POST', |
69 | body: JSON.stringify(data), | 69 | body: JSON.stringify(data), |
70 | }, false); | 70 | }, false); |
@@ -78,7 +78,7 @@ export default class ServerApi { | |||
78 | } | 78 | } |
79 | 79 | ||
80 | async activateTrial(data) { | 80 | async activateTrial(data) { |
81 | const request = await sendAuthRequest(`${API_URL}/payment/trial`, { | 81 | const request = await sendAuthRequest(`${apiBase()}/payment/trial`, { |
82 | method: 'POST', | 82 | method: 'POST', |
83 | body: JSON.stringify(data), | 83 | body: JSON.stringify(data), |
84 | }); | 84 | }); |
@@ -92,7 +92,7 @@ export default class ServerApi { | |||
92 | } | 92 | } |
93 | 93 | ||
94 | async inviteUser(data) { | 94 | async inviteUser(data) { |
95 | const request = await sendAuthRequest(`${API_URL}/invite`, { | 95 | const request = await sendAuthRequest(`${apiBase()}/invite`, { |
96 | method: 'POST', | 96 | method: 'POST', |
97 | body: JSON.stringify(data), | 97 | body: JSON.stringify(data), |
98 | }); | 98 | }); |
@@ -105,7 +105,7 @@ export default class ServerApi { | |||
105 | } | 105 | } |
106 | 106 | ||
107 | async retrievePassword(email) { | 107 | async retrievePassword(email) { |
108 | const request = await sendAuthRequest(`${API_URL}/auth/password`, { | 108 | const request = await sendAuthRequest(`${apiBase()}/auth/password`, { |
109 | method: 'POST', | 109 | method: 'POST', |
110 | body: JSON.stringify({ | 110 | body: JSON.stringify({ |
111 | email, | 111 | email, |
@@ -121,7 +121,7 @@ export default class ServerApi { | |||
121 | } | 121 | } |
122 | 122 | ||
123 | async userInfo() { | 123 | async userInfo() { |
124 | const request = await sendAuthRequest(`${API_URL}/me`); | 124 | const request = await sendAuthRequest(`${apiBase()}/me`); |
125 | if (!request.ok) { | 125 | if (!request.ok) { |
126 | throw request; | 126 | throw request; |
127 | } | 127 | } |
@@ -134,7 +134,7 @@ export default class ServerApi { | |||
134 | } | 134 | } |
135 | 135 | ||
136 | async updateUserInfo(data) { | 136 | async updateUserInfo(data) { |
137 | const request = await sendAuthRequest(`${API_URL}/me`, { | 137 | const request = await sendAuthRequest(`${apiBase()}/me`, { |
138 | method: 'PUT', | 138 | method: 'PUT', |
139 | body: JSON.stringify(data), | 139 | body: JSON.stringify(data), |
140 | }); | 140 | }); |
@@ -149,7 +149,7 @@ export default class ServerApi { | |||
149 | } | 149 | } |
150 | 150 | ||
151 | async deleteAccount() { | 151 | async deleteAccount() { |
152 | const request = await sendAuthRequest(`${API_URL}/me`, { | 152 | const request = await sendAuthRequest(`${apiBase()}/me`, { |
153 | method: 'DELETE', | 153 | method: 'DELETE', |
154 | }); | 154 | }); |
155 | if (!request.ok) { | 155 | if (!request.ok) { |
@@ -163,7 +163,7 @@ export default class ServerApi { | |||
163 | 163 | ||
164 | // Services | 164 | // Services |
165 | async getServices() { | 165 | async getServices() { |
166 | const request = await sendAuthRequest(`${API_URL}/me/services`); | 166 | const request = await sendAuthRequest(`${apiBase()}/me/services`); |
167 | if (!request.ok) { | 167 | if (!request.ok) { |
168 | throw request; | 168 | throw request; |
169 | } | 169 | } |
@@ -176,7 +176,7 @@ export default class ServerApi { | |||
176 | } | 176 | } |
177 | 177 | ||
178 | async createService(recipeId, data) { | 178 | async createService(recipeId, data) { |
179 | const request = await sendAuthRequest(`${API_URL}/service`, { | 179 | const request = await sendAuthRequest(`${apiBase()}/service`, { |
180 | method: 'POST', | 180 | method: 'POST', |
181 | body: JSON.stringify(Object.assign({ | 181 | body: JSON.stringify(Object.assign({ |
182 | recipeId, | 182 | recipeId, |
@@ -206,7 +206,7 @@ export default class ServerApi { | |||
206 | await this.uploadServiceIcon(serviceId, data.iconFile); | 206 | await this.uploadServiceIcon(serviceId, data.iconFile); |
207 | } | 207 | } |
208 | 208 | ||
209 | const request = await sendAuthRequest(`${API_URL}/service/${serviceId}`, { | 209 | const request = await sendAuthRequest(`${apiBase()}/service/${serviceId}`, { |
210 | method: 'PUT', | 210 | method: 'PUT', |
211 | body: JSON.stringify(data), | 211 | body: JSON.stringify(data), |
212 | }); | 212 | }); |
@@ -234,7 +234,7 @@ export default class ServerApi { | |||
234 | 234 | ||
235 | delete requestData.headers['Content-Type']; | 235 | delete requestData.headers['Content-Type']; |
236 | 236 | ||
237 | const request = await window.fetch(`${API_URL}/service/${serviceId}`, requestData); | 237 | const request = await window.fetch(`${apiBase()}/service/${serviceId}`, requestData); |
238 | 238 | ||
239 | if (!request.ok) { | 239 | if (!request.ok) { |
240 | throw request; | 240 | throw request; |
@@ -246,7 +246,7 @@ export default class ServerApi { | |||
246 | } | 246 | } |
247 | 247 | ||
248 | async reorderService(data) { | 248 | async reorderService(data) { |
249 | const request = await sendAuthRequest(`${API_URL}/service/reorder`, { | 249 | const request = await sendAuthRequest(`${apiBase()}/service/reorder`, { |
250 | method: 'PUT', | 250 | method: 'PUT', |
251 | body: JSON.stringify(data), | 251 | body: JSON.stringify(data), |
252 | }); | 252 | }); |
@@ -259,7 +259,7 @@ export default class ServerApi { | |||
259 | } | 259 | } |
260 | 260 | ||
261 | async deleteService(id) { | 261 | async deleteService(id) { |
262 | const request = await sendAuthRequest(`${API_URL}/service/${id}`, { | 262 | const request = await sendAuthRequest(`${apiBase()}/service/${id}`, { |
263 | method: 'DELETE', | 263 | method: 'DELETE', |
264 | }); | 264 | }); |
265 | if (!request.ok) { | 265 | if (!request.ok) { |
@@ -275,7 +275,7 @@ export default class ServerApi { | |||
275 | 275 | ||
276 | // Features | 276 | // Features |
277 | async getDefaultFeatures() { | 277 | async getDefaultFeatures() { |
278 | const request = await sendAuthRequest(`${API_URL}/features/default`); | 278 | const request = await sendAuthRequest(`${apiBase()}/features/default`); |
279 | if (!request.ok) { | 279 | if (!request.ok) { |
280 | throw request; | 280 | throw request; |
281 | } | 281 | } |
@@ -287,7 +287,7 @@ export default class ServerApi { | |||
287 | } | 287 | } |
288 | 288 | ||
289 | async getFeatures() { | 289 | async getFeatures() { |
290 | const request = await sendAuthRequest(`${API_URL}/features`); | 290 | const request = await sendAuthRequest(`${apiBase()}/features`); |
291 | if (!request.ok) { | 291 | if (!request.ok) { |
292 | throw request; | 292 | throw request; |
293 | } | 293 | } |
@@ -321,7 +321,7 @@ export default class ServerApi { | |||
321 | } | 321 | } |
322 | 322 | ||
323 | async getRecipeUpdates(recipeVersions) { | 323 | async getRecipeUpdates(recipeVersions) { |
324 | const request = await sendAuthRequest(`${API_URL}/recipes/update`, { | 324 | const request = await sendAuthRequest(`${apiBase()}/recipes/update`, { |
325 | method: 'POST', | 325 | method: 'POST', |
326 | body: JSON.stringify(recipeVersions), | 326 | body: JSON.stringify(recipeVersions), |
327 | }); | 327 | }); |
@@ -335,7 +335,7 @@ export default class ServerApi { | |||
335 | 335 | ||
336 | // Recipes Previews | 336 | // Recipes Previews |
337 | async getRecipePreviews() { | 337 | async getRecipePreviews() { |
338 | const request = await sendAuthRequest(`${API_URL}/recipes`); | 338 | const request = await sendAuthRequest(`${apiBase()}/recipes`); |
339 | if (!request.ok) throw request; | 339 | if (!request.ok) throw request; |
340 | const data = await request.json(); | 340 | const data = await request.json(); |
341 | const recipePreviews = this._mapRecipePreviewModel(data); | 341 | const recipePreviews = this._mapRecipePreviewModel(data); |
@@ -344,7 +344,7 @@ export default class ServerApi { | |||
344 | } | 344 | } |
345 | 345 | ||
346 | async getFeaturedRecipePreviews() { | 346 | async getFeaturedRecipePreviews() { |
347 | const request = await sendAuthRequest(`${API_URL}/recipes/popular`); | 347 | const request = await sendAuthRequest(`${apiBase()}/recipes/popular`); |
348 | if (!request.ok) throw request; | 348 | if (!request.ok) throw request; |
349 | 349 | ||
350 | const data = await request.json(); | 350 | const data = await request.json(); |
@@ -356,7 +356,7 @@ export default class ServerApi { | |||
356 | } | 356 | } |
357 | 357 | ||
358 | async searchRecipePreviews(needle) { | 358 | async searchRecipePreviews(needle) { |
359 | const url = `${API_URL}/recipes/search?needle=${needle}`; | 359 | const url = `${apiBase()}/recipes/search?needle=${needle}`; |
360 | const request = await sendAuthRequest(url); | 360 | const request = await sendAuthRequest(url); |
361 | if (!request.ok) throw request; | 361 | if (!request.ok) throw request; |
362 | 362 | ||
@@ -370,14 +370,29 @@ export default class ServerApi { | |||
370 | try { | 370 | try { |
371 | const recipesDirectory = path.join(app.getPath('userData'), 'recipes'); | 371 | const recipesDirectory = path.join(app.getPath('userData'), 'recipes'); |
372 | const recipeTempDirectory = path.join(recipesDirectory, 'temp', recipeId); | 372 | const recipeTempDirectory = path.join(recipesDirectory, 'temp', recipeId); |
373 | const archivePath = path.join(recipeTempDirectory, 'recipe.tar.gz'); | 373 | const tempArchivePath = path.join(recipeTempDirectory, 'recipe.tar.gz'); |
374 | const packageUrl = `${API_URL}/recipes/download/${recipeId}`; | 374 | |
375 | const internalRecipeFile = path.join(RECIPES_PATH, `${recipeId}.tar.gz`); | ||
375 | 376 | ||
376 | fs.ensureDirSync(recipeTempDirectory); | 377 | fs.ensureDirSync(recipeTempDirectory); |
377 | const res = await fetch(packageUrl); | 378 | |
378 | debug('Recipe downloaded', recipeId); | 379 | let archivePath; |
379 | const buffer = await res.buffer(); | 380 | |
380 | fs.writeFileSync(archivePath, buffer); | 381 | if (await fs.exists(internalRecipeFile)) { |
382 | console.log('[ServerApi::getRecipePackage] Using internal recipe file'); | ||
383 | archivePath = internalRecipeFile; | ||
384 | } else { | ||
385 | console.log('[ServerApi::getRecipePackage] Downloading recipe from server'); | ||
386 | archivePath = tempArchivePath; | ||
387 | |||
388 | const packageUrl = `${apiBase()}/recipes/download/${recipeId}`; | ||
389 | |||
390 | const res = await fetch(packageUrl); | ||
391 | debug('Recipe downloaded', recipeId); | ||
392 | const buffer = await res.buffer(); | ||
393 | fs.writeFileSync(archivePath, buffer); | ||
394 | } | ||
395 | console.log(archivePath); | ||
381 | 396 | ||
382 | await sleep(10); | 397 | await sleep(10); |
383 | 398 | ||
@@ -408,7 +423,7 @@ export default class ServerApi { | |||
408 | 423 | ||
409 | // Payment | 424 | // Payment |
410 | async getPlans() { | 425 | async getPlans() { |
411 | const request = await sendAuthRequest(`${API_URL}/payment/plans`); | 426 | const request = await sendAuthRequest(`${apiBase()}/payment/plans`); |
412 | if (!request.ok) throw request; | 427 | if (!request.ok) throw request; |
413 | const data = await request.json(); | 428 | const data = await request.json(); |
414 | const plan = new PlanModel(data); | 429 | const plan = new PlanModel(data); |
@@ -417,7 +432,7 @@ export default class ServerApi { | |||
417 | } | 432 | } |
418 | 433 | ||
419 | async getHostedPage(planId) { | 434 | async getHostedPage(planId) { |
420 | const request = await sendAuthRequest(`${API_URL}/payment/init`, { | 435 | const request = await sendAuthRequest(`${apiBase()}/payment/init`, { |
421 | method: 'POST', | 436 | method: 'POST', |
422 | body: JSON.stringify({ | 437 | body: JSON.stringify({ |
423 | planId, | 438 | planId, |
@@ -434,7 +449,7 @@ export default class ServerApi { | |||
434 | 449 | ||
435 | // News | 450 | // News |
436 | async getLatestNews() { | 451 | async getLatestNews() { |
437 | const url = `${API_URL}/news?platform=${os.platform()}&arch=${os.arch()}&version=${app.getVersion()}`; | 452 | const url = `${apiBase()}/news?platform=${os.platform()}&arch=${os.arch()}&version=${app.getVersion()}`; |
438 | const request = await sendAuthRequest(url); | 453 | const request = await sendAuthRequest(url); |
439 | if (!request.ok) throw request; | 454 | if (!request.ok) throw request; |
440 | const data = await request.json(); | 455 | const data = await request.json(); |
@@ -444,7 +459,7 @@ export default class ServerApi { | |||
444 | } | 459 | } |
445 | 460 | ||
446 | async hideNews(id) { | 461 | async hideNews(id) { |
447 | const request = await sendAuthRequest(`${API_URL}/news/${id}/read`); | 462 | const request = await sendAuthRequest(`${apiBase()}/news/${id}/read`); |
448 | if (!request.ok) throw request; | 463 | if (!request.ok) throw request; |
449 | debug('ServerApi::hideNews resolves', id); | 464 | debug('ServerApi::hideNews resolves', id); |
450 | } | 465 | } |
@@ -469,7 +484,7 @@ export default class ServerApi { | |||
469 | if (Object.prototype.hasOwnProperty.call(config, 'services')) { | 484 | if (Object.prototype.hasOwnProperty.call(config, 'services')) { |
470 | const services = await Promise.all(config.services.map(async (s) => { | 485 | const services = await Promise.all(config.services.map(async (s) => { |
471 | const service = s; | 486 | const service = s; |
472 | const request = await sendAuthRequest(`${API_URL}/recipes/${s.service}`); | 487 | const request = await sendAuthRequest(`${apiBase()}/recipes/${s.service}`); |
473 | 488 | ||
474 | if (request.status === 200) { | 489 | if (request.status === 200) { |
475 | const data = await request.json(); | 490 | const data = await request.json(); |