Skip to content

Instantly share code, notes, and snippets.

@leriaetnasta
Last active February 7, 2025 10:54

Revisions

  1. leriaetnasta revised this gist Feb 2, 2025. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions add_starship_fastapi.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    from fastapi import FastAPI
    from pydantic import BaseModel, computed_field
    from pydantic import BaseModel

    app = FastAPI()

    @@ -9,10 +9,6 @@ class Starship(BaseModel):
    price: float
    discount: float | None

    @computed_field
    def total(self) -> float:
    return self.price - (self.price * self.discount / 100)

    @app.post("/addStarship")
    async def add_starship(starship: Starship):
    return {
  2. leriaetnasta revised this gist Feb 2, 2025. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions add_starship_fastapi.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    from fastapi import FastAPI
    from pydantic import BaseModel, computed_field, Field
    import uuid
    from pydantic import BaseModel, computed_field

    app = FastAPI()

  3. leriaetnasta revised this gist Feb 2, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion add_starship_fastapi.py
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@
    app = FastAPI()

    class Starship(BaseModel):
    id: str = Field(default_factory=lambda: f"schorle-{uuid.uuid4()}")
    id: str
    name: str
    price: float
    discount: float | None
  4. leriaetnasta revised this gist Feb 2, 2025. 1 changed file with 8 additions and 10 deletions.
    18 changes: 8 additions & 10 deletions add_starship_fastapi.py
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,22 @@
    from fastapi import FastAPI, Body
    from pydantic import BaseModel, computed_field
    from fastapi import FastAPI
    from pydantic import BaseModel, computed_field, Field
    import uuid

    app = FastAPI()

    class Starship(BaseModel):
    id: str = Field(default_factory=lambda: f"schorle-{uuid.uuid4()}")
    name: str
    price: float
    discount: float
    discount: float | None

    @computed_field
    def total(self) -> float:
    return self.price - (self.price * self.discount / 100)
    class Jedi(BaseModel):
    name: str
    lightsaber_color: str

    @app.post("/addStarship/{starship_id}")
    async def add_starship(starship: Starship, starship_id: int):
    @app.post("/addStarship")
    async def add_starship(starship: Starship):
    return {
    "starship_id": starship_id,
    "starship_id": starship.id,
    "starship": starship
    }

  5. leriaetnasta revised this gist Feb 2, 2025. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion add_starship_fastapi.py
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,6 @@ class Jedi(BaseModel):
    @app.post("/addStarship/{starship_id}")
    async def add_starship(starship: Starship, starship_id: int):
    return {
    "message": "Starship added!",
    "starship_id": starship_id,
    "starship": starship
    }
  6. leriaetnasta revised this gist Feb 2, 2025. 2 changed files with 25 additions and 17 deletions.
    25 changes: 25 additions & 0 deletions add_starship_fastapi.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    from fastapi import FastAPI, Body
    from pydantic import BaseModel, computed_field

    app = FastAPI()

    class Starship(BaseModel):
    name: str
    price: float
    discount: float

    @computed_field
    def total(self) -> float:
    return self.price - (self.price * self.discount / 100)
    class Jedi(BaseModel):
    name: str
    lightsaber_color: str

    @app.post("/addStarship/{starship_id}")
    async def add_starship(starship: Starship, starship_id: int):
    return {
    "message": "Starship added!",
    "starship_id": starship_id,
    "starship": starship
    }

    17 changes: 0 additions & 17 deletions starships_fastapi.py
    Original file line number Diff line number Diff line change
    @@ -1,17 +0,0 @@
    from fastapi import FastAPI, Body
    from pydantic import BaseModel

    app = FastAPI()

    class Starship(BaseModel):
    name: str

    class Jedi(BaseModel):
    name: str
    lightsaber_color: str

    @app.post("/jedi/{jedi_id}/purchase/{starship_id}")
    def purchase(jedi_id: int, starship_id: int, jedi: Jedi = Body(...), starship: Starship = Body(...)):
    return {
    "message": f"Jedi {jedi.name} [ID: {jedi_id}] owns the {starship.name} [ID: {starship_id}]"
    }
  7. leriaetnasta revised this gist Feb 2, 2025. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions starships_fastapi.py
    Original file line number Diff line number Diff line change
    @@ -3,10 +3,10 @@

    app = FastAPI()

    class Starship():
    class Starship(BaseModel):
    name: str

    class Jedi():
    class Jedi(BaseModel):
    name: str
    lightsaber_color: str

  8. leriaetnasta revised this gist Feb 2, 2025. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions starships_fastapi.py
    Original file line number Diff line number Diff line change
    @@ -3,10 +3,10 @@

    app = FastAPI()

    class Starship(BaseModel):
    class Starship():
    name: str

    class Jedi(BaseModel):
    class Jedi():
    name: str
    lightsaber_color: str

  9. leriaetnasta renamed this gist Feb 2, 2025. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  10. leriaetnasta created this gist Feb 2, 2025.
    17 changes: 17 additions & 0 deletions starships_fastapi
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    from fastapi import FastAPI, Body
    from pydantic import BaseModel

    app = FastAPI()

    class Starship(BaseModel):
    name: str

    class Jedi(BaseModel):
    name: str
    lightsaber_color: str

    @app.post("/jedi/{jedi_id}/purchase/{starship_id}")
    def purchase(jedi_id: int, starship_id: int, jedi: Jedi = Body(...), starship: Starship = Body(...)):
    return {
    "message": f"Jedi {jedi.name} [ID: {jedi_id}] owns the {starship.name} [ID: {starship_id}]"
    }