forked from RobinBobin/react-native-google-drive-api-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GDrive.js
59 lines (46 loc) · 1.56 KB
/
GDrive.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
import Files from "./Files";
import Permissions from "./Permissions";
import {
StaticUtils,
ArrayStringifier
} from "simple-common-utils";
export default class GDrive {
static _urlFiles = "https://www.googleapis.com/drive/v3/files";
static _contentTypeJson = "application/json; charset=UTF-8";
static init(params = {}) {
GDrive.files = new Files(params.files);
GDrive.permissions = new Permissions();
}
static setAccessToken(accessToken) {
GDrive.accessToken = accessToken;
}
static isInitialized() {
return !!GDrive.accessToken;
}
static _createHeaders(contentType, contentLength, ... additionalPairs) {
let pairs = [
[ "Authorization", `Bearer ${GDrive.accessToken}` ]
];
[
[ "Content-Type", contentType] ,
[ "Content-Length", contentLength ]
].forEach(data => data[1] ? pairs.push(data) : undefined);
if (additionalPairs) {
pairs = pairs.concat(additionalPairs);
}
const headers = new Headers();
for (let pair of pairs) {
headers.append(pair[0], pair[1]);
}
return headers;
}
static _stringifyQueryParams(queryParams, prefix = "?", separator = "&", quoteIfString) {
const array = [];
Object.keys(queryParams).forEach(key => array.push(
`${key}=${StaticUtils.safeQuoteIfString(queryParams[key], quoteIfString)}`));
return new ArrayStringifier(array)
.setPrefix(prefix)
.setSeparator(separator)
.process();
}
}