Splitwise Python SDK
Project description
Splitwise Python SDK
This is the python sdk for Splitwise APIs. Pull requests and bug reports are welcomed.
Latest Version
The latest version of splitwise SDK is Splitwise-3.0.0
Docs
The detailed docs are hosted at readthedocs.org
Installation
Install using pip :
$ pip install splitwise
Register your application
Register your application on splitwise and get your consumer key and consumer secret.
Include splitwise in your application
from splitwise import Splitwise
Get splitwise instance
To get an instance to splitwise just provide the consumer key and secret.
sObj = Splitwise("<consumer key>","<consumer secret>")
Debug
To get the debug logs use
import logging
logging.basicConfig(level=logging.DEBUG)
Usage
Authorize splitwise
Before you can make call to splitwise, you need to get access token of the user on whose behalf you will be making call. Think of it as login with splitwise. Its based on OAuth and its a 2 step process.
OAuth1
-
Get the Authorize URL and Secret. Redirect the user to the Authorize url and store the secret in somewhere for eg in session.
sObj = Splitwise("<consumer key>","<consumer secret>") url, secret = sObj.getAuthorizeURL() #Store secret so you can retrieve it later #redirect user to url
-
After authorization splitwise will redirect the user back to the callback url that you provided during registration. It will also provide oauth_token and oauth_verifier that can be used along with secret from step 1 to get access token. The below snippet is from Flask application to extract parameters and then using SDK to get access token. This access token can be stored in your db to make calls on his/her behalf.
oauth_token = request.args.get('oauth_token') oauth_verifier = request.args.get('oauth_verifier') sObj = Splitwise(Config.consumer_key,Config.consumer_secret) access_token = sObj.getAccessToken(oauth_token,session['secret'],oauth_verifier) session['access_token'] = access_token
OAuth2
It is now possible to use OAuth2 with Splitwise. For details you can refer to readthedocs.org
API Key
You can use API Key provided by Splitwise to test APIs for your user.
sObj = Splitwise("<consumer key>","<consumer secret>",api_key="<api key>")
current = sObj.getCurrentUser()
Get data from splitwise
Once you have the access token you can make the calls to splitwise. Get the splitwise instance and set the access token and then make authorized calls.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
sObj.getFriends()
Get Current User
You can use getCurrentUser()
to get the current user. It returns a CurrentUser
object.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
sObj.getCurrentUser()
Get User
You can use getUser(id)
to get the user. It returns a User
object.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
id = 7123
user = sObj.getUser(id)
Update User
You can use updateUser(user)
to update the user. It takes in a partial CurrentUser
object
with atleast id
set. It returns a CurrentUser
object.
Note that you can update anything for your user and first_name
, last_name
and email
for
any acquaintances who has not created account yet.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
user = User()
user.setId(10)
user.setFirstName("naman")
updated_user, error = sObj.updateUser(user)
print(updated_user.getFirstName())
Get Friends
You can use getFriends()
to get all the friends of the current user along with the balances. It returns a list of Friend
objects.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
sObj.getFriends()
Get Groups
You can use getGroups()
to get all the groups of the current user along with the members and balances. It returns a list of Group
objects.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
sObj.getGroups()
Get Currencies
You can use getCurrencies()
to get all the currencies supported by splitwise. It returns a list of Currency
objects.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
sObj.getCurrencies()
Get Category
You can use getCategories()
to get all the categories and sub categories provided by splitwise. It returns a list of Category
objects.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
sObj.getCategories()
Get Group
You can use getGroup(id)
to get the particular group of the current user along with the members and balances. It returns a Group
object. Use id as 0 to get all non group expenses.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
sObj.getGroup(43233)
Get Expenses
You can use getExpenses(offset,limit,group_id,friend_id,dated_after,dated_before,updated_after,updated_before,visible)
to get all the expenses of the current user based on filter options. It returns a list of Expense
objects.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
sObj.getExpenses()
Get Expense
You can use getExpense(id)
to get the particular expense of the current user. It returns a Expense
object.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
sObj.getExpense(43233)
Create Expense
You can use createExpense(Expense)
to create a new Expense. It takes in parameter a partial Expense
object and returns an Expense
object.
Following things need to be set on the Expense
object.
- Cost
- Description
- Users - Should be a list of
ExpenseUser
with id and paidShare and owedShare set.
from splitwise.expense import Expense
from splitwise.user import ExpenseUser
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
expense = Expense()
expense.setCost('10')
expense.setDescription("Testing")
expense.setReceipt("/Users/naman/receipt.jpg")
user1 = ExpenseUser()
user1.setId(79774)
user1.setPaidShare('10.00')
user1.setOwedShare('2.0')
user2 = ExpenseUser()
user2.setId(281236)
user2.setPaidShare('0.00')
user2.setOwedShare('8.00')
users = []
users.append(user1)
users.append(user2)
expense.setUsers(users)
expense, errors = sObj.createExpense(expense)
print expense.getId()
Create Group
You can use createGroup(Group)
to create a new Group. It takes in parameter a partial Group
object and returns an Group
object.
Following things need to be set on the Group
object.
- Name
- Users - Should be a list of
User
with either FirstName, LastName and Email or just Id set.
from splitwise.group import Group
from splitwise.user import User
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
group = Group()
group.setName("Testing")
user1 = User()
user1.setId(79774)
user2 = User()
user2.setId(281236)
users = []
users.append(user1)
users.append(user2)
group.setMembers(users)
group, errors = sObj.createGroup(group)
print group.getId()
Add user to group
You can use addUserToGroup(User, group_id)
to add user to group. It takes in a splitwise.user.User
object that has either id
or firstName
and email
set and a group_id
.
from splitwise.group import Group
from splitwise.user import User
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
user = User()
user.setId(1223)
success, user, errors = sObj.addUserToGroup(user, 4456)
print(success)
Delete group
You can use deleteGroup(group_id)
to delete an existing group.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
success, errors = sObj.deleteGroup(4456)
print(success)
Update Expense
You can use updateExpense(Expense)
to update an existing Expense. It takes in parameter a partial Expense
object and returns an Expense
object.
Following things need to be set on the Expense
object.
- Id
- any field you would want to update
from splitwise.expense import Expense
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
expense = Expense()
expense.id = 12345678
expense.setCost('10')
expense.setDescription("Updated description")
expense, errors = sObj.updateExpense(expense)
print(expense.getId())
or update the fetched expense
from splitwise.expense import Expense
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
expense, error = sObj.getExpense(12345)
expense.setDescription("Updated description")
expense, errors = sObj.updateExpense(expense)
print(expense.getDescription())
Delete expense
You can use deleteExpense(expense_id)
to delete an existing expense.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
success, errors = sObj.deleteExpense(4456)
print(success)
Get Comments
You can use getComments(id)
to get the comments made on an expense. It returns an array of Comment
object.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
id = 982430660
comments = sObj.getComments(id)
Create Comment
You can use createComment(Comment)
to create a new Comment. It takes in parameters expense_id and content and returns a Comment
object.
Following are the parameters passed.
- expense_id
- content
from splitwise import Splitwise
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
expense_id = 982430660
content = "Test for create comment"
comment, errors = sObj.createComment(expense_id,content)
print("content:", comment.getContent())
print("errors:", errors)
Get Notifications
You can use getNotifications()
to get recent Notifications. It returns an array of Notification
object.
sObj = Splitwise(Config.consumer_key,Config.consumer_secret)
sObj.setAccessToken(session['access_token'])
id = 982430660
notifications = sObj.getNotifications()
Objects
User
Methods:
- getId() - Returns the id of the user
- getFirstName() - Returns the first name of user
- getLastName() - Returns the last name of user
- getEmail() - Returns the email of the user
- getRegistrationStatus() - Returns the registraion status of the user
- getPicture() - Returns a
Picture
object containing picture details - setId(id) - Sets the id of the user
- setFirstName(first_name) - Sets the first name of user
- setLastName(last_name) - Sets the last name of user
- setEmail(email) - Sets the email of the user
CurrentUser
Current user is inherited from User
. All the methods of user are available for current user.
Methods:
- getDefaultCurrency() - Returns the default currency
- getLocale() - Returns the locale
- getDateFormat() - Returns the date format
- getDefaultGroupId() - Returns the default group id of user
Friend
Friend is inherited from User
. All the methods of user are available for friend.
Methods:
- getUpdatedAt() - Gets the time when this user information was last updated
- getBalances() - Returns a list of
Balance
objects - getGroups() - Returns a list of
FriendGroup
objects
Expense User
ExpenseUser is inherited from User
. All the methods of user are available for Expense User.
Methods:
- getPaidShare() - Returns the Paid Share
- getOwedShare() - Returns the Owed Share
- getNetBalance() - Returns the Net Balance
- setPaidShare(paid_share) - Sets the Paid Share
- setOwedShare(owed_share) - Sets the Owed Share
Group
Methods:
- getId() - Returns the id of the group
- getName() - Returns the name of the group
- getUpdatedAt() - Get the time this group was last updated
- getWhiteBoard() - Get the whiteboard contents of this group
- isSimplifiedByDefault() - Returns if group is simplified by default or not
- getMembers() - Returns a list of
Friend
objects - getOriginalDebts() - Returns a list of
Debt
objects - getType() - Returns the type of group
- getGroupType() - Returns the type of group
- getSimplifiedDebts() - Returns a list of
Debt
objects - getInviteLink() - Returns the invite link
- setName(name) - Sets the name of the group
- setCountryCode(code) - Sets the country code of the group
- setWhiteBoard(text) - Sets the whiteboard contents of this group
- isSimplifiedByDefault(bool) - Sets if group is simplified by default or not
- setMembers(users) - Sets a list of
Friend
objects - addMember(user) - Add to a list of
Friend
objects - setType(type) - Sets the type of group
- setGroupType(type) - Sets the type of group
FriendGroup
Methods:
- getId() - Returns the id of the group
- getBalances() - Returns a list of
Balance
object - setId(id) - sets the id of the group
Balance
Methods:
- getCurrencyCode() - Returns the currency code.
- getAmount() - Returns the amount
Category
Methods:
- getSubcategories() - Returns a list of
Category
objects - getId() - Returns the id of category
- getName() - Returns the name of the category
Currency
Methods:
- getCode() - Returns the Currency Code
- getUnit() - Returns the Currency Unit
Debt
Methods:
- getFromUser() - Returns the id of the from user
- getToUser() - Returns the id of the to user
- getAmount() - Returns the amount of the debt
- getCurrencyCode() - Returns the currency code of debt
Expense
Methods:
- getId() - Returns the id of expense
- getGroupId() - Returns the id of the group expense belongs to
- getDescription() - Returns the description of expense
- isRepeat() - Returns if expense is repeat or not
- getRepeatInterval() - Returns the repeat interval of expense
- getEmailReminder() - Returns Email reminder
- getEmailReminderInAdvance() - Returns email reminder in advance
- getNextRepeat() - Returns the time of next repeat
- getDetails() - Returns the detail of expense
- getCommentsCount() - Returns the number of comments
- getPayment() - Returns the payment of expense
- getCreationMethod() - Returns the creation method of expense
- getTransactionMethod() - Returns the transaction method of expense
- getTransactionConfirmed() - Returns if transaction is confirmed
- getCost() - Returns the cost of transaction
- getCurrencyCode() - Returns the currency code of transaction
- getCreatedBy() - Returns a
User
object of user who created the expense - getDate() - Returns the expense date.
- getCreatedAt() - Returns the date time expense was created
- getUpdatedAt() - Returns the date time expense was last updated
- getDeletedAt() - Returns the date time expense was deleted
- getReceipt() - Returns a
Receipt
object for receipt - getCategory() - Returns a
Category
object for category - getUpdatedBy() - Returns a
User
object of user who last updated the expense - getDeletedBy() - Returns a
User
object of user who deleted the expense - getUsers() - Returns a list of
ExpenseUser
objects - getExpenseBundleId() - Returns Expense Bundle ID
- getFriendshipId() - Returns the Friendship ID
- getRepayments() - Returns a list of
Debt
objects - setGroupId(id) - Sets the id of the group expense belongs to
- setDescription(description) - Sets the description of expense
- setRepeatInterval(interval) - Sets the repeat interval of expense
- setCost(cost) - Sets the cost of transaction
- setCurrencyCode(code) - Sets the currency code of transaction
- setSplitEqually(is_split_equally) - Sets if expense is to be split equally
- setReceipt(receipt) - Sets a
Receipt
object for receipt - setCategory(category) - Sets a
Category
object for category - setUsers(users) - Sets a list of
ExpenseUser
objects - addUser(user) - Adds to a list of
ExpenseUser
objects - setReceipt(receiptPath) - Adds the file as a receipt
- setDetails(details) - Sets the expense details
Picture
Methods:
- getSmall() - Returns the link to the small image
- getMedium() - Returns the link to the medium image
- getLarge() - Returns the link to the large image
Receipt
Methods:
- getOriginal() - Returns the link to the original uploaded receipt
- getLarge() - Returns the link to large image of uploaded receipt
Comment
Methods:
- getId() - Returns the id of the comment
- getContent() - Returns comment message
- getCommentType() - Returns comment type
- getRelationType() - Returns relation type of the comment
- getRelationId() - Returns relation id
- getCreatedAt() - Returns datetime at which comment was created
- getDeletedAt(id) - Returns datetime at which comment was deleted
- getUser() - Returns a
User
object containing user details
Notification
Methods:
- getId() - Returns the id
- getContent() - Returns message
- getImageShape() - Returns comment type
- getImageType() - Returns relation type of the comment
- source - Returns source object
- getCreatedAt() - Returns datetime at which notification was created
- getCreatedBy() - Returns id of user who created notification
Source
Used with Notifications.
Methods:
- getId() - Returns the id
- getType() - Returns type. Use in combination with ID to fetch structured data
- getUrl() - Returns url
Sample Application
This is the GitHub Link to the sample application written in Flask to show the usage of splitwise application.
License
MIT
Donate
If you think this helped you and you want to donate, you can do it via -
Paypal
https://www.paypal.me/namanagg
Bitcoin
1DNhyZ696ekq6sY5vYMcmBLxLzAtq3oYpM
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file splitwise-3.0.0.tar.gz
.
File metadata
- Download URL: splitwise-3.0.0.tar.gz
- Upload date:
- Size: 42.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aaa956d3f65b0d9e716333afa612656b20f50010f10d9b8313e692f25f8b6a14 |
|
MD5 | 02b4824b7f53ecddf87f7a4decadb819 |
|
BLAKE2b-256 | c2b160d894e0de19074d7bac8fc6ac900c09eb5eeb2580238c3ca61d259f2c0e |
File details
Details for the file splitwise-3.0.0-py3-none-any.whl
.
File metadata
- Download URL: splitwise-3.0.0-py3-none-any.whl
- Upload date:
- Size: 55.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48049bc8c025224c7df6059b2d8def80c5cb1f9a210a5650d92a410b7706f6de |
|
MD5 | 512ddbdb6a9f7aa224d13b4bbae41a1c |
|
BLAKE2b-256 | d2ea6d66e57ce59ea2735e3e9478ef0d1cc233e41b26c29c6d731fcd8598dd41 |