Projects implemented in order of incremental challenge:
-
books_v1 -
books_v2 -
authentication -
user_management -
sql_alchemy -
todos -
movies
Hide the implementation and only show necessary details to the user.
class Circle():
"""docstring for Circle."""
def __init__(self, radius):
super(Circle, self).__init__()
self.r = radius
def circumference(self):
""" This method is abstracted """
return 2*3.14*self.r
if __name__ == "__main__":
c = Circle(12.3)
print(c.circumference())We don't have to know how the circumference() method works - it's abstracted.
- Changes public attributes to private.
- How?
- Use the
__(double underscore) notation in class properties.
class Circle:
def __init__(self, radius):
self.__r = radius # This is not encapsulated and cannot change
self.diameter = 2*radius
def get_rad(self):
return self.__rWhen we use private variables, we need to create getters and setters for the private variable(s).
- A class inherits all properties and methods of another class.
- It's a fundamental OOP concept.
- Enable overwriting.
This creates an "is-a" relationship between objects.
class Animal:
def __init__(self, animal_class, weight):
self.__animal_class = animal_class
self.weight = weight
def talk(self):
print("Blah")
class Dog(Animal):
super().__init__(animal_class, weight)
def talk(self): # Overwrites
print("Bark")
class Cat(Animal):
super().__init__(animal_class, weight)
def talk(self): # Overwrites
print("Meoww")The Animal is-a Dog and the Dog is-a Animal.
-
Have many forms?
-
In this case,
speak()can have many forms, it can both be a dog and a cat.
def speak(a: Animal):
a.talk()
speak(cat)
speak(dog)-
A way to create objects made up of other objects.
-
This creates a "HAS-A" relationship between the objects.
class Engine():
pass
class Vehicle():
def __init__(self, engine):
engine = engine
e = Engine()
v = Vehicle(e)The vehicles has-a engine.
CreateReadUpdateDelete
| Operation | Method |
|---|---|
| Read | GET |
| Create | POST |
| Update | PUT |
| Delete | DELETE |
- https://fastapi.tiangolo.com/tutorial/path-params/
- https://fastapi.tiangolo.com/tutorial/query-params/
- https://fastapi.tiangolo.com/tutorial/body/
- Is an ORM - Object Relational Mapper.
- Supports many relational DBMS - Database Management Systems
- Abstracts SQL from the DBMS
SQL commands correspond to HTTP operations - CRUD. These operations have the following syntax.
To insert data use INSERT INTO and VALUES.
INSERT INTO todos (title, description, priority, complete, ...)
VALUES ("Groceries", "Do the groceries", 3, False, ...);To extract data use SELECT. Use * to fetch all rows and columns or specific ones.
SELECT * FROM todos;
SELECT title, description FROM todos;Specify criteria with WHERE.
SELECT * from todos WHERE priority=5;
SELECT * from todos where title="Groceries";
SELECT * from todos where id=2;Update records.
UPDATE todos SET complete=True WHERE id=5;
UPDATE todos SET complete=True WHERE title="Groceries";
UPDATE todos set complete=False WHERE id=9;Delete records
DELETE FROM todos WHERE id=4;
DELETE FROM todos WHERE complete=1;The client sends a POST request to /login API endpoint.
The request contains the username and password of the client.
The backend performs the Login flow:
- Authenticate user
1.1 Verify the user exists in the database (based on the
username) 1.1.1 If the username is in the database return the UserInDB object 1.1.2 If the username does not exists in the database returnNone1.2 Verify the provided password with the stored hashed password 1.2.1 If verification is False, raiseexception1.3 Return user item from database - Generate JWT token using the user item
2.1 Initialize data:
{"sub": username}2.2 Add token expiration time:{"exp": time_delta}2.3 Join the above into a single dict (update()) 2.4 Encode it using:jwt.encode()2.5 Create the JWT token object withaccess_tokenandtoken_typefields 2.6 Return the JWT token object to the client
The client sends a GET request with the following header to the /protected endpoint.
curl -L -X 'GET' \
'http://127.0.0.1:8000/protected' \
-H 'accept: application/json' \
-H 'Authorization: BEARER eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJib2IiLCJleHAiOjE3NDQyNzkzMTJ9.WWHP9nTFpGfyfcpTrgPxwwZDwyV_mGbeE7qV2lK8glI'The backend performs the following steps:
- Decodes the JWT token 1.1 Verify "sub" is present 1.2 Extract username from "sub" 1.2 Verify "exp" has not expired
- Get user item from database
- Check if user item is disabled 3.1 Return user item