A proof of concept API to manage a referral program
The service allows users to sign up for an account and once signed up they can create a referral, which is a UUID. A referral can then be used by a new user to sign up. If the new user signs up using the referral token their balance is credited with $10. For every 5 people that a user refers their account is credited with an additional $10.
The endpoints provided are as follows:
POST /user/Create new userGET /user/{id}Get userPOST /user/{id}/referralCreate a new referral
Create a new user.
URL : /user/
Method : POST
Parameters
- email - The new user's email address
- referral(optional) - A referral provided by an existing user
Code : 200 OK
POST /user?email=barbie@gmail.com&referral=9a299020-6ddb-4bf4-8fdd-4989337d8e82
{
"id": 1234
}Retrieve the information for an existing user.
URL : /user/{user_id}
Method : GET
Parameters
- user_id - The user id
Code : 200 OK
GET /user/12
{
"id": "49",
"email": "somerandom@email.com",
"referral": "9a299020-6ddb-4bf4-8fdd-4989337d8e82",
"balance": "10.0",
"total_referrals": "0"
}Create a referral for the given user
URL : /user/{user_id}/referral
Method : POST
Parameters
- user_id - The user id
Code : 200 OK
POST /user/12/referral
{
"referral": "9a299020-6ddb-4bf4-8fdd-4989337d8e82"
}The service requires Postgres and the sqlalchemy.url value should be set to your Postgres instance.
First off lets create a virtual environment
virtualenv venv
And activate it
source ./venv/bin/activate
Now we install the dependencies
pip install -e .
Install the dev dependencies
pip install -e ".[dev]"
Run the unit tests
pytest tests
Run the database migrations
alembi upgrage head
Finally we can run the web server
pserve development.ini --reload
You need to create a config var for your app in heroku called SQLALCHEMY_URL and this should be set to
connection string for your postgres instance. In hindsight I should have made use of the default variable provided
in Heroku (DATABASE_URL). I may change that at a later date.
Initialize the Heroku stack
heroku create --stack cedar
Push the code to heroku
git push heroku master
Scale the workers
heroku scale web=1