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
|
import request = require('request-promise');
import { Album } from './typings/albums';
import * as qs from 'query-string';
const apiEndpoint = "https://photoslibrary.googleapis.com/v1/";
export interface Authorization {
token: string;
}
export namespace GooglePhotos {
export type Query = Album.Query;
export type QueryParameters = { query: GooglePhotos.Query };
interface DispatchParameters {
required: boolean;
method: "GET" | "POST";
ignore?: boolean;
}
export const ExecuteQuery = async (parameters: Authorization & QueryParameters): Promise<any> => {
let action = parameters.query.action;
let dispatch = SuffixMap.get(action)!;
let suffix = Suffix(parameters, dispatch, action);
if (suffix) {
let query: any = parameters.query;
let options: any = {
headers: { 'Content-Type': 'application/json' },
auth: { 'bearer': parameters.token },
};
if (query.body) {
options.body = query.body;
options.json = true;
}
let queryParameters = query.parameters;
if (queryParameters) {
suffix += `?${qs.stringify(queryParameters)}`;
}
let dispatcher = dispatch.method === "POST" ? request.post : request.get;
return dispatcher(apiEndpoint + suffix, options);
}
};
const Suffix = (parameters: QueryParameters, dispatch: DispatchParameters, action: Album.Action) => {
let query: any = parameters.query;
let id = query.albumId;
let suffix = 'albums';
if (dispatch.required) {
if (!id) {
return undefined;
}
suffix += `/${id}${dispatch.ignore ? "" : `:${action}`}`;
}
return suffix;
};
const SuffixMap = new Map<Album.Action, DispatchParameters>([
[Album.Action.AddEnrichment, { required: true, method: "POST" }],
[Album.Action.BatchAddMediaItems, { required: true, method: "POST" }],
[Album.Action.BatchRemoveMediaItems, { required: true, method: "POST" }],
[Album.Action.Create, { required: false, method: "POST" }],
[Album.Action.Get, { required: true, ignore: true, method: "GET" }],
[Album.Action.List, { required: false, method: "GET" }],
[Album.Action.Share, { required: true, method: "POST" }],
[Album.Action.Unshare, { required: true, method: "POST" }]
]);
}
|