From 1c84c009b4a5a48a5913a17305c948bc66787fac Mon Sep 17 00:00:00 2001 From: Sanjay Bhangar Date: Wed, 5 Dec 2018 20:45:55 +0200 Subject: [PATCH] add methods required to fetch data from the API --- src/utils/api.js | 208 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 src/utils/api.js diff --git a/src/utils/api.js b/src/utils/api.js new file mode 100644 index 0000000..df73729 --- /dev/null +++ b/src/utils/api.js @@ -0,0 +1,208 @@ +import config from '../config'; +import 'whatwg-fetch'; + + +/* + Base function to make API calls + All other API methods will call this internally + Ideally, this is not to be called from the outside + See https://858.ma/api for API documentation + + @param {String} action - API action to call + @param {Object} data - data to send with API call, will be JSON.stringified + @returns {Promise} - Resolves with data if successful, else throws error. +*/ +export function callApi(action, data) { + let formData = new FormData(); + formData.append('action', action); + formData.append('data', JSON.stringify(data)); + return fetch(config.apiUrl, { + method: 'POST', + body: formData + }) + .then(response => response.json()) + .then(data => { + if (data.status.code === 200) { + return data.data; + } else { + throw 'API Error'; + // Status code was not 200 + // We need to do some sort of error handling here + // Not sure the best way to handle this + } + }) +} + +/* + Fetch all topics from backend + @param {Number} startRange - range to start from, defaults to 0 + @param {Number} endRange - range to end at, defaults to 1000 + @returns {Promise} array of topic objects like: {'name': '', 'items': } +*/ +export function fetchAllTopics(startRange=0, endRange=1000) { + const data = { + "query": { + "conditions": [], + "operator": "&" + }, + "range": [startRange, endRange], + "sort": [{ + "key": "items", + "operator": "-" + }], + "group": "topic" + }; + return callApi('find', data).then(data => data.items); +} + +export function fetchAllMonths(startRange=0, endRange=500) { + const data = { + "query": { + "conditions": [], + "operator": "&" + }, + "range": [startRange, endRange], + "sort": [{ + "key": "items", + "operator": "-" + }], + "group": "month" + }; + return callApi('find', data).then(data => data.items); +} + +export function fetchAllDates(startRange=0, endRange=1000) { + const data = { + "query": { + "conditions": [], + "operator": "&" + }, + "range": [startRange, endRange], + "sort": [{ + "key": "items", + "operator": "-" + }], + "group": "date" + }; + return callApi('find', data).then(data => data.items); +} + +/* + Fetch videos filtered by key + @param {String} key - key to filter by - eg. "topic" + @param {String} value - value to filter by + @param {Number} startRange - start range + @param {Number} endRange - end range + @param {String} sortKey - key to sort by, defaults to "random" + @returns {Promise} Promise resolves to array of video objects +*/ +export function fetchVideosByX(key, value, startRange, endRange, sortKey='random') { + const data = { + "keys": ["editable", "modified", "title", "source", "project", "topic", "language", "duration", "id"], + "query": { + "conditions": [{ + "key": key, + "value": encodeURIComponent(value), + "operator": "==" + }], + "operator": "&" + }, + "range": [startRange, endRange], + "sort": [{ + "key": sortKey, + "operator": "-" + }] + }; + return callApi('find', data).then(data => data.items); +} + +/* + Fetch videos filtered by topic + @param {String} topic - topic to filter by + @param {Number} startRange - start range + @param {Number} endRange - end range + @param {String} sortKey - key to sort by, defaults to "random" + @returns {Promise} Promise resolves to array of video objects +*/ +export function fetchVideosByTopic(topic, startRange=0, endRange=100, sortKey='random') { + return fetchVideosByX('topic', topic, startRange, endRange, sortKey); +}; + +/* + month eg. "2011-11" +*/ +export function fetchVideosByMonth(month, startRange=0, endRange=100, sortKey='random') { + return fetchVideosByX('month', month, startRange, endRange, sortKey); +} + +/* + date eg. "2011-11-01" +*/ +export function fetchVideosByDate(date, startRange=0, endRange=100, sortKey='random') { + return fetchVideosByX('date', date, startRange, endRange, sortKey); +} + + +/* + Fetch places within bounding box + + @param {Float} north - latitude of north bounds + @param {Float} south - latitude of south bounds + @param {Float} west - longitude of west bounds + @param {Float} east - longitude of east bounds +*/ +export function fetchPlaces(north, south, west, east, startRange=0, endRange=100) { + const data = { + "itemsQuery": { + "conditions": [], + "operator": "&" + }, + "keys": ["id", "name", "alternativeNames", "geoname", "countryCode", "type", "lat", "lng", "south", "west", "north", "east", "area", "editable"], + "query": { + "conditions": [{ + "key": "lat", + "value": [south, north], + "operator": "=" + }, { + "key": "lng", + "value": [west, east], + "operator": "=" + }], + "operator": "&" + }, + "range": [startRange, endRange], + "sort": [{ + "key": "area", + "operator": "-" + }] + }; + return callApi('findPlaces', data).then(data => data.items); +} + +/* + Fetches a random place +*/ +export function fetchRandomPlace() { + const data = { + "itemsQuery": { + "conditions": [], + "operator": "&" + }, + "keys": ["id", "name", "alternativeNames", "geoname", "countryCode", "type", "lat", "lng", "south", "west", "north", "east", "area", "editable"], + "query": { + "conditions": [], + "operator": "&" + }, + "range": [0, 1], + "sort": [{ + "key": "random", + "operator": "-" + }] + }; + return callApi('findPlaces', data).then(data => data.items[0]); +} + + +export function fetchVideosByPlace(placeId, startRange=0, endRange=100, sortKey='random') { + return fetchVideosByX('place', placeId, startRange, endRange, sortKey); +}