Skip to content

Commit

Permalink
Merge pull request #60 from tecladocode/jose/recording-improvements-a…
Browse files Browse the repository at this point in the history
…ug-22
  • Loading branch information
jslvtr authored Aug 23, 2022
2 parents 9d22570 + bb0278c commit 4ced652
Show file tree
Hide file tree
Showing 484 changed files with 824 additions and 753 deletions.
10 changes: 7 additions & 3 deletions docs/docs/01_course_intro/04_what_is_rest_api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ That data might look like this:

### The purpose of APIs

Okay, we've learned that we can make a Flask app and expose certain functions to the public by using endpoints. Clients can then make requests (we'll learn how later), and get data.
We've learned that we can make a Flask app and expose certain functions to the public by using endpoints. Clients can then make requests (we'll learn how later), and get data.

Clients can also send data, which the functions can use.
Clients can also send data, which the Flask functions can use.

But _why_? If you want to use certain functions, why not just code them in your application?

Expand All @@ -74,10 +74,14 @@ Let's say you want to build a weather app.

You could try to install sensors at the top of your house, connect them directly to the computer running your code, and then offer weather info based on what the sensors say...

Or you could request weather data from the OpenWeatherMap API, just as tens of thousands of other devices do.
Or you could request weather data from the OpenWeatherMap API, just as tens of thousands of other devices do.

Much easier, and all you have to do is make a request to the API!

### What is a client?

An API client can be any device, such as a web app or a mobile app.

### Making an API for your own consumption

Make software companies make APIs that only they use (so they aren't fully public).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)
store_id = db.Column(db.Integer, unique=False, nullable=False)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)

store_id = db.Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)
store_id = db.Column(db.Integer, unique=False, nullable=False)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)

store_id = db.Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)

store_id = db.Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ description: Learn how to use SQLAlchemy to add new rows to our SQL database.
Inserting models with SQLAlchemy couldn't be easier! We'll use the `db.session`[^1] variable to `.add()` a model. Let's begin working on our `Item` resource:

```python title="resources/item.py"
from sqlalchemy.exc import SQLAlchemyError

from db import db
from models import ItemModel

...

@blp.arguments(ItemSchema)
@blp.response(201, ItemSchema)
def post(self, item_data):
Expand All @@ -31,6 +38,13 @@ def post(self, item_data):
Similarly in our `Store` resource:

```python title="resources/store.py"
from sqlalchemy.exc import SQLAlchemyError, IntegrityError

from db import db
from models import StoreModel

...

@blp.arguments(StoreSchema)
@blp.response(201, StoreSchema)
def post(self, store_data):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)

store_id = db.Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)

store_id = db.Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)

store_id = db.Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def delete(self, item_id):
@blp.arguments(ItemUpdateSchema)
@blp.response(200, ItemSchema)
def put(self, item_data, item_id):
item = ItemModel.query.get_or_404(item_id)
item = ItemModel.query.get(item_id)

raise NotImplementedError("Updating an item is not implemented.")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)

store_id = db.Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ This idempotency is frequently seen with `PUT` requests. You can see it in actio
@blp.arguments(ItemUpdateSchema)
@blp.response(200, ItemSchema)
def put(self, item_data, item_id):
item = ItemModel.query.get_or_404(item_id)
# highlight-start
item = ItemModel.query.get(item_id)
if item:
item.price = item_data["price"]
item.name = item_data["name"]
else:
item = ItemModel(**item_data)
item = ItemModel(id=item_id, **item_data)

db.session.add(item)
db.session.commit()

return item
# highlight-end
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)

store_id = db.Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ def delete(self, item_id):
@blp.arguments(ItemUpdateSchema)
@blp.response(200, ItemSchema)
def put(self, item_data, item_id):
item = ItemModel.query.get_or_404(item_id)
item = ItemModel.query.get(item_id)

if item:
item.price = item_data["price"]
item.name = item_data["name"]
else:
item = ItemModel(**item_data)
item = ItemModel(id=item_id, **item_data)

db.session.add(item)
db.session.commit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)

store_id = db.Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def delete(self, item_id):
@blp.arguments(ItemUpdateSchema)
@blp.response(200, ItemSchema)
def put(self, item_data, item_id):
item = ItemModel.query.get_or_404(item_id)
item = ItemModel.query.get(item_id)

raise NotImplementedError("Updating an item is not implemented.")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)

store_id = db.Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ def delete(self, item_id):
@blp.arguments(ItemUpdateSchema)
@blp.response(200, ItemSchema)
def put(self, item_data, item_id):
item = ItemModel.query.get_or_404(item_id)
item = ItemModel.query.get(item_id)

if item:
item.price = item_data["price"]
item.name = item_data["name"]
else:
item = ItemModel(**item_data)
item = ItemModel(id=item_id, **item_data)

db.session.add(item)
db.session.commit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)

store_id = db.Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ def delete(self, item_id):
@blp.arguments(ItemUpdateSchema)
@blp.response(200, ItemSchema)
def put(self, item_data, item_id):
item = ItemModel.query.get_or_404(item_id)
item = ItemModel.query.get(item_id)

if item:
item.price = item_data["price"]
item.name = item_data["name"]
else:
item = ItemModel(**item_data)
item = ItemModel(id=item_id, **item_data)

db.session.add(item)
db.session.commit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)

store_id = db.Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ def delete(self, item_id):
@blp.arguments(ItemUpdateSchema)
@blp.response(200, ItemSchema)
def put(self, item_data, item_id):
item = ItemModel.query.get_or_404(item_id)
item = ItemModel.query.get(item_id)

if item:
item.price = item_data["price"]
item.name = item_data["name"]
else:
item = ItemModel(**item_data)
item = ItemModel(id=item_id, **item_data)

db.session.add(item)
db.session.commit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)

store_id = db.Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ def delete(self, item_id):
@blp.arguments(ItemUpdateSchema)
@blp.response(200, ItemSchema)
def put(self, item_data, item_id):
item = ItemModel.query.get_or_404(item_id)
item = ItemModel.query.get(item_id)

if item:
item.price = item_data["price"]
item.name = item_data["name"]
else:
item = ItemModel(**item_data)
item = ItemModel(id=item_id, **item_data)

db.session.add(item)
db.session.commit()
Expand Down
3 changes: 2 additions & 1 deletion docs/docs/06_sql_storage_sqlalchemy/10_conclusion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)

store_id = db.Column(
Expand Down Expand Up @@ -92,6 +92,7 @@ class ItemSchema(PlainItemSchema):
class ItemUpdateSchema(Schema):
name = fields.Str()
price = fields.Float()
store_id = fields.Int()


class StoreSchema(PlainStoreSchema):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)

store_id = db.Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ def delete(self, item_id):
@blp.arguments(ItemUpdateSchema)
@blp.response(200, ItemSchema)
def put(self, item_data, item_id):
item = ItemModel.query.get_or_404(item_id)
item = ItemModel.query.get(item_id)

if item:
item.price = item_data["price"]
item.name = item_data["name"]
else:
item = ItemModel(**item_data)
item = ItemModel(id=item_id, **item_data)

db.session.add(item)
db.session.commit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)

store_id = db.Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ def delete(self, item_id):
@blp.arguments(ItemUpdateSchema)
@blp.response(200, ItemSchema)
def put(self, item_data, item_id):
item = ItemModel.query.get_or_404(item_id)
item = ItemModel.query.get(item_id)

if item:
item.price = item_data["price"]
item.name = item_data["name"]
else:
item = ItemModel(**item_data)
item = ItemModel(id=item_id, **item_data)

db.session.add(item)
db.session.commit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ItemModel(db.Model):
__tablename__ = "items"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
name = db.Column(db.String(80), unique=False, nullable=False)
price = db.Column(db.Float(precision=2), unique=False, nullable=False)

store_id = db.Column(
Expand Down
Loading

1 comment on commit 4ced652

@vercel
Copy link

@vercel vercel bot commented on 4ced652 Aug 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.