Skip to content

DEV: Feature - Initial Buildup of Meeting Room Reservation API#1

Open
AliTahir-101 wants to merge 1 commit intodevfrom
feature-reservation-system
Open

DEV: Feature - Initial Buildup of Meeting Room Reservation API#1
AliTahir-101 wants to merge 1 commit intodevfrom
feature-reservation-system

Conversation

@AliTahir-101
Copy link
Owner

Description

A team member has developed a new API designed for scheduling meeting rooms. Below is their inaugural PR submitted to the recently established repository. It requires a thorough review and constructive feedback.

Copy link
Owner Author

@AliTahir-101 AliTahir-101 left a comment

Choose a reason for hiding this comment

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

@AliTahir-101 Please consider having a more structured code along with test coverage around your implementation. I've given a basic initial folder structure for an idea.

/project_root
    /src
        __init__.py
        main.py  # Application entry point, includes API routes
        models.py  # Pydantic models
        services.py  # Business logic, e.g., `BookingSystem`
        settings.py  # Configuration settings from the environment
    /tests
        test_main.py  # Test cases for your application
    .env  # Environment variables
    requirements.txt  # Project dependencies

@@ -0,0 +1,71 @@
import logging
Copy link
Owner Author

Choose a reason for hiding this comment

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

@AliTahir-101 Looks like the logging import isn't being used in the current implementation. Could we remove it to keep the codebase clean till you need it?

from datetime import datetime
import requests
from fastapi import FastAPI
from pydantic import *
Copy link
Owner Author

Choose a reason for hiding this comment

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

from pydantic import * is not a good practice. Can we please Import only what is needed here?

@@ -0,0 +1,71 @@
import logging
from datetime import datetime
import requests
Copy link
Owner Author

Choose a reason for hiding this comment

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

The requests library is synchronous and will possibly block the event loop in your async function. Could we use some async alternative like httpx for async HTTP calls?

app = FastAPI()


class RoomBooking(BaseModel):
Copy link
Owner Author

Choose a reason for hiding this comment

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

It seems you missed the BaseModel import.

from pydantic import BaseModel.



class BookingSystem:
url = "https://third-party-booking-system/room_availability"
Copy link
Owner Author

Choose a reason for hiding this comment

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

We should have the base URL for the third-party booking system in the environment variable for flexibility and ease of configuration, especially when moving between different environments (development, staging, production) makes life easier 😊

== "free"
)

def book_rom(self, room_id: str, start_date: datetime, end_date: datetime) -> None:
Copy link
Owner Author

Choose a reason for hiding this comment

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

same comments as the last function. Moreover, can we make it book_room so as to be consistent in using proper keywords?

"end_date": end_date.isoformat(),
},
)
print("Room booked! 🎉"
Copy link
Owner Author

Choose a reason for hiding this comment

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

We should use logging for any debugging or informational messages what do you think?

)


@app.post("/book_room")
Copy link
Owner Author

Choose a reason for hiding this comment

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

We should change the endpoint @app.post("/book_room") to follow restful conventions, like @app.post("/rooms/book") what do you think?

Comment on lines +54 to +55
assert room_booking.start_date < room_booking.end_date
assert room_booking.start_date > datetime.now()
Copy link
Owner Author

Choose a reason for hiding this comment

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

I think we should avoid assertions here and provide clear error responses with HTTPException.

from fastapi import HTTPException
from datetime import datetime

@app.post("/book_room")
async def book_room(room_booking: RoomBooking):
    if room_booking.start_date >= room_booking.end_date:
        raise HTTPException(status_code=400, detail="Start date must be before end date.")
    if room_booking.start_date <= datetime.now():
        raise HTTPException(status_code=400, detail="Start date must be in the future.")

Comment on lines +60 to +66
if available:
booking_system.book_rom(
room_booking.room_id, room_booking.start_date, room_booking.end_date
)
return {"status": "success"}
else:
return {"status": "slot unavailable"}
Copy link
Owner Author

Choose a reason for hiding this comment

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

@AliTahir-101 you should use Pydantic models for response bodies in FastAPI. Doing so ensures your responses are consistently structured and type-checked.

from pydantic import BaseModel

class BookingResponse(BaseModel):
    status: str
    message: str

@app.post("/book_room", response_model=BookingResponse)
async def book_room(room_booking: RoomBooking):
    # ... your endpoint logic ...
    return BookingResponse(status="success", message="Room booked successfully!")

Copy link
Owner Author

Choose a reason for hiding this comment

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

Moreover, add proper exception handling.

@AliTahir-101 AliTahir-101 self-assigned this Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant