-
Notifications
You must be signed in to change notification settings - Fork 2
uMigrate API
This guide is for developing and using the uMigrate API.
The following are commonly used API endpoints and pages.
Visit the /admin/ page to use administrative control over resources in the database. You must be have the superuser status and staff status to have access to this page.
Visit the /swagger/ page to see all available endpoints. More endpoints will be visible when visiting this page as an authenticated user. You can test these endpoints through the swagger page or by visiting the endpoints directly.
Visit the /api/registration/ endpoint to register for uMigrate. You will need to make a POST request with your email, password, and password again. An email will be sent with a link to verify your registration. Click the link to complete registration. If running the API locally, the email will be in your running terminal.
Visit the /api/login/ endpoint to log into your account. You will need to make a POST request with your email and password.
Visit the /api/logout/ endpoint to log out of your account. You will need to make an empty POST request.
Visit the /api/password/reset/ endpoint to reset your account password. You will need to make a POST request with your email. An email will be sent with a link to set a new password. Click the link to go to the password reset page. Follow the instructions on the page to complete the password reset.
Visit the /api/password/change/ endpoint to change your account password. You will need to make a POST request with your old password, new password, and new password again. You must be authenticated to have access to this endpoint.
Follow the following steps to get the API running locally on your machine.
- Git 2: Used for source control and integration with GitHub. Find the version to download for your OS here.
- Python 3.9: Used to run the API. Find the version to download for your OS here.
- Docker Desktop: Used to run the Redis in-memory data store and postgres database. Download the Windows version here or the Mac version here. Check all checkboxes when installing. You will need to restart after installing. After restart, open Docker Desktop and it may ask you to install WSL 2. Follow the instructions to install WSL 2. You will need to restart after installing WSL 2. There is no Docker Desktop for Linux, but you can download the Docker Engine for Ubuntu Linux here.
- In the terminal of your choice, navigate to the directory you want to put the umigrate project in.
- Clone the repo by running the command
git clone https://github.com/Team-uMigrate/umigrate.git.
- In the terminal of your choice, navigate to the umigrate project, then navigate to the umigrate/api/src/ directory.
- Create a virtual environment by running the command
py -3.9 -m venv envon Windows andpython3.9 -m venv envon Mac or Linux. - Download the Redis Docker Container image by running the command
docker pull redis. - Download the Postgres Docker Container image by running the command
docker pull postgres.
- In the terminal of your choice, navigate to the umigrate project, then navigate to the umigrate/api/src/ directory.
- Activate the virtual environment by running the command
.\env\Scripts\activatefor Windows orsource env/bin/activatefor Mac or Linux. If using git bash on Windows, dosource env/Scripts/activateinstead. You will now see the text(env)on the current line in your terminal. - If you want to deactivate the virtual environment, run the command
deactivate.
- Activate the virtual environment.
- Update the packages by running the command
pip install -r requirements.txt.
- In the terminal of your choice, navigate to the umigrate project, then navigate to the umigrate/api/src/ directory.
- Run the Postgres Docker Container by running the command
docker run -d --name postgres-docker -p 5432:5432 -e POSTGRES_PASSWORD=postgres postgres. - Enter the container by running the command
docker exec -it postgres-docker /bin/bash. - Enter the psql command-line by running the command
psql -U postgres. - Create the umigratedb database by running the command
CREATE DATABASE umigratedb; - If you want to exit psql, run the command
\q. - If you want to exit the container, run the command
exit.
- In the terminal of your choice, navigate to the umigrate project, then navigate to the umigrate/api/src/ directory.
- Run the Redis Docker Container by running the command
docker run -d --name redis-docker -p 6379:6379 redis.
- Run the Postgres Docker Container.
- Activate the virtual environment.
- Run the command
python manage.py migrate.
- Activate the virtual environment.
- Run the Postgres Docker Container.
- Run the Redis Docker Container.
- Start the Django server by running the command
python manage.py runserver. The API should be running on port 8000. You can click the URL in the terminal to view the browsable API.
The following are some things to keep in mind when developing locally.
The API team uses either Visual Studio Code (free) or PyCharm Professional (free for students). You can download Visual Studio Code here. You can download PyCharm Professional here.
Python Extension Pack: donjayamanne.python-extension-pack Django Test Runner: pachwenko.django-test-runner Docker: ms-azuretools.vscode-docker PostgreSQL: ckolkman.vscode-postgres
Always open the API project from the umigrate/api/src/ directory to avoid namespacing issues.
After modifying models in any models.py file, you need to create new migration files that will update the database schema to reflect the changed models. Create the migration files by running the command python manage.py makemigrations. Then update the database schema by running the command python manage.py migrate.
To clear the data in the database, run the command python manage.py flush. This command will not drop the database or the tables in the database.
To add new packages to the virtual env, add them to the requirements.txt file, then run the command pip install -r requirements.txt.
To run all unit tests, run the command python manage.py test. The test results will appear in the running terminal.
To format all files, run the command black --exclude env ..\src on windows or black --exclude env ../src on unix.
To run rq,
- On the local env, run
export SCHEDULER_QUEUE="local_default"On the dev env, runexport SCHEDULER_QUEUE="dev_default" - Run
python manage.py rqscheduler --interval 1 --queue $SCHEDULER_QUEUE - Open a new terminal, repeat step 1, and run
python manage.py rqworker $SCHEDULER_QUEUE - Note: these should be running in the background while you are running
python manage.py runserverif you want to use scheduler
Django: https://www.linkedin.com/learning/learning-django-2 Django rest framework: https://www.linkedin.com/learning/building-restful-web-apis-with-django