Requirements:
- Python, pip
- PostgreSQL
- Virtualenv
Setup:
- In a terminal/ command line window, navigate to the root folder of the project
- Create a virtual environment
env
by running:- [Mac/Linux]
python3 -m venv env
- [Windows]
py -m venv env
- [Mac/Linux]
- Activate the virtual environment by running:
- [Max/Linux]
source env/bin/activate
- [Windows]
.\env\Scripts\activate
- [Max/Linux]
- To install required dependencies, run
pip install -r requirements.txt
- Using psql, create the
customers
database by enteringcreate database customers;
- In the root folder of the project, create a file named
.env
with the following contents. Substitute {username} and {password} with the username and password of the Postgres user who owns thecustomers
databaseFLASK_ENV=development SQLALCHEMY_DATABASE_URI='postgresql://{username}:{password}@localhost:5432/customers'
- Start the PostgreSQL service
- On the terminal/ command line window, create the required tables by running the following:
flask db init flask db migrate flask db upgrade
- To start the app, run
flask start
In this example, for demonstration purposes, we provide an unsecured method to create an administrator account. Using an administrator username and password, you are able to generate a JWT token allowing you to access the Customer resource endpoints.
Endpoint
: POST /admin
Content-Type
: application/json
Name | Data Type | Required/ Optional |
---|---|---|
username |
string | required |
password |
string | required |
Endpoint
: /login
The API uses Basic Auth. To login, encode a valid administrator username and password (created above) with base64:
echo -n {username}:{password} | base64
This will return a base64-encoded string.
Example usage: echo -n Admin:1234 | base64
produces an output QWRtaW46MTIzNA==
Authorization
: Basic {str}
where {str}
is the base64-encoded string output
Example usage: Authorization
: Basic QWRtaW46MTIzNA==
Name | Data Type | Description |
---|---|---|
jwt-token |
string | Token required to access customer resource endpoints |
The token expires within 2 minutes.
The token should be passed into the x-access-token
header field of the following requests.
Retrieves all customers in the database.
Endpoint
: GET /users/api/v1.0/users
Content-Type
: application/json
Name | Data Type | Required/ Optional | Description |
---|---|---|---|
sort_by |
string | optional | The attribute of the customer that we want to sort by. When sort_by =dob , we return a list of customers sorted by date of birth starting from the most recent. |
number |
number | optional | When sort_by is specified, indicates the number of customers to be returned. |
Adds a new customer to the database.
Endpoint
: POST /users/api/v1.0/users
Content-Type
: application/json
Name | Data Type | Required/ Optional | Description |
---|---|---|---|
name |
string | required | The name of the customer |
dob |
datetime | required | The birthdate of the customer in MM/DD/YYYY format |
Updates a specific customer in the database.
Endpoint
: PUT /users/api/v1.0/users/{user_id}
Content-Type
: application/json
Name | Data Type | Required/ Optional | Description |
---|---|---|---|
user_id |
number | required | The id of the customer whose details will be edited |
Name | Data Type | Required/ Optional | Description |
---|---|---|---|
name |
string | required | The name of the customer |
dob |
datetime | required | The birthdate of the customer in MM/DD/YYYY format |
Endpoint
: DELETE /users/api/v1.0/users/{user_id}
Content-Type
: application/json
Name | Data Type | Required/ Optional | Description |
---|---|---|---|
user_id |
number | required | The id of the customer whose details will be edited |