1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | 1× 1× 1× 1× 1× 5× 5× 5× 1× 5× 5× 5× 5× 5× 1× 1× 4× 1× 1× 1× 1× 5× 1× 1× 1× 1× 8× 1× 1× 1× 1× 1× 1× | 'use strict'; // Dependencies var request = require('request'), q = require('querystring'); /** * Create an instance of Behance * @param {string} token - authentication for Behance API */ var Behance = function(token) { this.clientId = token; // Throw an error if Auth Key is not specified Iif (this.clientId === undefined) { throw new Error('Please supply an authorization token.'); } }; /** * Endpoint and Query Builder * @param {string} endpoint - endpoint to query * @param {object} options - Queries * @api private */ Behance.prototype.buildUrl = function(endpoint, options) { var _this = this; var query = '?' + (options ? q.stringify(options) + '&' : ''), clientId = 'client_id=' + _this.clientId; return 'https://api.behance.net/v2/' + endpoint + query + clientId; }; /** * Request Handler * @param {string} requestUrl - Requested Url * @param {function} cb - callback * @api private */ Behance.prototype.requestHandler = function(requestUrl, cb) { request(requestUrl, function(err, res, body) { Iif (!body) { return cb(new Error('No response from the Behance API')); } try { body = JSON.parse(body); } catch(e) {} cb(err, res, body); }); }; /** * Endpoints that only support Options */ var endpointWithOptionOnly = [{ name: 'projects', path: 'projects' }, { name: 'creativesToFollow', path: 'creativestofollow' }, { name: 'users', path: 'users' }, { name: 'collections', path: 'collections' }]; endpointWithOptionOnly.forEach(function(def) { /** * Get a list of projects/creatives/users/collections * @param {object} opts - queries * @param {function} cb - callback */ Behance.prototype[def.name] = function(opts, cb) { var endpoint = def.path; this.requestHandler(this.buildUrl(endpoint, opts), cb); } }); /** * Endpoints that require an ID with no Options */ var endpointWithOnlyAnId = [{ name: 'project', pathprefix: 'projects/' }, { name: 'user', pathprefix: 'users/' }, { name: 'userStats', pathprefix: 'users/', pathsuffix: '/stats' }, { name: 'userWorkExperience', pathprefix: 'users/', pathsuffix: '/work_experience' }, { name: 'collection', path: 'collection' }]; endpointWithOnlyAnId.forEach(function(def) { /** * Get info about a project/user/collection * @param {string} id - identifier * @param {function} cb - callback */ Behance.prototype[def.name] = function(id, cb) { var endpoint = def.pathprefix + id + (def.pathsuffix ? def.pathsuffix : ''); this.requestHandler(this.buildUrl(endpoint), cb); } }); /** * Endpoints that require an ID and support Options */ var endpointWithIdAndOptions = [{ name: 'projectComments', pathprefix: 'projects/', pathsuffix: '/comments' }, { name: 'userProjects', pathprefix: 'users/', pathsuffix: '/projects' }, { name: 'userWips', pathprefix: 'users/', pathsuffix: '/wips' }, { name: 'userAppreciations', pathprefix: 'users/', pathsuffix: '/appreciations' }, { name: 'userCollections', pathprefix: 'users/', pathsuffix: '/collections' }, { name: 'userFollowers', pathprefix: 'users/', pathsuffix: '/followers' }, { name: 'userFollowing', pathprefix: 'users/', pathsuffix: '/following' }, { name: 'collectionProjects', pathprefix: 'collections/', pathsuffix: '/projects' }]; endpointWithIdAndOptions.forEach(function(def) { /** * Get a list of comments/projects/wips/appreciations/collections/followers * @param {string} id - identifier * @param {object} opts - queries * @param {function} cb - callback */ Behance.prototype[def.name] = function(id, opts, cb) { var endpoint = def.pathprefix + id + (def.pathsuffix ? def.pathsuffix : ''); this.requestHandler(this.buildUrl(endpoint, opts), cb); } }); /** * Get Creative Fields */ Behance.prototype.fields = function(cb) { var endpoint = 'fields'; this.requestHandler(this.buildUrl(endpoint), cb); }; module.exports = Behance; |