| 1234567891011121314151617181920212223242526272829 |
- import { Meteor } from 'meteor/meteor';
- import 'whatwg-fetch';
- import querystring from 'querystring';
-
- let KEY = '';
- Meteor.call('security.key', (err, res) => KEY = res.key);
- const ENDPOINT = 'https://www.googleapis.com/youtube/v3';
-
- const checkStatus = response => {
- if (response.status >= 200 && response.status < 300) return response;
- let error = new Error(response.statusText);
- error.response = response;
- throw error
- };
-
- const parseJson = response => response.json();
-
- const YouTubeAPIQuery = ({ action, params}) =>
- fetch(`${ENDPOINT}/${action}?${querystring.stringify(params)}`)
- .then(checkStatus)
- .then(parseJson);
-
- const YouTubeSearch = query => YouTubeAPIQuery({ action: 'search',
- params: { part: 'snippet', type: 'video', key: KEY, q: query }});
-
- const YouTubeVideoInfo = id => YouTubeAPIQuery({ action: 'videos',
- params: { part: 'statistics,contentDetails', key: KEY, id: id }});
-
- export { YouTubeSearch, YouTubeVideoInfo };
|