Skip to content

LeoAlbu125/drone-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 

Repository files navigation

Drone API

I was motivated to create this API because in the future I will implement more endpoint and use it to communicate with small drones that I will create later, so to hit two bunnies with only one slash I made this to store images and register my drones, later I will implement some face recognition models together with the API to make some expiriments with my drones.

Getting Started

Installing Dependencies

Python 3.7

Follow instructions to install the latest version of python for your platform in the python docs

Virtual Environment

We recommend working within a virtual environment whenever using Python for projects. This keeps your dependencies for each project separate and organized. Instructions for setting up a virtual environment for your platform can be found in the python docs

PIP Dependencies

Once you have your virtual environment setup and running, install dependencies by naviging to the /backend directory and running:

pip install -r requirements.txt

This will install all of the required packages we selected within the requirements.txt file.

Key Dependencies
  • Flask is a lightweight backend microservices framework. Flask is required to handle requests and responses.
  • SQLAlchemy and Flask-SQLAlchemy are libraries to handle the lightweight sqlite database. Since we want you to focus on auth, we handle the heavy lift for you in ./src/database/models.py. We recommend skimming this code first so you know how to interface with the Drink model.
  • python-jose JavaScript Object Signing and Encryption for JWTs. Useful for encoding, decoding, and verifying JWTS.

Running the server locally

From within the ./src directory first ensure you are working using your created virtual environment.

After that create a .env file and configure the DB URI, set a variable with the name DB_NAME and put these other variables in it too

DB_NAME = 'PUT YOUR DB URI HERE'
AUTH0_DOMAIN = "dev-wxmngepzwpjnyg04.us.auth0.com"
API_AUDIENCE = 'http://127.0.0.1:5050/'
DRONE_USER = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IkV0ZUo5b0tZbkV2NzlzNFpmNWx0WCJ9.eyJpc3MiOiJodHRwczovL2Rldi13eG1uZ2Vwendwam55ZzA0LnVzLmF1dGgwLmNvbS8iLCJzdWIiOiJPc0QyNzFRdGJ0cGlFRWFsWXhYbmNQeDJYdXFvQWxzYUBjbGllbnRzIiwiYXVkIjoiaHR0cDovLzEyNy4wLjAuMTo1MDUwLyIsImlhdCI6MTY5ODk4MTAwNSwiZXhwIjoxNjk5MDY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yIsInBhdGNoOmRyb25lcyIsImRlbGV0ZTpwaG90b3MiXX0.Sk9KAPJN6nAako6sChzcPq3vWCSI63hH9TwBdgwshMjkBf6sfZv3sQKckQeGcswReIC752aOSuB1LHqb_BN5Fbz00C57xjpX1nxBO2PFBNDxwKPzObMPAiGiFCj6lvntn5WEUANCy56QXOlCc3VizQvTMGSIRECDjsHj-i5D0IXPZJGx59TErARgc4V6wFMnlpW19SM-j4v-IQhGXX_-BESAcBugouvKMJBob_0UtbGIkUoo-DpzFjD9zLFNqcQ5dEvq1NwsojkcDO9Z_inlTpo1L92L1ejLxUhMKN_H0WspdD0j0nuFjUMGgMVfuCt1hrNqQi3M2u8i5SqbBPKALA'

Each time you open a new terminal session, run:

export FLASK_APP=droneapi.py;

To run the server, execute:

flask run -p 5050

The --reload flag will detect file changes and restart the server automatically.

Using the exposed link

If you dont want to run locally you can use it with the public link. (there is a problem with tests running in the online DB, some of them fail because the DB "receives too much connections at the same time")

API Reference

()You should substitute <your-api-base-url> for the local link or the exposed one.)

Authentication

This API uses authentication to secure certain endpoints. You need to include a valid JWT token in the Authorization header for these protected endpoints. The token should be in the format: Bearer YOUR_TOKEN. (I will send two tokens one for the ADMIN ROLE and other for the DRONE ROLE)


1. Get All Drones

  • Endpoint: /drones
  • HTTP Method: GET
  • Description: Get a list of all drones.
  • Authentication: None required.
  • CURL Example:
curl -X GET http://your-api-base-url/drones

2. Get Photos by Drone

  • Endpoint: /<drone_id>/photos
  • HTTP Method: GET
  • Description: Get photos associated with a specific drone.
  • Authentication: Requires a valid JWT token with get:photos permission. (DRONE, ADMIN)
  • CURL Example:
curl -X GET -H "Authorization: Bearer YOUR_TOKEN" http://your-api-base-url/1/photos 

Replace YOUR_TOKEN with a valid JWT token and your-api-base-url with the local or exposed URL.

3. Post a Photo

  • Endpoint: /photos
  • HTTP Method: POST
  • Description: Add a new photo associated with a drone.
  • Authentication: Requires a valid JWT token with post:photos permission. (DRONE, ADMIN)
  • CURL Example:
curl -X POST -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" -d '{
  "tag": "Aerial View",
  "content": "URL to Photo 3",
  "drone_id": 1
}' http://your-api-base-url/photos

Replace YOUR_TOKEN with a valid JWT token and your-api-base-url with the local or exposed URL.

4. Patch Drone Details

  • Endpoint: /drones/<id>
  • HTTP Method: PATCH
  • Description: Update details of a specific drone.
  • Authentication: Requires a valid JWT token with patch:drones permission. (ADMIN)
  • CURL Example:
curl -X PATCH -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" -d '{
  "drone_name":"new_name", "drone_model":"new_model"
}' http://your-api-base-url/drones/1

Replace YOUR_TOKEN with a valid JWT token and your-api-base-url with the local or exposed URL.

5. Delete a Photo

  • Endpoint: /photos/<id>
  • HTTP Method: DELETE
  • Description: Delete a specific photo by its ID.
  • Authentication: Requires a valid JWT token with delete:photos permission. (ADMIN)
  • CURL Example:
curl -X DELETE -H "Authorization: Bearer YOUR_TOKEN" http://your-api-base-url/photos/3

Replace YOUR_TOKEN with a valid JWT token and your-api-base-url with the local or exposed URL.


Error Handling

The API handles errors with the following error codes and messages:

  • 404 Not Found: When a requested resource does not exist.
  • 422 Unprocessable Entity: When there is a problem with the request or database operation.
  • 500 Internal Server Error: For internal server errors.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors