forked from sritejakv/splitwise-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplitwise.java
More file actions
347 lines (321 loc) · 12.2 KB
/
Splitwise.java
File metadata and controls
347 lines (321 loc) · 12.2 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import com.github.scribejava.core.model.OAuth1AccessToken;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Token;
import com.github.scribejava.core.model.Verb;
import constants.Strings;
import constants.URL;
import utils.OAuthUtil;
import utils.Splitwise10Api;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
/**
* Java SDK for Splitwise. Authentication is done using OAuth1.0
*/
public class Splitwise {
private String consumerKey, consumerSecret;
protected OAuthUtil util;
/**
* Constructor.
* @param consumerKey consumerKey provided by splitwise
* @param consumerSecret consumerSecret provided by splitwise
*/
public Splitwise(String consumerKey, String consumerSecret){
this.consumerKey = consumerKey;
this.consumerSecret = consumerSecret;
this.util = new OAuthUtil().setApiKey(this.consumerKey)
.setApiSecret(this.consumerSecret)
.build(Splitwise10Api.instance());
}
/**
* Returns authorization url using which user can get token verifier.
* @return authorization url
* @throws InterruptedException
* @throws ExecutionException
* @throws IOException
*/
public String getAuthorizationUrl() throws InterruptedException, ExecutionException, IOException {
return this.util.getAuthorizationUrl();
}
/**
* Authenticates with splitwise with the verifier passed and returns the token details.
* @param verifier
* @return Token details provided by OAuth
* @throws InterruptedException
* @throws ExecutionException
* @throws IOException
*/
public Token getAccessToken(String verifier) throws InterruptedException, ExecutionException, IOException {
this.util.setAccessToken(verifier);
return this.util.getAccessToken();
}
/**
* Returns the JSON string of current user based on the consumerKey and consumerSecret.
* @return JSON string of user
* @throws Exception
*/
public String getCurrentUser() throws Exception {
Response response = this.util.makeRequest(String.format(URL.GET_CURRENT_USER, Strings.SPLITWISE_API_VERSION)
, Verb.GET);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Returns the JSON string of user based on the userId passed.
* @param userId Splitwise id of the user
* @return JSON string containing user details
* @throws Exception
*/
public String getUser(String userId) throws Exception {
String url = String.format(URL.GET_USER_WITH_ID, userId);
Response response = this.util.makeRequest(url, Verb.GET);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Update the parameters of the current user.
* @param id splitwise id of the user
* @param details details to be updated (http://dev.splitwise.com/?javascript#update_user-id)
* @return JSON response string from splitwise
* @throws Exception
*/
public String updateUser(String id, Map<String, String> details) throws Exception {
String url = String.format(URL.UPDATE_USER_WITH_ID, id);
Response response = this.util.makeRequest(url, Verb.POST, details);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Returns the JSON string of the splitwise groups of the current user.
* @return JSON string containing the groups
* @throws Exception
*/
public String getGroups() throws Exception {
Response response = this.util.makeRequest(URL.GET_GROUPS, Verb.GET);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Returns the JSON string of the group based on the group id passed.
* @param id splitwise group id
* @return JSON string containing group details
* @throws Exception
*/
public String getGroup(String id) throws Exception {
String url = String.format(URL.GET_GROUP_WITH_ID, id);
Response response = this.util.makeRequest(url, Verb.GET);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Creates a splitwise group.
* @param details details of the group (http://dev.splitwise.com/?javascript#create_group)
* @return JSON response from splitwise
* @throws Exception
*/
public String createGroup(Map<String, String> details) throws Exception {
Response response = this.util.makeRequest(URL.CREATE_GROUP_URL, Verb.POST, details);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Delete splitwise group based on the id.
* @param id splitwise group id
* @return JSON response from splitwise
* @throws Exception
*/
public String deleteGroup(String id) throws Exception {
String url = String.format(URL.DELETE_GROUP_WITH_ID, id);
Response response = this.util.makeRequest(url, Verb.POST);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Add user to a splitwise group.
* @param userDetails details of the user to be added (http://dev.splitwise.com/?javascript#add_user_to_group)
* @return JSON response from splitwise
* @throws Exception
*/
public String addUserToGroup(Map<String, String> userDetails) throws Exception {
Response response = this.util.makeRequest(URL.ADD_USER_TO_GROUP, Verb.POST, userDetails);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Remove user from a splitwise group.
* @param groupId splitwise group id
* @param userId splitwise user id
* @return JSON response from splitwise
* @throws Exception
*/
public String removeUserFromGroup(final String groupId, final String userId) throws Exception {
Map<String, String> details = new HashMap<String, String>(){{
put(Strings.USER_ID, userId);
put(Strings.GROUP_ID, groupId);
}};
Response response = this.util.makeRequest(URL.REMOVE_USER_FROM_GROUP, Verb.POST, details);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Returns splitiwse friends of the current user.
* @return JSON string containing friends
* @throws Exception
*/
public String getFriends() throws Exception {
Response response = this.util.makeRequest(URL.GET_FRIENDS, Verb.GET);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Returns splitwise friend details based on the id passed.
* @param id splitwise user id of the friend
* @return JSON string containing friend details
* @throws Exception
*/
public String getFriend(String id) throws Exception {
String url = String.format(URL.GET_FRIEND, id);
Response response = this.util.makeRequest(url, Verb.GET);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Create a splitwise friend
* @param firstName first name of the friend
* @param lastName last name of the friend
* @param email email of the friend
* @return JSON response from splitwise
* @throws Exception
*/
public String createFriend(final String firstName, final String lastName, final String email) throws Exception {
Map<String, String> friendDetails = new HashMap<String, String>(){{
put(Strings.USER_FIRST_NAME, firstName);
put(Strings.USER_LAST_NAME, lastName);
put(Strings.USER_EMAIL, email);
}};
Response response = this.util.makeRequest(URL.CREATE_FRIEND, Verb.POST, friendDetails);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Delete a friend from splitwise.
* @param friendId splitwise user id
* @return JSON response string from splitwise
* @throws Exception
*/
public String deleteFriend(String friendId) throws Exception {
String url = String.format(URL.DELETE_FRIEND_WITH_ID, friendId);
Response response = this.util.makeRequest(url, Verb.POST);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Get splitwise comments on an expense.
* @param expenseId splitwise expense id
* @return JSON string of comments on the expense
* @throws Exception
*/
public String getComments(String expenseId) throws Exception {
String url = String.format(URL.GET_COMMENTS_FOR_EXPENSE, expenseId);
Response response = this.util.makeRequest(url, Verb.GET);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Get all expenses for the current user.
* @return JSON string containing user expenses
* @throws Exception
*/
public String getExpenses() throws Exception {
Response response = this.util.makeRequest(URL.GET_EXPENSES, Verb.GET);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Get user expense for an expense id.
* @param expenseId splitwise expense id
* @return JSON string containing expense details
* @throws Exception
*/
public String getExpense(String expenseId) throws Exception {
String url = String.format(URL.GET_EXPENSE_WITH_ID, expenseId);
Response response = this.util.makeRequest(url, Verb.GET);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Delete splitwise expense of current user
* @param expenseId splitwise expense id
* @return JSON string response from splitwise
* @throws Exception
*/
public String deleteExpense(String expenseId) throws Exception {
String url = String.format(URL.DELETE_EXPENSE_WITH_ID, expenseId);
Response response = this.util.makeRequest(url, Verb.GET);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Get unseen notifications of the current user.
* @return JSON string response containing user notifications
* @throws Exception
*/
public String getNotifications() throws Exception {
Response response = this.util.makeRequest(URL.GET_NOTIFICATIONS, Verb.GET);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Get currencies supported by splitwise
* @return JSON string containing splitwise currencies
* @throws Exception
*/
public String getCurrencies() throws Exception {
Response response = this.util.makeRequest(URL.GET_CURRENCIES, Verb.GET);
if (response.getCode() == 200)
return response.getBody();
return null;
}
/**
* Get categories supported by splitwise
* @return JSON string containing splitwise categories
* @throws Exception
*/
public String getCategories() throws Exception {
Response response = this.util.makeRequest(URL.GET_CATEGORIES, Verb.GET);
if (response.getCode() == 200)
return response.getBody();
return null;
}
public static void main(String... args) throws Exception {
Splitwise splitwise = new Splitwise("<consumerKey>",
"<consumerSecret>");
String authorizationURL = splitwise.getAuthorizationUrl();
splitwise.util.setAccessToken("<verifier_from_authorizationURL>");
OAuth1AccessToken accessToken = (OAuth1AccessToken) splitwise.util.getAccessToken();
splitwise.util.setAccessToken(accessToken.getToken(),
accessToken.getTokenSecret(),
accessToken.getRawResponse()
);
System.out.println("Response \n" + splitwise.getCurrentUser());
}
}