|
| 1 | +/** |
| 2 | + * This module provides a service for conventient access to Topcoder APIs. |
| 3 | + */ |
| 4 | + |
| 5 | +import _ from 'lodash'; |
| 6 | +import 'isomorphic-fetch'; /* global fetch */ |
| 7 | +import config from 'utils/config'; |
| 8 | + |
| 9 | +/** |
| 10 | + * API service object. It is reused for both Topcoder API v2 and v3, |
| 11 | + * as in these cases we are fine with the same interface, and the only |
| 12 | + * thing we need to be different is the base URL and auth token to use. |
| 13 | + */ |
| 14 | +export default class Api { |
| 15 | + /** |
| 16 | + * @param {String} base Base URL of the API. |
| 17 | + * @param {String} token Optional. Authorization token. |
| 18 | + */ |
| 19 | + constructor(base, token) { |
| 20 | + this.private = { base, token }; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Sends a request to the specified endpoint of the API. This method just |
| 25 | + * wraps fetch() in a convenient way. If this object was created with the |
| 26 | + * auth token, it will be automatically added to auth header of all |
| 27 | + * requests. |
| 28 | + * For additional details see https://github.github.io/fetch/ |
| 29 | + * @param {String} enpoint Should start with slash, like /endpoint. |
| 30 | + * @param {Object} options Optional. Fetch options. |
| 31 | + * @return {Promise} It resolves to the HTTP response object. To get the |
| 32 | + * actual data you probably want to call .json() method of that object. |
| 33 | + * Mind that this promise rejects only on network errors. In case of |
| 34 | + * HTTP errors (404, etc.) the promise will be resolved successfully, |
| 35 | + * and you should check .status or .ok fields of the response object |
| 36 | + * to find out the response status. |
| 37 | + */ |
| 38 | + fetch(endpoint, options) { |
| 39 | + const p = this.private; |
| 40 | + const headers = { 'Content-Type': 'application/json' }; |
| 41 | + if (p.token) headers.Authorization = `Bearer ${p.token}`; |
| 42 | + const ops = _.merge(_.cloneDeep(options) || {}, { headers }); |
| 43 | + return fetch(`${p.base}${endpoint}`, ops); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Sends DELETE request to the specified endpoint. |
| 48 | + * @param {String} endpoint |
| 49 | + * @param {Blob|BufferSource|FormData|String} body |
| 50 | + * @return {Promise} |
| 51 | + */ |
| 52 | + delete(endpoint, body) { |
| 53 | + return this.fetch(endpoint, { body, method: 'DELETE' }); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Sends GET request to the specified endpoint. |
| 58 | + * @param {String} endpoint |
| 59 | + * @return {Promise} |
| 60 | + */ |
| 61 | + get(endpoint) { |
| 62 | + return this.fetch(endpoint); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Sends POST request to the specified endpoint. |
| 67 | + * @param {String} endpoint |
| 68 | + * @param {Blob|BufferSource|FormData|String} body |
| 69 | + * @return {Promise} |
| 70 | + */ |
| 71 | + post(endpoint, body) { |
| 72 | + return this.fetch(endpoint, { body, method: 'POST' }); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Sends POST request to the specified endpoint, with JSON payload. |
| 77 | + * @param {String} endpoint |
| 78 | + * @param {JSON} json |
| 79 | + * @return {Promise} |
| 80 | + */ |
| 81 | + postJson(endpoint, json) { |
| 82 | + return this.post(endpoint, JSON.stringify(json)); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Sends PUT request to the specified endpoint. |
| 87 | + * @param {String} endpoint |
| 88 | + * @param {Blob|BufferSource|FormData|String} body |
| 89 | + * @return {Promise} |
| 90 | + */ |
| 91 | + put(endpoint, body) { |
| 92 | + return this.fetch(endpoint, { body, method: 'PUT' }); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Sends PUT request to the specified endpoint. |
| 97 | + * @param {String} endpoint |
| 98 | + * @param {JSON} json |
| 99 | + * @return {Promise} |
| 100 | + */ |
| 101 | + putJson(endpoint, json) { |
| 102 | + return this.put(endpoint, JSON.stringify(json)); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Topcoder API v2. |
| 108 | + */ |
| 109 | + |
| 110 | +/** |
| 111 | + * Returns a new or existing Api object for Topcoder API v2. |
| 112 | + * @param {String} token Optional. Auth token for Topcoder API v2. |
| 113 | + * @return {Api} API v2 service object. |
| 114 | + */ |
| 115 | +let lastApiV2 = null; |
| 116 | +export function getApiV2(token) { |
| 117 | + if (!lastApiV2 || lastApiV2.private.token !== token) { |
| 118 | + lastApiV2 = new Api(config.API.V2, token); |
| 119 | + } |
| 120 | + return lastApiV2; |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * Topcoder API v3. |
| 125 | + */ |
| 126 | + |
| 127 | +/** |
| 128 | + * Returns a new or existing Api object for Topcoder API v3 |
| 129 | + * @param {String} token Optional. Auth token for Topcoder API v3. |
| 130 | + * @return {Api} API v3 service object. |
| 131 | + */ |
| 132 | +let lastApiV3 = null; |
| 133 | +export function getApiV3(token) { |
| 134 | + if (!lastApiV3 || lastApiV3.private.token !== token) { |
| 135 | + lastApiV3 = new Api(config.API.V3, token); |
| 136 | + } |
| 137 | + return lastApiV3; |
| 138 | +} |
0 commit comments