forked from christiansmith/ngGAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgapi.js
702 lines (530 loc) · 17.9 KB
/
gapi.js
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
'use strict';
angular.module('gapi', [])
/**
* GAPI exposes many services, but their respective APIs follow
* a pattern. This is fortunate. We can define a core abstraction
* for communicating with all google apis, and then specialize it
* for each service.
*
* The reponsibility of this general service is to implement the
* authorization flow, and make requests on behalf of a dependent
* API specific service.
*
* Each google API maps specific methods to HTTP verbs.
*
* METHOD HTTP
* list GET
* insert POST
* update PUT
* delete DELETE
* etc ...
*
* Among the collected API's, these methods appear to map consistently.
*/
.factory('GAPI', function ($q, $http, GoogleApp) {
/**
* GAPI Credentials
*/
GAPI.app = GoogleApp;
/**
* Google APIs base URL
*/
var server = 'https://www.googleapis.com';
/**
* Generate a method name from an action and a resource
*/
function methodName (action, resource) {
// allow resources with a path prefix
resource = resource.split('/').pop()
// uppercase the first character
resource = resource.charAt(0).toUpperCase() + resource.slice(1);
return action + resource;
}
/**
* Recurse through a "spec" object and create methods for
* resources and nested resources.
*
* For each resource in the provided spec, we define methods
* for each of the its actions.
*/
function createMethods (service, spec, parents) {
var resources = Object.keys(spec);
resources.forEach(function (resource) {
var actions = spec[resource];
actions.forEach(function (action) {
// if the action is an object, treat it as a nested
// spec and recurse
if (typeof action === 'object') {
if (!parents) { parents = []; }
// we can't keep passing around the
// same array, we need a new one
var p = parents.concat([resource]);
createMethods(service, action, p);
} else {
var method = methodName(action, resource);
service[method] = GAPI[action](resource, parents);
}
});
});
}
/**
* GAPI Service Constructor
*/
function GAPI (api, version, spec) {
this.api = api;
this.version = version;
this.url = [ server, api, version, '' ].join('/');
createMethods(this, spec);
}
/**
* OAuth 2.0 Signatures
*/
function oauthHeader(options) {
if (!options.headers) { options.headers = {}; }
options.headers['Authorization'] = 'Bearer ' + GAPI.app.oauthToken.access_token;
}
function oauthParams(options) {
if (!options.params) { options.params = {}; }
options.params.access_token = GAPI.app.oauthToken.access_token;
}
/**
* HTTP Request Helper
*/
function request (config) {
var deferred = $q.defer();
oauthHeader(config);
function success(response) {
console.log(config, response);
deferred.resolve(response.data);
}
function failure(fault) {
console.log(config, fault);
deferred.reject(fault);
}
$http(config).then(success, failure);
return deferred.promise;
}
GAPI.request = request;
/**
* HTTP GET method available on service instance
*/
GAPI.prototype.get = function () {
var args = Array.prototype.slice.call(arguments)
, path = []
, params
;
args.forEach(function (arg, i) {
if (arg && typeof arg !== 'object') {
path.push(arg);
} else {
params = arg
}
});
return request({
method: 'GET',
url: this.url + path.join('/'),
params: params
});
};
/**
* HTTP POST method available on service instance
*/
GAPI.prototype.post = function () {
var args = Array.prototype.slice.call(arguments)
, path = []
, other = 0
, data
, params
;
args.forEach(function (arg, i) {
if (!arg || typeof arg === 'object') { // if the arg is not part of the path
other += 1; // increment the number of nonpath args
if (other === 1) { data = arg; }
if (other === 2) { params = arg; }
} else { // if the arg is defined and not and object
path.push(arg); // push to the path array
}
});
return request({
method: 'POST',
url: this.url + path.join('/'),
data: data,
params: params
});
}
/**
* Build a resource url, optionally with nested resources
*/
function resourceUrl (args, parents, base, resource) {
var argIndex = 0
, nodes = []
, params = args[args.length.toString()]
;
if (parents && parents.length > 0) {
parents.forEach(function (parent, i) {
nodes.push(parent, args[i.toString()])
argIndex += 1;
});
}
nodes.push(resource);
if (['string', 'number'].indexOf(typeof args[argIndex.toString()]) !== -1) {
nodes.push(args[argIndex.toString()]);
}
return base += nodes.join('/');
}
/**
* Parse params from arguments
*/
function parseParams (args) {
var last = args[(args.length - 1).toString()];
return (typeof last === 'object') ? last : null
}
/**
* Parse data and params from arguments
*/
function parseDataParams (a) {
var args = Array.prototype.slice.call(a)
, parsedArgs = {}
, other = 0
;
args.forEach(function (arg, i) {
if (!arg || typeof arg === 'object') {
other += 1;
if (other === 1) { parsedArgs.data = arg; }
if (other === 2) { parsedArgs.params = arg; }
}
});
return parsedArgs;
}
/**
* Resource methods
*
* These methods are used to construct a service.
* They are not intended to be called directly on GAPI.
*/
GAPI.get = function (resource, parents) {
return function () {
return request({
method: 'GET',
url: resourceUrl(arguments, parents, this.url, resource),
params: parseParams(arguments)
});
};
};
GAPI.set = function (resource, parents) {
return function () {
return request({
method: 'POST',
url: resourceUrl(arguments, parents, this.url, resource) + '/set',
params: parseParams(arguments)
});
};
};
GAPI.unset = function (resource, parents) {
return function () {
return request({
method: 'POST',
url: resourceUrl(arguments, parents, this.url, resource) + '/unset',
params: parseParams(arguments)
});
};
};
GAPI.list = function (resource, parents) {
return function () {
return request({
method: 'GET',
url: resourceUrl(arguments, parents, this.url, resource),
params: parseParams(arguments)
});
};
};
GAPI.insert = function (resource, parents) {
return function () {
var args = parseDataParams(arguments);
return request({
method: 'POST',
url: resourceUrl(arguments, parents, this.url, resource),
data: args.data,
params: args.params
});
};
};
GAPI.update = function (resource, parents) {
return function () {
var args = parseDataParams(arguments);
return request({
method: 'PUT',
url: resourceUrl(arguments, parents, this.url, resource),
data: args.data,
params: args.params
});
};
};
GAPI.patch = function (resource, parents) {
return function () {
var args = parseDataParams(arguments);
return request({
method: 'PATCH',
url: resourceUrl(arguments, parents, this.url, resource),
data: args.data,
params: args.params
});
};
};
GAPI.delete = function (resource, parents) {
return function () {
return request({
method: 'DELETE',
url: resourceUrl(arguments, parents, this.url, resource),
params: parseParams(arguments)
});
};
};
/**
* Authorization
*/
GAPI.init = function () {
var app = GAPI.app
, deferred = $q.defer();
gapi.load('auth', function () {
gapi.auth.authorize({
client_id: app.clientId,
scope: app.scopes,
immediate: false
}, function() {
app.oauthToken = gapi.auth.getToken();
deferred.resolve(app);
console.log('authorization', app)
});
});
return deferred.promise;
}
return GAPI;
})
/**
* Youtube API
*/
.factory('Youtube', function (GAPI) {
var Youtube = new GAPI('youtube', 'v3', {
activities: ['list', 'insert'],
channels: ['list', 'update'],
guideCategories: ['list'],
liveBroadcasts: ['list', 'insert', 'update', 'delete'],
liveStreams: ['list', 'insert', 'update', 'delete'],
playlistItems: ['list', 'insert', 'update', 'delete'],
playlists: ['list', 'insert', 'update', 'delete'],
subscriptions: ['list', 'insert', 'delete'],
thumbnails: ['set'],
videoCategories: ['list'],
videos: ['list', 'insert', 'update', 'delete'],
watermarks: ['set', 'unset']
});
// Some methods don't fit the pattern
// Define them explicitly here
Youtube.insertChannelBanners = function () {};
Youtube.bindLiveBroadcasts = function () {};
Youtube.controlLiveBroadcasts = function () {};
Youtube.transitionLiveBroadcasts = function () {};
Youtube.rateVideos = function (params) {
return Youtube.post('videos', 'rate', undefined, params);
};
Youtube.getVideoRating = function (params) {
return Youtube.get('videos', 'getRating', params);
};
Youtube.search = function (params) {
return Youtube.get('search', params);
}
return Youtube;
})
/**
* Blogger API
*/
.factory('Blogger', function (GAPI) {
var Blogger = new GAPI('blogger', 'v3', {
users: ['get'],
blogs: ['get', {
pages: ['list', 'get', 'insert', 'update', 'patch', 'delete'],
posts: ['list', 'get', 'insert', 'update', 'patch', 'delete', {
comments: ['list', 'get', 'delete']
}]
}]
});
Blogger.getBlogByUrl = function (params) {
return Blogger.get('blogs', 'byurl', params);
};
Blogger.listBlogsByUser = function (userId, params) {
return Blogger.get('users', userId, 'blogs', params);
};
Blogger.approveComments = function (blogId, postId, commentId) {
return Blogger.post('blogs', blogId, 'posts', postId, 'comments', commentId, 'approve');
};
Blogger.listCommentsByBlog = function (blogId, params) {
return Blogger.get('blogs', blogId, 'comments', params);
};
Blogger.markCommentsAsSpam = function (blogId, postId, commentId) {
return Blogger.post('blogs', blogId, 'posts', postId, 'comments', commentId, 'spam');
};
Blogger.removeContent = function (blogId, postId, commentId) {
return Blogger.post('blogs', blogId, 'posts', postId, 'comments', commentId, 'removecontent');
};
Blogger.searchPosts = function (blogId, params) {
return Blogger.get('blogs', blogId, 'posts/search', params);
};
Blogger.getPostsByPath = function (blogId, params) {
return Blogger.get('blogs', blogId, 'posts/bypath', params);
};
Blogger.publishPosts = function (blogId, postId, params) {
return Blogger.post('blogs', blogId, 'posts', postId, 'publish', undefined, params);
};
Blogger.revertPosts = function (blogId, postId) {
return Blogger.post('blogs', blogId, 'posts', postId, 'revert');
};
Blogger.getBlogUserInfos = function (userId, blogId, params) {
return Blogger.get('users', userId, 'blogs', blogId, params);
};
Blogger.getPageViews = function (blogId, params) {
return Blogger.get('blogs', blogId, 'pageviews', params);
};
Blogger.getPostUserInfos = function (userId, blogId, postId, params) {
return Blogger.get('users', userId, 'blogs', blogId, 'posts', postId, params);
};
Blogger.listPostUserInfos = function (userId, blogId, params) {
return Blogger.get('users', userId, 'blogs', blogId, 'posts', params);
};
return Blogger;
})
/**
* Calendar API
*/
.factory('Calendar', function (GAPI) {
var Calendar = new GAPI('calendar', 'v3', {
colors: ['get'],
calendars: ['get', 'insert', 'update', 'delete', 'patch', {
acl: ['list', 'get', 'insert', 'update', 'delete', 'patch'],
events: ['list', 'get', 'insert', 'update', 'delete', 'patch']
}],
'users/me/calendarList': ['list', 'get', 'insert', 'update', 'delete', 'patch'],
'users/me/settings': ['list', 'get']
});
Calendar.clearCalendar = function (id, params) {
return Calendar.post('calendars', id, 'clear', undefined, params);
};
Calendar.importEvents = function (calendarId, data, params) {
return Calendar.post('calendars', calendarId, 'events', 'import', data, params);
};
Calendar.moveEvents = function (calendarId, eventId, destinationId) {
return Calendar.post('calendars', calendarId, 'events', eventId, 'move', undefined, {
destination: destinationId
});
};
Calendar.listEventInstances = function (calendarId, eventId, params) {
return Calendar.get('calendars', calendarId, 'events', eventId, 'instances', params);
};
Calendar.quickAdd = function (id, params) {
return Calendar.post('calendars', id, 'events', 'quickAdd', undefined, params);
};
Calendar.watchEvents = function (id, data, params) {
return Calendar.post('calendars', id, 'events', 'watch', data, params);
};
Calendar.freeBusy = function (data) {
return Calendar.post('freeBusy', data);
}
Calendar.stopWatching = function (data) {
return Calendar.post('channels', 'stop', data)
};
return Calendar;
})
/**
* Drive API
*/
.factory('Drive', function (GAPI) {
var Drive = new GAPI('drive', 'v2', {
files: ['get', 'list', 'insert', 'update', 'delete', 'patch', {
children: ['get', 'list', 'insert', 'delete'],
parents: ['get', 'list', 'insert', 'delete'],
permissions: ['get', 'list', 'insert', 'update', 'delete', 'patch'],
revisions: ['get', 'list', 'update', 'delete', 'patch'],
comments: ['get', 'list', 'insert', 'update', 'delete', 'patch', {
replies: ['get', 'list', 'insert', 'update', 'delete', 'patch']
}],
properties: ['get', 'list', 'insert', 'update', 'delete', 'patch'],
realtime: ['get']
}],
changes: ['get', 'list'],
apps: ['get', 'list']
});
Drive.copyFile = function (fileId, data, params) {
return Drive.post('files', fileId, 'copy', data, params);
};
Drive.touchFile = function (fileId) {
return Drive.post('files', fileId, 'touch');
};
Drive.trashFile = function (fileId) {
return Drive.post('files', fileId, 'trash');
};
Drive.untrashFile = function (fileId) {
return Drive.post('files', fileId, 'untrash');
};
Drive.watchFile = function (fileId, data) {
return Drive.post('files', fileId, 'watch', data);
};
Drive.about = function (params) {
return Drive.get('about', params);
}
Drive.watchChanges = function (data) {
return Drive.post('changes', 'watch', data);
};
Drive.getPermissionIdForEmail = function (email) {
return Drive.get('permissionIds', email);
};
Drive.stopChannels = function (data) {
return Drive.post('channels', 'stop', data);
};
Drive.updateRealtime = function (fileId, params) {
return GAPI.request({
method: 'PUT',
url: Drive.url + ['files', fileId, 'realtime'].join('/'),
params: params
});
};
return Drive;
})
/**
* Google+ API
*/
.factory('Plus', function (GAPI) {
var Plus = new GAPI('plus', 'v1', {
people: ['get', {
activities: ['list']
}],
activities: ['get', {
comments: ['list']
}],
comments: ['get']
});
Plus.searchPeople = function (params) {
return Plus.get('people', params);
};
Plus.listPeopleByActivity = function (activityId, collection, params) {
return Plus.get('activities', activityId, 'people', collection, params);
};
Plus.listPeople = function (userId, collection, params) {
return Plus.get('people', userId, 'people', collection, params);
}
Plus.searchActivities = function (params) {
return Plus.get('activities', params);
};
Plus.insertMoments = function (userId, collection, data, params) {
return Plus.post('people', userId, 'moments', collection, data, params);
};
Plus.listMoments = function (userId, collection, params) {
return Plus.get('people', userId, 'moments', collection, params);
};
Plus.removeMoments = function (id) {
return GAPI.request({
method: 'DELETE',
url: Plus.url + ['moments', id].join('/')
});
};
return Plus;
})