2018-03-29 22:08:13 -05:00
|
|
|
import config from '../config';
|
|
|
|
import * as Errors from './Errors';
|
|
|
|
|
|
|
|
class Api {
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
|
|
|
baseUrl() { return config.baseApiUrl; }
|
|
|
|
|
|
|
|
url(path) {
|
|
|
|
return this.baseUrl() + path;
|
|
|
|
}
|
|
|
|
|
|
|
|
checkStatus(response) {
|
|
|
|
if (response.status >= 200 && response.status < 300) {
|
|
|
|
return response;
|
|
|
|
} else if (response.status === 404) {
|
|
|
|
throw new Errors.ApiNotFoundError(response.statusText, response);
|
2018-04-01 21:43:23 -05:00
|
|
|
} else if (response.status === 422) {
|
|
|
|
return response.json().then(json => { throw new Errors.ApiValidationError(null, response, json) }, jsonErr => { throw new Errors.ApiValidationError(null, response, null) });
|
2018-03-29 22:08:13 -05:00
|
|
|
} else {
|
|
|
|
throw new Errors.ApiServerError(response.statusText || "Unknown Server Error", response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parseJSON(response) {
|
|
|
|
return response.json();
|
|
|
|
}
|
|
|
|
|
|
|
|
performRequest(url, method, params = {}) {
|
|
|
|
const hasBody = Object.keys(params || {}).length !== 0;
|
|
|
|
|
|
|
|
const headers = new Headers();
|
|
|
|
headers.append('Accept', 'application/json');
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
headers,
|
2018-03-30 17:08:09 -05:00
|
|
|
method: method,
|
|
|
|
credentials: "same-origin"
|
2018-03-29 22:08:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if (hasBody) {
|
|
|
|
headers.append('Content-Type', 'application/json');
|
|
|
|
opts.body = JSON.stringify(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fetch(url, opts).then(this.checkStatus).then(this.parseJSON);
|
|
|
|
}
|
|
|
|
|
2018-04-01 21:43:23 -05:00
|
|
|
objectToUrlParams(obj, queryParams = [], prefixes = []) {
|
|
|
|
for (let key in obj) {
|
|
|
|
const val = obj[key];
|
|
|
|
const paramName = prefixes.join("[") + "[".repeat(Math.min(prefixes.length, 1)) + encodeURIComponent(key) + "]".repeat(prefixes.length);
|
2018-03-29 22:08:13 -05:00
|
|
|
if (Array.isArray(val)) {
|
|
|
|
for (let x of val) {
|
2018-04-01 21:43:23 -05:00
|
|
|
queryParams.push(paramName + "[]=" + (x === null ? '' : encodeURIComponent(x)));
|
2018-03-29 22:08:13 -05:00
|
|
|
}
|
2018-04-01 21:43:23 -05:00
|
|
|
} else if (typeof(val) === "object") {
|
|
|
|
this.objectToUrlParams(val, queryParams, prefixes.concat([key]));
|
2018-03-29 22:08:13 -05:00
|
|
|
} else {
|
2018-04-01 21:43:23 -05:00
|
|
|
queryParams.push(paramName + "=" + (val === null ? '' : encodeURIComponent(val)));
|
2018-03-29 22:08:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-01 21:43:23 -05:00
|
|
|
return queryParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
get(url, params = {}) {
|
|
|
|
|
|
|
|
const queryParams = this.objectToUrlParams(params);
|
|
|
|
|
2018-03-29 22:08:13 -05:00
|
|
|
if (queryParams.length) {
|
|
|
|
url = url + "?" + queryParams.join("&");
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.performRequest(url, "GET");
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:08:09 -05:00
|
|
|
post(url, params = {}) {
|
|
|
|
return this.performRequest(url, "POST", params);
|
|
|
|
}
|
|
|
|
|
2018-04-01 21:43:23 -05:00
|
|
|
patch(url, params = {}) {
|
|
|
|
return this.performRequest(url, "PATCH", params);
|
|
|
|
}
|
|
|
|
|
2018-03-30 14:31:09 -05:00
|
|
|
getRecipeList(page, per, sortColumn, sortDirection, name, tags) {
|
|
|
|
const params = {
|
2018-04-01 21:43:23 -05:00
|
|
|
criteria: {
|
|
|
|
page: page || null,
|
|
|
|
per: per || null,
|
|
|
|
sort_column: sortColumn || null,
|
|
|
|
sort_direction: sortDirection || null,
|
|
|
|
name: name || null,
|
|
|
|
tags: tags || null
|
|
|
|
}
|
2018-03-30 14:31:09 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
return this.get("/recipes", params);
|
|
|
|
}
|
2018-03-30 17:08:09 -05:00
|
|
|
|
2018-04-01 21:43:23 -05:00
|
|
|
getRecipe(id) {
|
|
|
|
return this.get("/recipes/" + id);
|
|
|
|
}
|
|
|
|
|
2018-04-01 22:32:13 -05:00
|
|
|
buildRecipeParams(recipe) {
|
2018-04-01 21:43:23 -05:00
|
|
|
const params = {
|
|
|
|
recipe: {
|
|
|
|
name: recipe.name,
|
|
|
|
description: recipe.description,
|
|
|
|
source: recipe.source,
|
|
|
|
yields: recipe.yields,
|
|
|
|
total_time: recipe.total_time,
|
|
|
|
active_time: recipe.active_time,
|
|
|
|
step_text: recipe.step_text,
|
|
|
|
tag_names: recipe.tag_names,
|
|
|
|
recipe_ingredients_attributes: recipe.ingredients.map(i => {
|
|
|
|
if (i._destroy) {
|
|
|
|
return {
|
|
|
|
id: i.id,
|
|
|
|
_destroy: true
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
id: i.id,
|
|
|
|
name: i.name,
|
2018-04-01 22:32:13 -05:00
|
|
|
ingredient_id: i.ingredient_id,
|
2018-04-01 21:43:23 -05:00
|
|
|
quantity: i.quantity,
|
|
|
|
units: i.units,
|
|
|
|
preparation: i.preparation,
|
|
|
|
sort_order: i.sort_order
|
|
|
|
};
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
2018-04-01 22:32:13 -05:00
|
|
|
return params;
|
|
|
|
}
|
|
|
|
|
|
|
|
patchRecipe(recipe) {
|
|
|
|
return this.patch("/recipes/" + recipe.id, this.buildRecipeParams(recipe));
|
|
|
|
}
|
2018-04-01 21:43:23 -05:00
|
|
|
|
2018-04-01 22:32:13 -05:00
|
|
|
postRecipe(recipe) {
|
|
|
|
return this.post("/recipes/", this.buildRecipeParams(recipe));
|
2018-04-01 21:43:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
postPreviewSteps(step_text) {
|
|
|
|
const params = {
|
|
|
|
step_text: step_text
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.post("/recipes/preview_steps", params);
|
|
|
|
}
|
|
|
|
|
|
|
|
getSearchIngredients(query) {
|
|
|
|
const params = { query: query };
|
|
|
|
return this.get("/ingredients/search", params);
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:08:09 -05:00
|
|
|
postLogin(username, password) {
|
|
|
|
const params = {
|
|
|
|
username: username,
|
|
|
|
password: password
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.post("/login", params);
|
|
|
|
}
|
|
|
|
|
2018-04-01 12:17:54 -05:00
|
|
|
getLogout() {
|
|
|
|
return this.get("/logout");
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:08:09 -05:00
|
|
|
getCurrentUser() {
|
|
|
|
return this.get("/user")
|
|
|
|
}
|
2018-03-29 22:08:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const api = new Api();
|
|
|
|
|
|
|
|
export default api;
|