-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvpapi.js
More file actions
180 lines (141 loc) · 5.44 KB
/
Copy pathvpapi.js
File metadata and controls
180 lines (141 loc) · 5.44 KB
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
//
// vpapi.js
//
// Functions to load data from the AWE Video Promition API.
//
/**
* The Video Promotion API's base URL, **without** a trailing slash.
* @const
*/
var API_BASE_URL = 'https://pt.protoawe.com/api/video-promotion/v1';
var API_PSID = 'YOUR_PSID_HERE';
var API_ACCESS_KEY = 'YOUR_ACCESS_KEY_HERE';
/**********************************************************************************/
/******************************** HTTP Requests ***********************************/
/**********************************************************************************/
/**
* Sends a request to the API, then parses and returns the API's JSON response.
* @internal
* @param {string} path The URL path to request from.
* @param {object} params Optional parameters to send to the API.
* @return {Promise<object>} The parsed response.
*/
function sendRequest(path, params) {
// remove leading slashes from the path to avoid multiple slashes in the URL
path = path.replace(/^\/+/, '');
// ensure params is an object
params = params || {};
// inject parameters that are needed for every request
params.psid = API_PSID;
params.accessKey = API_ACCESS_KEY;
// remove all params that are `undefined`
for (var key in params) {
if (typeof params[key] === 'undefined') {
delete params[key];
}
}
// Create a URL object, then add the params to it. For more info, see:
// - https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
// - https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
var url = new URL(API_BASE_URL + '/' + path);
url.search = new URLSearchParams(params);
// create the request options
var options = {
// all requests against the Video Promotion API are GET
method: 'GET',
// add the necessary headers
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
};
// send the request, parse it as JSON, then return the nested `data`
return fetch(url, options).then(function(rawResponse) {
return rawResponse.json().then(function(parsedResponse) {
return parsedResponse.data;
});
});
}
/**********************************************************************************/
/************************************* Tags ***************************************/
/**********************************************************************************/
/**
* Used to cache all tags available in the Video Promotion API.
* Variable is assigned when the tags have been loaded for the first time.
* @internal
*/
var cachedTags;
/**
* Actually loads all tags from the Video Promotion API.
* Updates `cachedTags` with the newly loaded tags.
* @internal
* @return {Promise<string[]>} The returned promise will resolve to a string array,
* containing all available tags.
*/
function loadTagListInternal() {
return sendRequest('tags').then(function(data) {
// tags are nested in the response
var tags = data.tags;
// update the tag cache
cachedTags = tags;
// Return the tags from here so callers of `loadTagListInternal`
// get the tags as a return value.
// Return a copy though to prevent accidental modification of the
// `cachedTags` array.
return [].concat(tags);
});
}
/**
* Returns a list of all tags available through the Video Promotion API.
* @param {boolean} allowCache Whether to allow using cached tags.
* @return {Promise<string[]>} The returned promise will resolve to a string array,
* containing all available tags.
*/
function loadTagList(allowCache) {
// if we're allowed to use cached tags and if there are any cached tags,
// return them instead of reloading from the API
if (allowCache && cachedTags) {
// return a copy of the cached tags array to prevent
// accidental modification of our actual cache
var tags = [].concat(cachedTags);
return Promise.resolve(tags);
}
// tags not cached yet, load the tags from the API instead
else {
return loadTagListInternal();
}
}
/**********************************************************************************/
/************************************* List ***************************************/
/**********************************************************************************/
function loadList(params) {
// ensure the params object exists
params = params || {};
return sendRequest('client/list', {
pageIndex: Math.max(params.page || 1, 1),
limit: params.limit || undefined,
sexualOrientation: params.sexualOrientation || 'straight',
primaryColor: params.primaryColor || 'ff9900',
labelColor: params.labelColor || '000',
tags: params.tags || undefined
});
}
/**********************************************************************************/
/************************************* Related ************************************/
/**********************************************************************************/
function loadRelated(params) {
// ensure the params object exists
params = params || {};
return sendRequest('client/related/' + params.id, {
pageIndex: Math.max(params.page || 1, 1),
limit: params.limit || undefined,
});
}
/**********************************************************************************/
/*********************************** Details **************************************/
/**********************************************************************************/
function loadDetails(params) {
return sendRequest('client/details/' + params.videoId, {
primaryColor: params.primaryColor || 'ff9900',
labelColor: params.labelColor || '000'
});
}