-
Notifications
You must be signed in to change notification settings - Fork 157
/
photos.js
168 lines (141 loc) 路 3.53 KB
/
photos.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
/* @flow */
import { getUrlComponents } from "../utils";
import get from "lodash.get";
export default function photos(): Object {
return {
listPhotos: (page = 1, perPage = 10, orderBy = "latest") => {
const url = "/photos";
const query = {
page,
per_page: perPage,
order_by: orderBy
};
return this.request({
url,
method: "GET",
query
});
},
listCuratedPhotos: (page = 1, perPage = 10, orderBy = "latest") => {
const url = "/photos/curated";
const query = {
page,
per_page: perPage,
order_by: orderBy
};
return this.request({
url,
method: "GET",
query
});
},
searchPhotos: (q, category = [""], page = 1, perPage = 10) => {
const url = "/photos/search";
const query = {
query: q,
category: category.length > 1
? category.join(",")
: category.toString(),
page,
per_page: perPage
};
return this.request({
url,
method: "GET",
query
});
},
getPhoto: (id, width, height, rectangle) => {
const url = `/photos/${id}`;
const query = {
w: width,
h: height,
rect: rectangle
};
return this.request({
url,
method: "GET",
query
});
},
getPhotoStats: (id) => {
const url = `/photos/${id}/stats`;
return this.request({
url,
method: "GET"
});
},
getRandomPhoto: (options = {}) => {
const url = "/photos/random";
const category = options.category || [];
const collections = options.collections || [];
const query = {
featured: options.featured,
username: options.username,
orientation: options.orientation,
category: category.join(),
collections: collections.join(),
query: options.query,
w: options.width,
h: options.height,
c: options.cacheBuster || new Date().getTime(), // Avoid ajax response caching
count: options.count
};
Object.keys(query).forEach(key => {
if (!query[key]) {
delete query[key];
}
});
return this.request({
url,
method: "GET",
query
});
},
uploadPhoto: (photo) => {
if (!this._bearerToken) {
throw new Error("Requires a bearerToken to be set.");
}
const url = "/photos";
return this.request({
url,
method: "POST",
body: {
photo: photo
}
});
},
likePhoto: (id) => {
if (!this._bearerToken) {
throw new Error("Requires a bearerToken to be set.");
}
const url = `/photos/${id}/like`;
return this.request({
url,
method: "POST"
});
},
unlikePhoto: (id) => {
if (!this._bearerToken) {
throw new Error("Requires a bearerToken to be set.");
}
const url = `/photos/${id}/like`;
return this.request({
url,
method: "DELETE"
});
},
downloadPhoto: (photo) => {
const downloadLocation = get(photo, "links.download_location", undefined);
if (downloadLocation === undefined) {
throw new Error(`Object received is not a photo. ${photo}`);
}
const urlComponents = getUrlComponents(downloadLocation);
return this.request({
url: urlComponents.pathname,
method: "GET",
query: urlComponents.query
});
}
};
}