aboutsummaryrefslogtreecommitdiffstats
path: root/src/api
diff options
context:
space:
mode:
authorLibravatar vantezzen <properly@protonmail.com>2019-08-25 13:56:14 +0200
committerLibravatar vantezzen <properly@protonmail.com>2019-08-25 13:56:14 +0200
commit051314eac6f062f18ea52a2fda2f1ecd1164b64b (patch)
tree067f432695184ae78ed2474723e9b0ec70d42507 /src/api
parentAdd custom tray icons (diff)
downloadferdium-app-051314eac6f062f18ea52a2fda2f1ecd1164b64b.tar.gz
ferdium-app-051314eac6f062f18ea52a2fda2f1ecd1164b64b.tar.zst
ferdium-app-051314eac6f062f18ea52a2fda2f1ecd1164b64b.zip
Unifying apiBase function
Diffstat (limited to 'src/api')
-rw-r--r--src/api/apiBase.js25
-rw-r--r--src/api/server/ServerApi.js76
2 files changed, 52 insertions, 49 deletions
diff --git a/src/api/apiBase.js b/src/api/apiBase.js
new file mode 100644
index 000000000..42f09eaa8
--- /dev/null
+++ b/src/api/apiBase.js
@@ -0,0 +1,25 @@
1/**
2 * Get API base URL from store
3 */
4import {
5 API_VERSION,
6 API,
7} from '../environment';
8
9const apiBase = () => {
10 let url;
11 if (!window.ferdi.stores.settings || !window.ferdi.stores.settings.all) {
12 // Stores have not yet been loaded - send invalid URL to force a retry when stores are loaded
13 url = 'https://localhost:9999';
14 } else if (window.ferdi.stores.settings.all.app.server) {
15 // Load URL from store
16 url = window.ferdi.stores.settings.all.app.server;
17 } else {
18 // Use default server url
19 url = API;
20 }
21
22 return `${url}/${API_VERSION}`;
23};
24
25export default apiBase;
diff --git a/src/api/server/ServerApi.js b/src/api/server/ServerApi.js
index 46a5b928d..2111af9fe 100644
--- a/src/api/server/ServerApi.js
+++ b/src/api/server/ServerApi.js
@@ -15,6 +15,7 @@ import OrderModel from '../../models/Order';
15import { sleep } from '../../helpers/async-helpers'; 15import { sleep } from '../../helpers/async-helpers';
16 16
17import { API } from '../../environment'; 17import { API } from '../../environment';
18import apiBase from '../apiBase';
18import { prepareAuthRequest, sendAuthRequest } from '../utils/auth'; 19import { prepareAuthRequest, sendAuthRequest } from '../utils/auth';
19 20
20import { 21import {
@@ -38,38 +39,15 @@ const { app } = remote;
38const { default: fetch } = remote.require('electron-fetch'); 39const { default: fetch } = remote.require('electron-fetch');
39 40
40const SERVER_URL = API; 41const SERVER_URL = API;
41const API_VERSION = 'v1';
42 42
43export default class ServerApi { 43export default class ServerApi {
44 recipePreviews = []; 44 recipePreviews = [];
45 45
46 recipes = []; 46 recipes = [];
47 47
48 stores = {};
49
50 setStores(stores) {
51 this.stores = stores;
52 }
53
54 apiUrl() {
55 let url;
56 if (!this.stores.settings) {
57 // Stores have not yet been loaded - send invalid URL to force a retry when stores are loaded
58 url = 'https://localhost:9999';
59 } else if (this.stores.settings.all.app.server) {
60 // Load URL from store
61 url = this.stores.settings.all.app.server;
62 } else {
63 // Use default server url
64 url = SERVER_URL;
65 }
66
67 return `${url}/${API_VERSION}`;
68 }
69
70 // User 48 // User
71 async login(email, passwordHash) { 49 async login(email, passwordHash) {
72 const request = await sendAuthRequest(`${this.apiUrl()}/auth/login`, { 50 const request = await sendAuthRequest(`${apiBase()}/auth/login`, {
73 method: 'POST', 51 method: 'POST',
74 headers: { 52 headers: {
75 Authorization: `Basic ${window.btoa(`${email}:${passwordHash}`)}`, 53 Authorization: `Basic ${window.btoa(`${email}:${passwordHash}`)}`,
@@ -85,7 +63,7 @@ export default class ServerApi {
85 } 63 }
86 64
87 async signup(data) { 65 async signup(data) {
88 const request = await sendAuthRequest(`${this.apiUrl()}/auth/signup`, { 66 const request = await sendAuthRequest(`${apiBase()}/auth/signup`, {
89 method: 'POST', 67 method: 'POST',
90 body: JSON.stringify(data), 68 body: JSON.stringify(data),
91 }, false); 69 }, false);
@@ -99,7 +77,7 @@ export default class ServerApi {
99 } 77 }
100 78
101 async inviteUser(data) { 79 async inviteUser(data) {
102 const request = await sendAuthRequest(`${this.apiUrl()}/invite`, { 80 const request = await sendAuthRequest(`${apiBase()}/invite`, {
103 method: 'POST', 81 method: 'POST',
104 body: JSON.stringify(data), 82 body: JSON.stringify(data),
105 }); 83 });
@@ -112,7 +90,7 @@ export default class ServerApi {
112 } 90 }
113 91
114 async retrievePassword(email) { 92 async retrievePassword(email) {
115 const request = await sendAuthRequest(`${this.apiUrl()}/auth/password`, { 93 const request = await sendAuthRequest(`${apiBase()}/auth/password`, {
116 method: 'POST', 94 method: 'POST',
117 body: JSON.stringify({ 95 body: JSON.stringify({
118 email, 96 email,
@@ -128,7 +106,7 @@ export default class ServerApi {
128 } 106 }
129 107
130 async userInfo() { 108 async userInfo() {
131 const request = await sendAuthRequest(`${this.apiUrl()}/me`); 109 const request = await sendAuthRequest(`${apiBase()}/me`);
132 if (!request.ok) { 110 if (!request.ok) {
133 throw request; 111 throw request;
134 } 112 }
@@ -141,7 +119,7 @@ export default class ServerApi {
141 } 119 }
142 120
143 async updateUserInfo(data) { 121 async updateUserInfo(data) {
144 const request = await sendAuthRequest(`${this.apiUrl()}/me`, { 122 const request = await sendAuthRequest(`${apiBase()}/me`, {
145 method: 'PUT', 123 method: 'PUT',
146 body: JSON.stringify(data), 124 body: JSON.stringify(data),
147 }); 125 });
@@ -156,7 +134,7 @@ export default class ServerApi {
156 } 134 }
157 135
158 async deleteAccount() { 136 async deleteAccount() {
159 const request = await sendAuthRequest(`${this.apiUrl()}/me`, { 137 const request = await sendAuthRequest(`${apiBase()}/me`, {
160 method: 'DELETE', 138 method: 'DELETE',
161 }); 139 });
162 if (!request.ok) { 140 if (!request.ok) {
@@ -170,7 +148,7 @@ export default class ServerApi {
170 148
171 // Services 149 // Services
172 async getServices() { 150 async getServices() {
173 const request = await sendAuthRequest(`${this.apiUrl()}/me/services`); 151 const request = await sendAuthRequest(`${apiBase()}/me/services`);
174 if (!request.ok) { 152 if (!request.ok) {
175 throw request; 153 throw request;
176 } 154 }
@@ -183,7 +161,7 @@ export default class ServerApi {
183 } 161 }
184 162
185 async createService(recipeId, data) { 163 async createService(recipeId, data) {
186 const request = await sendAuthRequest(`${this.apiUrl()}/service`, { 164 const request = await sendAuthRequest(`${apiBase()}/service`, {
187 method: 'POST', 165 method: 'POST',
188 body: JSON.stringify(Object.assign({ 166 body: JSON.stringify(Object.assign({
189 recipeId, 167 recipeId,
@@ -213,7 +191,7 @@ export default class ServerApi {
213 await this.uploadServiceIcon(serviceId, data.iconFile); 191 await this.uploadServiceIcon(serviceId, data.iconFile);
214 } 192 }
215 193
216 const request = await sendAuthRequest(`${this.apiUrl()}/service/${serviceId}`, { 194 const request = await sendAuthRequest(`${apiBase()}/service/${serviceId}`, {
217 method: 'PUT', 195 method: 'PUT',
218 body: JSON.stringify(data), 196 body: JSON.stringify(data),
219 }); 197 });
@@ -241,7 +219,7 @@ export default class ServerApi {
241 219
242 delete requestData.headers['Content-Type']; 220 delete requestData.headers['Content-Type'];
243 221
244 const request = await window.fetch(`${this.apiUrl()}/service/${serviceId}`, requestData); 222 const request = await window.fetch(`${apiBase()}/service/${serviceId}`, requestData);
245 223
246 if (!request.ok) { 224 if (!request.ok) {
247 throw request; 225 throw request;
@@ -253,7 +231,7 @@ export default class ServerApi {
253 } 231 }
254 232
255 async reorderService(data) { 233 async reorderService(data) {
256 const request = await sendAuthRequest(`${this.apiUrl()}/service/reorder`, { 234 const request = await sendAuthRequest(`${apiBase()}/service/reorder`, {
257 method: 'PUT', 235 method: 'PUT',
258 body: JSON.stringify(data), 236 body: JSON.stringify(data),
259 }); 237 });
@@ -266,7 +244,7 @@ export default class ServerApi {
266 } 244 }
267 245
268 async deleteService(id) { 246 async deleteService(id) {
269 const request = await sendAuthRequest(`${this.apiUrl()}/service/${id}`, { 247 const request = await sendAuthRequest(`${apiBase()}/service/${id}`, {
270 method: 'DELETE', 248 method: 'DELETE',
271 }); 249 });
272 if (!request.ok) { 250 if (!request.ok) {
@@ -282,7 +260,7 @@ export default class ServerApi {
282 260
283 // Features 261 // Features
284 async getDefaultFeatures() { 262 async getDefaultFeatures() {
285 const request = await sendAuthRequest(`${this.apiUrl()}/features/default`); 263 const request = await sendAuthRequest(`${apiBase()}/features/default`);
286 if (!request.ok) { 264 if (!request.ok) {
287 throw request; 265 throw request;
288 } 266 }
@@ -294,7 +272,7 @@ export default class ServerApi {
294 } 272 }
295 273
296 async getFeatures() { 274 async getFeatures() {
297 const request = await sendAuthRequest(`${this.apiUrl()}/features`); 275 const request = await sendAuthRequest(`${apiBase()}/features`);
298 if (!request.ok) { 276 if (!request.ok) {
299 throw request; 277 throw request;
300 } 278 }
@@ -328,7 +306,7 @@ export default class ServerApi {
328 } 306 }
329 307
330 async getRecipeUpdates(recipeVersions) { 308 async getRecipeUpdates(recipeVersions) {
331 const request = await sendAuthRequest(`${this.apiUrl()}/recipes/update`, { 309 const request = await sendAuthRequest(`${apiBase()}/recipes/update`, {
332 method: 'POST', 310 method: 'POST',
333 body: JSON.stringify(recipeVersions), 311 body: JSON.stringify(recipeVersions),
334 }); 312 });
@@ -342,7 +320,7 @@ export default class ServerApi {
342 320
343 // Recipes Previews 321 // Recipes Previews
344 async getRecipePreviews() { 322 async getRecipePreviews() {
345 const request = await sendAuthRequest(`${this.apiUrl()}/recipes`); 323 const request = await sendAuthRequest(`${apiBase()}/recipes`);
346 if (!request.ok) throw request; 324 if (!request.ok) throw request;
347 const data = await request.json(); 325 const data = await request.json();
348 const recipePreviews = this._mapRecipePreviewModel(data); 326 const recipePreviews = this._mapRecipePreviewModel(data);
@@ -351,7 +329,7 @@ export default class ServerApi {
351 } 329 }
352 330
353 async getFeaturedRecipePreviews() { 331 async getFeaturedRecipePreviews() {
354 const request = await sendAuthRequest(`${this.apiUrl()}/recipes/popular`); 332 const request = await sendAuthRequest(`${apiBase()}/recipes/popular`);
355 if (!request.ok) throw request; 333 if (!request.ok) throw request;
356 334
357 const data = await request.json(); 335 const data = await request.json();
@@ -363,7 +341,7 @@ export default class ServerApi {
363 } 341 }
364 342
365 async searchRecipePreviews(needle) { 343 async searchRecipePreviews(needle) {
366 const url = `${this.apiUrl()}/recipes/search?needle=${needle}`; 344 const url = `${apiBase()}/recipes/search?needle=${needle}`;
367 const request = await sendAuthRequest(url); 345 const request = await sendAuthRequest(url);
368 if (!request.ok) throw request; 346 if (!request.ok) throw request;
369 347
@@ -378,7 +356,7 @@ export default class ServerApi {
378 const recipesDirectory = path.join(app.getPath('userData'), 'recipes'); 356 const recipesDirectory = path.join(app.getPath('userData'), 'recipes');
379 const recipeTempDirectory = path.join(recipesDirectory, 'temp', recipeId); 357 const recipeTempDirectory = path.join(recipesDirectory, 'temp', recipeId);
380 const archivePath = path.join(recipeTempDirectory, 'recipe.tar.gz'); 358 const archivePath = path.join(recipeTempDirectory, 'recipe.tar.gz');
381 const packageUrl = `${this.apiUrl()}/recipes/download/${recipeId}`; 359 const packageUrl = `${apiBase()}/recipes/download/${recipeId}`;
382 360
383 fs.ensureDirSync(recipeTempDirectory); 361 fs.ensureDirSync(recipeTempDirectory);
384 const res = await fetch(packageUrl); 362 const res = await fetch(packageUrl);
@@ -415,7 +393,7 @@ export default class ServerApi {
415 393
416 // Payment 394 // Payment
417 async getPlans() { 395 async getPlans() {
418 const request = await sendAuthRequest(`${this.apiUrl()}/payment/plans`); 396 const request = await sendAuthRequest(`${apiBase()}/payment/plans`);
419 if (!request.ok) throw request; 397 if (!request.ok) throw request;
420 const data = await request.json(); 398 const data = await request.json();
421 const plan = new PlanModel(data); 399 const plan = new PlanModel(data);
@@ -424,7 +402,7 @@ export default class ServerApi {
424 } 402 }
425 403
426 async getHostedPage(planId) { 404 async getHostedPage(planId) {
427 const request = await sendAuthRequest(`${this.apiUrl()}/payment/init`, { 405 const request = await sendAuthRequest(`${apiBase()}/payment/init`, {
428 method: 'POST', 406 method: 'POST',
429 body: JSON.stringify({ 407 body: JSON.stringify({
430 planId, 408 planId,
@@ -441,7 +419,7 @@ export default class ServerApi {
441 419
442 // News 420 // News
443 async getLatestNews() { 421 async getLatestNews() {
444 const url = `${this.apiUrl()}/news?platform=${os.platform()}&arch=${os.arch()}&version=${app.getVersion()}`; 422 const url = `${apiBase()}/news?platform=${os.platform()}&arch=${os.arch()}&version=${app.getVersion()}`;
445 const request = await sendAuthRequest(url); 423 const request = await sendAuthRequest(url);
446 if (!request.ok) throw request; 424 if (!request.ok) throw request;
447 const data = await request.json(); 425 const data = await request.json();
@@ -451,7 +429,7 @@ export default class ServerApi {
451 } 429 }
452 430
453 async hideNews(id) { 431 async hideNews(id) {
454 const request = await sendAuthRequest(`${this.apiUrl()}/news/${id}/read`); 432 const request = await sendAuthRequest(`${apiBase()}/news/${id}/read`);
455 if (!request.ok) throw request; 433 if (!request.ok) throw request;
456 debug('ServerApi::hideNews resolves', id); 434 debug('ServerApi::hideNews resolves', id);
457 } 435 }
@@ -476,7 +454,7 @@ export default class ServerApi {
476 if (Object.prototype.hasOwnProperty.call(config, 'services')) { 454 if (Object.prototype.hasOwnProperty.call(config, 'services')) {
477 const services = await Promise.all(config.services.map(async (s) => { 455 const services = await Promise.all(config.services.map(async (s) => {
478 const service = s; 456 const service = s;
479 const request = await sendAuthRequest(`${this.apiUrl()}/recipes/${s.service}`); 457 const request = await sendAuthRequest(`${apiBase()}/recipes/${s.service}`);
480 458
481 if (request.status === 200) { 459 if (request.status === 200) {
482 const data = await request.json(); 460 const data = await request.json();