Skip to content

Add a method for calling the /create_expense API #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/main/java/Splitwise.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ public String getUser(String userId) throws Exception {
/**
* 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)
* @param details details to be updated
* @return JSON response string from splitwise
* @throws Exception
* @see <a href="https://dev.splitwise.com/#tag/users/paths/~1update_user~1{id}/post">Splitwise API
* documentation</a> for required and optional parameters
*/
public String updateUser(String id, Map<String, String> details) throws Exception {
String url = String.format(URL.UPDATE_USER_WITH_ID, id);
Expand Down Expand Up @@ -126,9 +128,11 @@ public String getGroup(String id) throws Exception {

/**
* Creates a splitwise group.
* @param details details of the group (http://dev.splitwise.com/?javascript#create_group)
* @param details details of the group
* @return JSON response from splitwise
* @throws Exception
* @see <a href="https://dev.splitwise.com/#tag/groups/paths/~1create_group/post">Splitwise API
* documentation</a> for required and optional parameters
*/
public String createGroup(Map<String, String> details) throws Exception {
Response response = this.util.makeRequest(URL.CREATE_GROUP_URL, Verb.POST, details);
Expand All @@ -153,9 +157,11 @@ public String deleteGroup(String id) throws Exception {

/**
* Add user to a splitwise group.
* @param userDetails details of the user to be added (http://dev.splitwise.com/?javascript#add_user_to_group)
* @param userDetails details of the user to be added
* @return JSON response from splitwise
* @throws Exception
* @see <a href="https://dev.splitwise.com/#tag/groups/paths/~1add_user_to_group/post">Splitwise API
* documentation</a> for required and optional parameters
*/
public String addUserToGroup(Map<String, String> userDetails) throws Exception {
Response response = this.util.makeRequest(URL.ADD_USER_TO_GROUP, Verb.POST, userDetails);
Expand All @@ -173,8 +179,8 @@ public String addUserToGroup(Map<String, String> userDetails) 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);
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)
Expand Down Expand Up @@ -256,6 +262,21 @@ public String getComments(String expenseId) throws Exception {
return null;
}

/**
* Creates a splitwise expense.
* @param details details of the expense
* @return JSON response from splitwise
* @throws Exception
* @see <a href="https://dev.splitwise.com/#tag/expenses/paths/~1create_expense/post">Splitwise API
* documentation</a> for required and optional parameters
*/
public String createExpense(Map<String, String> details) throws Exception {
Response response = this.util.makeRequest(URL.CREATE_EXPENSE, Verb.POST, details);
if (response.getCode() == 200)
return response.getBody();
return null;
}

/**
* Get all expenses for the current user.
* @return JSON string containing user expenses
Expand Down
1 change: 1 addition & 0 deletions src/main/java/constants/URL.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class URL {
public static final String DELETE_FRIEND_WITH_ID = BASE_URL + "/delete_friend/%s";

//URLs for Expenses API
public static final String CREATE_EXPENSE = BASE_URL + "/create_expense";
public static final String GET_EXPENSES = BASE_URL + "/get_expenses";
public static final String GET_EXPENSE_WITH_ID = BASE_URL + "/get_expense/%s";
public static final String DELETE_EXPENSE_WITH_ID = BASE_URL + "/delete_expense/%s";
Expand Down