forked from CGUC/skybunk-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiClient.js
More file actions
154 lines (136 loc) · 3.79 KB
/
ApiClient.js
File metadata and controls
154 lines (136 loc) · 3.79 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
var config = require('./config');
import {AsyncStorage} from 'react-native';
var token;
export default class ApiClient {
static async formatHeaders(options){
const contentType = options.contentType ? options.contentType : 'application/json'
if(options.authorized){
return {
'Accept': 'application/json',
'Content-Type': contentType,
'Authorization': 'Bearer ' + await this.getAuthToken(),
...options.headers
}
}
else {
return {
'Accept': 'application/json',
'Content-Type': contentType,
...options.headers
}
}
}
static async getAuthToken(){
if(token != undefined) return token;
token = await AsyncStorage.getItem('@Skybunk:token');
return token;
}
static async setAuthToken(_token){
token = _token;
await AsyncStorage.setItem('@Skybunk:token', token);
}
static async clearAuthToken(){
await AsyncStorage.removeItem('@Skybunk:token');
token = undefined;
}
static async get(endpoint, options={}) {
return fetch(`${config.API_ADDRESS}${endpoint}`, {
method: 'GET',
headers: await this.formatHeaders(options),
})
.then(response => response.json())
.then(responseJSON => {
return responseJSON;
})
.catch(err => {
err = err.replace(/</g, '').replace(/>/g, '');
console.error(err);
});
}
static async post(endpoint, body, options={}) {
return fetch(`${config.API_ADDRESS}${endpoint}`, {
method: 'POST',
headers: await this.formatHeaders(options),
body: JSON.stringify(body),
})
.catch(err => {
err = err.replace(/</g, '').replace(/>/g, '');
console.error(err);
});
};
static async put(endpoint, body, options={}) {
/**
* HACKFIX (Neil): Sending too many notification objects with requests has
* returned 413s and crashed the app. Here we're limiting the saved notifications to 30.
* This logic doesn't belong client-side, but putting it here should neutralize the bug for now.
*/
if (body.notifications) {
console.log("Trimming notifications...");
body.notifications = body.notifications.slice(0, 30);
} else console.log("No notifications being sent");
return fetch(`${config.API_ADDRESS}${endpoint}`, {
method: 'PUT',
headers: await this.formatHeaders(options),
body: JSON.stringify(body),
})
.then(response => {
return response.json()
})
.then(responseJSON => {
return responseJSON
})
.catch(err => {
err = err.replace(/</g, '').replace(/>/g, '');
console.error(err);
});
}
static async uploadPhoto(endpoint, uri, name, options={}) {
const method = options.method ? options.method : 'PUT'
let uriParts = uri.split('.');
let fileType = uriParts[uriParts.length - 1];
let formData = new FormData();
formData.append(name, {
uri,
name: `${name}.${fileType}`,
type: `image/${fileType}`,
});
return fetch(`${config.API_ADDRESS}${endpoint}`, {
method: method,
headers: await this.formatHeaders({...options, contentType: 'multipart/form-data'}),
body: formData,
})
.then(response => {
return response.json();
})
.then(responseJSON => responseJSON)
.catch(err => {
err = err.replace(/</g, '').replace(/>/g, '');
console.error(err);
});
}
static async delete(endpoint, options={}) {
return fetch(`${config.API_ADDRESS}${endpoint}`, {
method: 'DELETE',
headers: await this.formatHeaders(options)
})
.catch(err => {
err = err.replace(/</g, '').replace(/>/g, '');
console.error(err);
});;
}
static makeCancelable(promise) {
let hasCanceled_ = false;
const wrappedPromise = new Promise((resolve, reject) => {
promise.then(
val => hasCanceled_ ? reject({isCanceled: true}) : resolve(val),
error => hasCanceled_ ? reject({isCanceled: true}) : reject(error)
);
});
return {
promise: wrappedPromise,
cancel() {
hasCanceled_ = true;
},
};
};
}