This implements an imitation of an online shop with customers and items; accessed through openidconnect authed users, and social logins (Oauth2)
To use this service, access test data at onlineshop.onrender.com/kiokogit/
To run on local development mode: Clone repository
git clone https://github.com/kiokogit/OnlineShopAPI.git
Create virtual env
python -m venv venv
install requirements using pip (or any other tool)
pip install -r requirements.txt
Create a .env file with the following mandatory envs
# if using google. can use any other social app for auth
GOOGLE_CLIENT_ID=clientid
GOOGLE_CLIENT_SECRET=None
SECRET_KEY=secret_key
DEBUG=True/False
AFR_TKG_USERNAME = ''
AFR_TKG_API_KEY = ''
Create superuser
python manage.py createsuperuser
Run
python manage.py runserver
Access the server at localhost:800 by default
All the api endpoints are documented using swagger. Check it on {your_endpoint}/documentation/
To get the deployed swagger docs, check out:
API Swagger Documentation
To run tests using coverage
pip install coverage pytest
coverage run -m pytest
coverage report -m
Include OAuth2 authentication headers
With the app running on localhost, register an application to get an access code to interact with the app
Get access code for authentication
http://localhost:8000/o/token/
Use BASIC AUTH for authorization
And for request body, enter the following in form data
Success response:
{
"access_token": "<access_token>",
"token_type": "Bearer",
"expires_in": 36000,
"refresh_token": "<refresh_token>",
"scope": "read write groups"
}
To call resources, add basic token authentication
For Alerting the customer of the orders Login to Africa's Talking sandbox, and get the credentials for the sandbox; When you place an order, a message should appear as below:
Cannot place order without phone number
/api/customers/
create example:
{
"name": "Cutomer1",
"code": "bs001",
"phone_number": "+2547xxxxxx" // must have country code
}
- For customer as the logged in user
POST
/api/orders/
body_example:
{
"item_name": "Sugae2",
"amount":100.56,
"customer_id": 1
}
Since this is a test app, dbsqlite3 is the configured database; for postgres or any other db configuration, uncomment these lines in settings.py
# DATABASES = {
# 'default': {
# 'ENGINE': os.getenv('DATABASES_DEFAULT_ENGINE', 'django.db.backends.postgresql_psycopg2'),
# 'NAME': os.getenv('DATABASES_DEFAULT_NAME'),
# 'USER': os.getenv('DATABASES_DEFAULT_USER'),
# 'PASSWORD': os.getenv('DATABASES_DEFAULT_PASSWORD'),
# 'HOST': os.getenv('DATABASES_DEFAULT_HOST'),
# 'PORT': os.getenv('DATABASES_DEFAULT_PORT'),
# }
# }
Then set the corresponding env values
This app has dev.yaml file that triggers github actions to deploy
on render.com using DEPLOYMENT HOOK
To set it up,
Login to render.com and set up a new web service, and set autodeploy=False
so that it does not deploy by commits, but by pull requests and push to main branch
on your github repository (if using github actions), set Repository secrets on Repository settings
add: RENDER_DEPLOY_HOOK_URL
and add value from render.com service
....END...