Repository navigation
Expand file tree
/
Copy pathTheCatAPI.js
More file actions
314 lines (289 loc) · 7.92 KB
/
Copy pathTheCatAPI.js
File metadata and controls
314 lines (289 loc) · 7.92 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
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
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex);
const SET_IMAGES = 'SET_IMAGES';
const SET_SUB_ID = 'SET_SUB_ID';
const SET_CATEGORIES = 'SET_CATEGORIES';
const SET_BREEDS = 'SET_BREEDS';
const SET_API_KEY = 'SET_API_KEY';
const SET_LAST_FAVOURITED = 'SET_LAST_FAVOURITED';
const SET_FAVOURITES = 'SET_FAVOURITES';
axios.defaults.baseURL = process.env.VUE_APP_API_ENDPOINT;// "https://api.thedogapi.com/"
//axios.defaults.baseURL = "http://localhost:4009"
axios.interceptors.response.use(null, function(error) {
console.warn('Error status', error.response.status);
return Promise.reject(error);
});
const applySubID = function (sub_id, params)
{
if(!params)params = {}
if(sub_id)params.sub_id = sub_id
return params;
}
const createDataForm = function(data) {
let formData = new FormData();
for(let prop in data){
formData.append(prop, data[prop]);
}
return formData;
}
const merchants = {
namespaced: true,
state: {
sub_id: 'demo-'+((1<<24)*Math.random()|0).toString(16), // Default value
images: null,
api_key: "DEMO-API-KEY", // Swap this out with the one from the email sent after signing up
last_favourited: null,
favourites:null,
categories: [],
breeds:[]
},
mutations: {
SET_CATEGORIES(state, data){
state.categories = data
},
SET_SUB_ID(state, data){
state.sub_id = data
},
SET_IMAGES(state, data) {
state.images = data
},
SET_API_KEY(state, data) {
state.api_key = data
},
SET_LAST_FAVOURITED(state, data) {
state.last_favourited = data
},
SET_FAVOURITES(state, data) {
state.favourites = data
},
SET_BREEDS(state, data) {
state.breeds = data
}
},
getters: {
images(state) {
return state.images
},
apiKey(state) {
return state.api_key
},
subID(state) {
return state.sub_id;
},
lastFavourited(state) {
return state.last_favourited;
},
categories(state) {
return state.categories;
}
},
actions: {
async setAPIKey({
dispatch,
commit
}, key) {
try {
commit(SET_API_KEY, key)
} catch (error) {
throw new Error(error)
}
},
async setSubID({
dispatch,
commit
}, sub_id) {
console.log("SetSubID", sub_id)
try {
commit(SET_SUB_ID, sub_id)
} catch (error) {
throw new Error(error)
}
},
getSubID({
dispatch,
rootGetters
}) {
try {
return rootGetters['TheCatAPI/subID']
} catch (error) {
throw new Error(error)
}
},
async searchImages({
dispatch,
rootGetters,
commit
}, query) {
try {
// add x-api-key as header
axios.defaults.headers.common['x-api-key'] = rootGetters['TheCatAPI/apiKey']
let response = await axios.get('/v1/images/search',{
params: applySubID(rootGetters['TheCatAPI/subID'], query)
}
);
commit(SET_IMAGES,response.data)
return response;
} catch (error) {
throw new Error(error)
}
},
async getImage({
dispatch,
rootGetters
}, query) {
try {
// add x-api-key as header
axios.defaults.headers.common['x-api-key'] = rootGetters['TheCatAPI/apiKey']
let response = await axios.get('/v1/images/'+query.image_id, {
params: applySubID(rootGetters['TheCatAPI/subID'], query)
});
return response;
} catch (error) {
throw new Error(error.response.data.message)
}
},
async getCategories({
dispatch,
rootGetters,
commit
}, query) {
try {
let response = await axios.get('/v1/categories/');
commit(SET_CATEGORIES,response.data)
return response;
} catch (error) {
throw new Error(error.response.data.message)
}
},
async getUploads({
dispatch,
rootGetters
}, query) {
try {
// add x-api-key as header
axios.defaults.headers.common['x-api-key'] = rootGetters['TheCatAPI/apiKey']
let response = await axios.get('/v1/images/', {
params: applySubID(rootGetters['TheCatAPI/subID'], query)
});
return response;
} catch (error) {
throw new Error(error.response.data.message)
}
},
async voteImage({
dispatch,
rootGetters
}, params) {
try {
// add x-api-key as header
axios.defaults.headers.common['x-api-key'] = rootGetters['TheCatAPI/apiKey']
let response = await axios.post('/v1/votes/',
applySubID(rootGetters['TheCatAPI/subID'], params)
);
return response;
} catch (error) {
throw new Error(error.response.data.message)
}
},
async uploadImage({
dispatch,
rootGetters
}, params) {
try {
// add x-api-key as header
axios.defaults.headers.common['x-api-key'] = rootGetters['TheCatAPI/apiKey']
params = applySubID(rootGetters['TheCatAPI/subID'], params)
let dataForm = createDataForm(params);
let response = await axios.post('/v1/images/upload',dataForm,{
headers: {
'Content-Type':'multipart/form-data'
}
});
return response;
} catch (error) {
throw new Error(error.response.data.message)
}
},
async getFavourites({
dispatch,
rootGetters,
commit
}, query) {
try {
// add x-api-key as header
axios.defaults.headers.common['x-api-key'] = rootGetters['TheCatAPI/apiKey']
let response = await axios.get('/v1/favourites',{
params: applySubID(rootGetters['TheCatAPI/subID'], query)
}
);
commit(SET_FAVOURITES,response.data)
return response;
} catch (error) {
throw new Error(error)
}
},
async favouriteImage({
dispatch,
rootGetters,
commit
}, params) {
try {
// add x-api-key as header
axios.defaults.headers.common['x-api-key'] = rootGetters['TheCatAPI/apiKey']
let response = await axios.post('/v1/favourites/',
applySubID(rootGetters['TheCatAPI/subID'], params)
);
commit(SET_LAST_FAVOURITED, params.image_id)
return response;
} catch (error) {
throw new Error(error.response.data.message)
}
},
async unFavouriteImage({
dispatch,
rootGetters,
commit
}, params) {
try {
// add x-api-key as header
axios.defaults.headers.common['x-api-key'] = rootGetters['TheCatAPI/apiKey']
let response = await axios.delete('/v1/favourites/'+params.favourite_id
);
commit(SET_LAST_FAVOURITED, params.image_id)
return response;
} catch (error) {
throw new Error(error)
}
},
async signup({
dispatch,
rootGetters
}, params) {
try {
// this is rate limited on the server and uses a dynamic env var to prevent being spammed so it probably won't work well/ if at all outisde of theCatAPI.com / theDogAPI.com
let response = await axios.post('/v1/user/passwordlesssignup', params);
return response;
} catch (error) {
throw new Error(error.response.data.message)
}
},
async getBreeds({
dispatch,
rootGetters,
commit
}, query) {
try {
// add x-api-key as header
axios.defaults.headers.common['x-api-key'] = rootGetters['TheCatAPI/apiKey']
let response = await axios.get('/v1/breeds', {params: query});
commit(SET_BREEDS,response.data)
return response;
} catch (error) {
throw new Error(error)
}
},
}
}
export default merchants