From 93823347c33b2db565bb8102593afa61b988f904 Mon Sep 17 00:00:00 2001 From: albertkun Date: Thu, 18 Jul 2024 15:15:18 -0700 Subject: [PATCH 1/2] feat: initial documentation blitz --- architecture.md | 25 ++++++++ documentation/README.md | 21 +------ documentation/docs/intro.md | 94 +++++++++++++++++++++-------- fastapi/app/update.md | 38 ++++++++++++ newendpoint.md | 115 ++++++++++++++++++++++++++++++++++++ 5 files changed, 247 insertions(+), 46 deletions(-) create mode 100644 architecture.md create mode 100644 fastapi/app/update.md create mode 100644 newendpoint.md diff --git a/architecture.md b/architecture.md new file mode 100644 index 0000000..d8037dd --- /dev/null +++ b/architecture.md @@ -0,0 +1,25 @@ +# Architecture + +This document describes the architecture of Metro API 2.x, including the backend, frontend, database, and API components. It provides an overview of the technologies used in the project and the repository structure. + +## Overview + +The project is built using the following technologies: + +- **Frontend**: The frontend is built using [React](https://reactjs.org/) and [Docusaurus](https://docusaurus.io/), a modern static website generator. It provides a user-friendly interface for interacting with the Metro API. +- **Backend**: The backend is built using [FastAPI](https://fastapi.tiangolo.com/), a modern web framework for building APIs with Python. It provides the logic for handling requests to the Metro API and serves as the interface between the frontend and the Metro API. +- **Database**: The project uses a [PostgreSQL](https://www.postgresql.org/) database to store data related to the Metro API, such as routes, stops, and schedules. The database is accessed using the [SQLAlchemy](https://www.sqlalchemy.org/) ORM. +- **API**: The Metro API provides access to real-time data, GTFS data, and other information related to the Los Angeles Metro system. It is built using FastAPI and provides endpoints for retrieving data about routes, stops, schedules, and live transit updates. +- **WebSocket**: The Metro API also supports WebSocket connections to receive real-time updates about vehicles and trips. This allows developers to receive updates on vehicle positions or trip updates in real-time. Mainly the web socket is used as a pass-through for the Swiftly API. + +## Repository Structure + +The project repository is structured as follows: + +- **`documentation/`**: Contains documentation for the project built on Docusaurus and using the plugin `docusaurus-plugin-redoc` to generate API documentation from OpenAPI specifications. +- **`fastapi/`**: Contains the FastAPI backend code for the Metro API, including routes, models, and logic for handling requests. +- **`data-loading-service`**: Contains the code for the data loading service that loads data from the Metro API into the PostgreSQL database. +- **`notebooks/`**: Contains Jupyter notebooks for data analysis and exploration. +- **`docker-compose.yml`**: Contains the Docker Compose configuration for running the project locally. +- **`README.md`**: Contains the project README with information about setting up and running the project. + diff --git a/documentation/README.md b/documentation/README.md index 7ab3ee3..d0ba0ea 100644 --- a/documentation/README.md +++ b/documentation/README.md @@ -1,28 +1,9 @@ # Template -This template is built for [Docusaurus 2](https://docusaurus.io/), a modern static website generator. +This template is built for [Docusaurus 3](https://docusaurus.io/), a modern static website generator. ### Usage -```bash -npx create-docusaurus@2.2.0 my-website --package-manager yarn -``` - -> When prompted to select a template choose `Git repository`. - -Template Repository URL: - -```bash -https://github.com/PaloAltoNetworks/docusaurus-template-openapi-docs.git -``` - -> When asked how the template repo should be cloned choose "copy" (unless you know better). - -```bash -cd my-website -yarn -``` - ### Local Development ```bash diff --git a/documentation/docs/intro.md b/documentation/docs/intro.md index 7a962a3..165c19e 100644 --- a/documentation/docs/intro.md +++ b/documentation/docs/intro.md @@ -1,45 +1,87 @@ ---- -sidebar_position: 1 ---- +# Welcome to the Los Angeles Metro User Guide -# Tutorial Intro +This guide provides an overview of the Los Angeles Metro API, including key features, common tasks, and troubleshooting tips. The developer side of this documentation is geared towards developers of the Los Angeles Metro API. -Let's discover **the Metro API in less than 5 minutes** by creating a webpage. +## Overview -## Getting Started +The Metro API provides access for developers to retrieve real-time data, GTFS data, and other information related to the Los Angeles Metro system. By leveraging this API, you can access detailed information about routes, stops, schedules, and live transit updates. -Get started by **creating a new site**. +- **Real-Time Data:** Access up-to-the-minute information on bus and train schedules, including delays and cancellations. +- **GTFS Data:** Explore detailed information about routes, stops, and schedules. +- **Canceled Service Data:** Stay informed about any service disruptions or cancellations that might affect your journey. +- **Static Data:** Explore detailed information about routes, stops, and schedules to plan your trip effectively. +- **Other Data:** Access additional resources like Gopass Schools information. -### What you'll need +## How to Use Key Features -- [Node.js](https://nodejs.org/en/download/) version 16.14 or above: - - When installing Node.js, you are recommended to check all checkboxes related to dependencies. +### Accessing Real-Time Data -## Generate a new site +To receive real-time updates about vehicles and trips, connect to our WebSocket endpoint. This endpoint is a pass-through for the Swiftly API, providing updates on vehicle positions or trip updates. -Generate a new Docusaurus site using the **classic template**. +#### How It Works: -The classic template will automatically be added to your project after you run the command: +1. **Connect to the WebSocket:** Use the endpoint format above to establish a WebSocket connection. +2. **Receive Updates:** Once connected, you will receive real-time updates every 3 seconds. Updates include information about vehicle positions or trip updates, depending on the `endpoint` parameter specified. +3. **Data Format:** Updates are sent in JSON format, including details such as `vehicle_id`, `route_code`, and trip information. +4. **Error Handling:** If an error occurs while processing updates, an error message will be sent in the format `"Error: error_message"`. -```bash -npm init docusaurus@latest my-website classic +#### Example Update Format: + +```json +{ + "id": "vehicle_id", + "vehicle": { + "trip": { + "route_id": "route_code", + ... + }, + ... + }, + "route_code": "route_code", + ... +} ``` +### Accessing GTFS Data -You can type this command into Command Prompt, Powershell, Terminal, or any other integrated terminal of your code editor. +- **Get All Agencies:** Use the `/agencies` endpoint to view a list of all available agencies. +- **Get Agency Routes:** Access `/routes/{agency_id}` to view all routes for a specific agency. +- **Get Route Stops:** Use `/stops/{agency_id}/{route_code}` to view all stops for a specific route. +- **Get Stop Times:** Access `/stop_times/{agency_id}/{route_code}` to view stop times for a specific route. +- **Get Trips:** Use `/trips/{agency_id}/{trip_id}` to view details about a specific trip. +- **Get All Trips:** Access `/trips/{agency_id}` to view all trips for a specific agency. +- **Get All Routes:** Use `/routes/{agency_id}` to view all routes for a specific agency. +- **Get All Stops:** Access `/stops/{agency_id}` to view all stops for a specific agency. +- **Get All Stop Times:** Use `/stop_times/{agency_id}` to view all stop times for a specific agency. -The command also installs all necessary dependencies you need to run Docusaurus. -## Start your site +### Checking Canceled Service Data -Run the development server: +- **Get Canceled Trip Summary:** Use `/canceled_service_summary` to view a summary of all canceled trips. +- **Get Canceled Trip:** To find details about canceled trips for a specific line, use `/canceled_service/line/{line}`. +- **View All Canceled Trips:** Access `/canceled_service/all` to see all canceled trips. -```bash -cd my-website -npm run start -``` +### Exploring Static Data + +- **Route and Stop Information:** Use endpoints like `/{agency_id}/route_stops/{route_code}` and `/{agency_id}/stops/{stop_id}` to get information about routes and stops. +- **Schedule and Trip Details:** Access detailed schedule information with endpoints such as `/{agency_id}/stop_times/route_code/{route_code}` and `/{agency_id}/trips/{trip_id}`. +- **Route Overview:** Get an overview of routes with `/{agency_id}/route_overview` or detailed information for a specific route with `/{agency_id}/route_overview/{route_code}`. + +## Examples of Common Tasks + +- **How to Check for Service Alerts:** Use the canceled service data endpoints to check for any disruptions or cancellations. +- **How to Find Real-Time Schedule Information:** Utilize the real-time data endpoints to get the latest schedule updates for your route. + +## Troubleshooting Tips + +- **Issue: The schedule seems outdated.** Solution: Ensure you're accessing the real-time data endpoints for the most current information. +- **Issue: Can't find my route.** Solution: Double-check the route code or use the `/{agency_id}/routes` endpoint to search for all available routes. + +## Support and Contact Information -The `cd` command changes the directory you're working with. In order to work with your newly created Docusaurus site, you'll need to navigate the terminal there. +For further assistance or to provide feedback, please contact our customer support team: -The `npm run start` command builds your website locally and serves it through a development server, ready for you to view at http://localhost:3000/. +- **Email:** support@lametro.com +- **Phone:** 1-800-LA-METRO +- **Live Chat:** Available on our web application during business hours. -Open `docs/intro.md` (this page) and edit some lines: the site **reloads automatically** and displays your changes. +We're here to help make your Los Angeles Metro experience as smooth and enjoyable as possible. Start exploring today and take the hassle out of public transit! \ No newline at end of file diff --git a/fastapi/app/update.md b/fastapi/app/update.md new file mode 100644 index 0000000..bb2069a --- /dev/null +++ b/fastapi/app/update.md @@ -0,0 +1,38 @@ +# Metro API v2.7 Update Notes + +## Using the new `/{agency_id}/route_details/{route_code}` endpoint + +The new `/{agency_id}/route_details/{route_code}` endpoint allows you to get stop time and line geometries for upcoming trips given a specific route, direction, day type, and time. + +The endpoint requires the following parameters: + +- `agency_id`: The agency ID of the transit agency (e.g. LACMTA for bus, LACMTA_Rail for rail) +- `route_code`: The route code of the route (e.g. 720) +- `direction_id`: The direction ID of the route (0 or 1) +- `day_type`: The day type of the trip (Weekday, Saturday, Sunday) +- `time`: The time of the trip (e.g. 12:00:00) + +The endpoint also has an optional parameter: + +- `num_results`: The number of results to return (default is 3) + +An example request to the endpoint would look like this: + +``` +https://api.metro.net/LACMTA/route_details/720?direction_id=1&day_type=Weekday&time=12:00:00&num_results=3 +``` + +This request would return a list of upcoming trips for the 720 route in the weekday direction 1 at 12:00:00 with a limit of 3 results that are closest to the specified time. An example return value would look like this: + +```json +{ + "stop_times": + [["Central / 6th",["12:06:00","12:14:00","12:21:00"],"7201303_DEC23"],["5th / San Pedro",["12:09:00","12:17:00","12:24:00"],"7201303_DEC23"],["5th / Main",["12:11:00","12:19:00","12:26:00"],"7201303_DEC23"],["5th / Broadway",["12:12:00","12:20:00","12:27:00"],"13:29:00"],["Wilshire / Glendon",["13:19:00","13:26:00","13:34:00"],"7201303_DEC23"],["Veteran Federal Building",["13:21:00","13:36:00"],"7201303_DEC23"],["Wilshire / Veteran",["13:28:00"],"7201303_DEC23"],["Wilshire / Bonsall",["13:30:00"],"7201303_DEC23"],["Wilshire / Barrington",["13:33:00"],"7201303_DEC23"],["Wilshire / Bundy",["13:36:00"],"7201303_DEC23"],["Wilshire / 26th",["13:40:00"],"7201303_DEC23"],["Wilshire / 14th",["13:45:00"],"7201303_DEC23"],["4th / Arizona",["13:50:00"],"7201303_DEC23"]], +"geometries": + {"7201303_DEC23":"{\"type\":\"LineString\",\"coordinates\":[[-118.238495863,34.036918093],[-118.238495863,34.036918093],[-118.238495863,34.036918093],[-118.238495863,34.036918093]..."} +} +``` +The `stop_times` field contains a list of stop names, stop times, and trip IDs for the upcoming trips. The `geometries` field contains a dictionary of trip IDs and their corresponding line geometries. + +This endpoint is meant to be used for populating a schedules page with upcoming trips for a specific route, direction, day type, and time and replaces the former `{agency_id}/route_stops_grouped/{route_code}` endpoint which had the data returned as a single JSON object in the payload column, but did not include support for multiple trips or line geometries. + diff --git a/newendpoint.md b/newendpoint.md new file mode 100644 index 0000000..1ade1b6 --- /dev/null +++ b/newendpoint.md @@ -0,0 +1,115 @@ +# Adding a new endpoint to Metro API + +## Overview + +The Metro API uses [FastAPI](https://fastapi.tiangolo.com/) to create a RESTful API that provides access to real-time data, GTFS data, and other information related to the Los Angeles Metro system. It also has support for WebSocket connections to receive real-time updates about vehicles and trips. + +To add a new endpoint to the Metro API, you will need to create a new route in the `fastapi/app/main.py` file. This route will define the path and method for the new endpoint, as well as the logic for handling requests to that endpoint. + +| Endpoint Type | Description | Example Use Case | +| --- | --- | --- | +| RESTful API | Provides access to data using HTTP methods (GET, POST, PUT, DELETE) | GTFS static data| +| WebSocket | Allows real-time communication between the client and server | GTFS real-time data | + + +## Steps to Add a New Endpoint - RESTful API + +1. Open the `fastapi/app/main.py` file in your code editor. +2. Define a new route using the `@app.get` or `@app.post` decorator, depending on the HTTP method you want to use for the endpoint. +3. Specify the path for the new endpoint in the decorator, e.g., `@app.get("/new_endpoint")`. +4. Define a function that will handle requests to the new endpoint. This function should return the data you want to send back to the client. +5. Test the new endpoint by running the FastAPI development server and sending a request to the endpoint using a tool like [Postman](https://www.postman.com/). +6. Document the new endpoint in the API documentation to inform users about its purpose, required parameters, and expected response. + +### Example + +Here is an example of adding a new endpoint to the Metro API that returns a list of upcoming trips for a specific route, direction, day type, and time: + +```python +from fastapi import FastAPI + +app = FastAPI() +@app.get("/new_endpoint") +def get_upcoming_trips(route: str, direction: int, day_type: str, time: str): + # Logic to fetch upcoming trips based on route, direction, day type, and time + return {"route": route, "direction": direction, "day_type": day_type, "time": time, "trips": ["Trip 1", "Trip 2", "Trip 3"]} +``` + +In this example, the `get_upcoming_trips` function takes route, direction, day type, and time as parameters and returns a list of upcoming trips for that route. + +### Testing the New Endpoint + +To test the new endpoint, you can run the FastAPI development server using the following command inside of the `fastapi` directory: + +```bash +uvicorn main:app --reload +``` + +Alternatively, it is recommeded you use the provided `docker-compose` file to run the FastAPI development server, see the [README.md](Readme.md) for more information. + +Then, you can send a request to the new endpoint using a tool like Postman: + +``` +GET http://localhost:8000/new_endpoint?route=720&direction=1&day_type=Weekday&time=12:00:00 +``` + +This request would return a list of upcoming trips for the 720 route in the weekday direction 1 at 12:00:00. + +## Steps to Add a New Endpoint - WebSocket + +To add a new WebSocket endpoint to the Metro API, you will need to create a new route in the `fastapi/app/main.py` file that uses the `WebSocket` class from FastAPI. + +### Use Cases + +The websocket endpoint is mainly used as a pass-through for the Swiftly API, providing updates on vehicle positions or trip updates. The implementation uses reddis as a cache to store the data and the websocket to send the data to the client, meaning that only a single connection is needed to the Swiftly API for multiple clients. + +1. Open the `fastapi/app/main.py` file in your code editor. +2. Define a new route using the `@app.websocket` decorator. +3. Specify the path for the new WebSocket endpoint in the decorator, e.g., `@app.websocket("/new_websocket")`. +4. Define a function that will handle WebSocket connections to the new endpoint. This function should receive a `WebSocket` instance and handle incoming messages. +5. Test the new WebSocket endpoint by running the FastAPI development server and connecting to the endpoint using a WebSocket client. +6. Document the new WebSocket endpoint in the API documentation to inform users about its purpose and expected message format. +7. Implement logic to send real-time updates to connected clients based on incoming messages. + +### Example + +Here is an example of adding a new WebSocket endpoint to the Metro API that provides real-time updates about vehicle positions: + +```python +from fastapi import FastAPI, WebSocket + +app = FastAPI() + +@app.websocket("/new_websocket") +async def websocket_endpoint(websocket: WebSocket): + await websocket.accept() + while True: + data = await websocket.receive_text() + # Logic to process incoming messages and send real-time updates + await websocket.send_text(f"Received: {data}") +``` + +In this example, the `websocket_endpoint` function accepts WebSocket connections and sends back a message when it receives a text message from the client. + +### Testing the New WebSocket Endpoint + +To test the new WebSocket endpoint, you can run the FastAPI development server using the following command: + +```bash + +uvicorn main:app --reload +``` + + +Remember, it is recommeded you use the provided `docker-compose` file to run the FastAPI development server, see the [README.md](Readme.md) for more information. + + +Then, you can connect to the new WebSocket endpoint using a WebSocket client and send messages to receive real-time updates. + +## Conclusion + +By following these steps, you can add new RESTful API endpoints and WebSocket endpoints to the Metro API. This allows you to expand the functionality of the API and provide users with access to additional data and real-time updates. Remember to document the new endpoints in the API documentation to help users understand how to interact with them. + +For more information on FastAPI and WebSocket connections, refer to the [FastAPI documentation](https://fastapi.tiangolo.com/) and [WebSocket documentation](https://fastapi.tiangolo.com/advanced/websockets/). + + From a78bbc7436d75bda3846d92084a39483912cdf6b Mon Sep 17 00:00:00 2001 From: albertkun Date: Thu, 18 Jul 2024 15:16:40 -0700 Subject: [PATCH 2/2] removed blog --- .vscode/tasks.json | 32 +++---- .../blog/2021-08-01-mdx-blog-post.mdx | 20 ---- .../blog/2023-03-19-long-blog-post.md | 12 --- documentation/blog/2023-03-20.mdx | 29 ------ documentation/blog/2023-03-21.mdx | 14 --- documentation/blog/2023-03-29.mdx | 93 ------------------- documentation/blog/authors.yml | 11 --- 7 files changed, 16 insertions(+), 195 deletions(-) delete mode 100644 documentation/blog/2021-08-01-mdx-blog-post.mdx delete mode 100644 documentation/blog/2023-03-19-long-blog-post.md delete mode 100644 documentation/blog/2023-03-20.mdx delete mode 100644 documentation/blog/2023-03-21.mdx delete mode 100644 documentation/blog/2023-03-29.mdx delete mode 100644 documentation/blog/authors.yml diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 789cedb..d6a11a4 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -1,20 +1,20 @@ { "version": "2.0.0", "tasks": [ - { - "label": "Docker: Compose Up", - "type": "shell", - "command": "docker compose build && docker compose up", - "group": { - "kind": "build", - "isDefault": true - }, - "presentation": { - "reveal": "always", - "panel": "dedicated", - "showReuseMessage": false, - }, - "problemMatcher": [] - } - ] + { + "label": "Docker: Compose Up", + "type": "shell", + "command": "docker compose build && docker compose up", + "group": { + "kind": "build", + "isDefault": true + }, + "presentation": { + "reveal": "always", + "panel": "dedicated", + "showReuseMessage": false + }, + "problemMatcher": [] + } + ] } \ No newline at end of file diff --git a/documentation/blog/2021-08-01-mdx-blog-post.mdx b/documentation/blog/2021-08-01-mdx-blog-post.mdx deleted file mode 100644 index c8f8702..0000000 --- a/documentation/blog/2021-08-01-mdx-blog-post.mdx +++ /dev/null @@ -1,20 +0,0 @@ ---- -slug: mdx-blog-post -title: MDX Blog Post -authors: [nina] -tags: [docusaurus] ---- - -Blog posts support [Markdown features](https://docusaurus.io/docs/markdown-features), such as [MDX](https://mdxjs.com/). - -:::tip - -Use the power of React to create interactive blog posts. - -```js - -``` - - - -::: diff --git a/documentation/blog/2023-03-19-long-blog-post.md b/documentation/blog/2023-03-19-long-blog-post.md deleted file mode 100644 index 008def9..0000000 --- a/documentation/blog/2023-03-19-long-blog-post.md +++ /dev/null @@ -1,12 +0,0 @@ ---- -slug: long-blog-post -title: Long Blog Post -authors: [nina,albert] -tags: [documentation] ---- - -Welcome to the dev blog for Metro's API! - - - -More content to come! \ No newline at end of file diff --git a/documentation/blog/2023-03-20.mdx b/documentation/blog/2023-03-20.mdx deleted file mode 100644 index 8709cc4..0000000 --- a/documentation/blog/2023-03-20.mdx +++ /dev/null @@ -1,29 +0,0 @@ ---- -slug: api-documentation-launch -title: API 2.0 Documentation Launch 🚀 -authors: [albert] -tags: [documentation,api] ---- - -Metro API 2.0 has launched into BETA, which means we are scaling up for wide spread usage. If you have any issues, please head on over to the GitHub Issue's board and add an [issue](https://github.com/LACMTA/metro-api-v2/issues) - -## API Architecture - -The API is composed of the following: - -1. `data-loading-service` - the backend **docker** container that routinely executes Python scripts and [pandas](https://pandas.pydata.org/) to load data into the PostgreSQL database. -2. `api` - the frontend **docker** container that hosts the connections to the database using [fastapi](https://fastapi.tiangolo.com/). -3. `documentation` - a static generated website built with [docusaurus](https://docusaurus.io/). - - -```mermaid -graph TD; - id1(Data Loading Service)-->API; - API-->Documentation; -``` - -### Data - put into a PostgreSQL database -#### Data Sources -- GTFS Data - diff --git a/documentation/blog/2023-03-21.mdx b/documentation/blog/2023-03-21.mdx deleted file mode 100644 index a6ebc46..0000000 --- a/documentation/blog/2023-03-21.mdx +++ /dev/null @@ -1,14 +0,0 @@ ---- -slug: hello-world -title: Hello World! -authors: [nina] -tags: [general] ---- - -We'll be making regular blog updates as we work on the API and the documentation site. Our team is small but we've got big dreams and a desire to make our data more open and accessible. - -Keep an eye on this space as we post about changes, tutorials, etc. It's as much for your information as it is to keep ourselves accountable. - -Questions/comments are welcomed on our GitHub repository [discussions board](https://github.com/LACMTA/metro-api-v2/discussions). - -We'd love to hear your about any [bugs or issues](https://github.com/LACMTA/metro-api-v2/issues) as well! \ No newline at end of file diff --git a/documentation/blog/2023-03-29.mdx b/documentation/blog/2023-03-29.mdx deleted file mode 100644 index 52e08c1..0000000 --- a/documentation/blog/2023-03-29.mdx +++ /dev/null @@ -1,93 +0,0 @@ ---- -slug: websocket-to-me -title: WebSocket to Me! -authors: [albert] -tags: [api,websockets,update] ---- - -Hola Metro A-PIoneers! Today we are launching **2.1.21** of our API which is a minor version release that enables websockets! - -Currently, every 10 seconds the `live/trip_detail` websocket returns trip details for a route, alongside headsign and current stop information. - - - -You can find the URL of the websockets here: - -`wss://api.metro.net/{AGENCY_ID}/live/trip_detail/route_code/{ROUTE_CODE}?geojson={BOOLEAN}` - -- `{AGENCY_ID}` can either be `LACMTA` for bus or `LACMTA_Rail` for LACMTA_Rail -- `{ROUTE_CODE}` can be a line, like the `720` or `801` -- `{BOOLEAN}` for geojson value of `True` is ONLY supported right now. - -A working example WebSocket would be: -- `wss://api.metro.net/LACMTA/live/trip_detail/route_code/720?geojson=True` - -You can check this page to play around: -- https://api.metro.net/websocket_test - - -Feel free to request to see what else you'd like to see on the GitHub [issues board](https://github.com/LACMTA/metro-api-v2/issues)! - -### The devil is in the (WebSocket) details - -And gosh, were websockets a pain to implement!!! While FastAPI has good support for websockets, our API was not meant for asynchronous calls. - -Why did we need these asynchronous calls? Well, the data we wanted to send through websockets was live GTFS Real Time data that was also joined to the GTFS Static data. - -## Technology Stack - -The API needed to be basically re-done for async calls through `asyncpg` with `aysncio` and new updated `sqlalchemy` commands. - -### asyncpg -We needed to redo the database connection to listen for live updates, so we connected using asyncpg. - -``` python -from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession -``` - -We then re-did our SqlAlchemy engine to connect using the async url: -``` python -async_engine = create_async_engine(create_async_uri(Config.API_DB_URI), echo=False) -async_session = sessionmaker(async_engine, expire_on_commit=False, class_=AsyncSession) -``` - -Finally we created a new function to connect to the database: -``` python - -async def get_async_db(): - async with async_session() as db: - try: - yield db - finally: - await async_engine.dispose() -``` -Phew! - -### Asynchronously querying the data -Now that we were connected to the database, we had to re-do the queries! - -For async calls, SqlAlchemy uses `.select` instead of `.query` so our code went from: - -``` python - the_query = db.query(gtfs_models.VehiclePosition).filter(gtfs_models.VehiclePosition.agency_id == agency_id).all() -``` - -to: - -``` python -the_query = await session.execute(select(gtfs_models.VehiclePosition).where(gtfs_models.VehiclePosition.route_code == route_code,gtfs_models.VehiclePosition.agency_id == agency_id)) -``` - -And the results needed to be converted to `scalars`: -``` - for row in the_query.scalars().all(): - print(row) -``` - -All the code is in the API's `database.py`,`main.py`, `crud.py` here: https://github.com/LACMTA/metro-api-v2/tree/main/fastapi/app. - -## Conclusion - -After being pummeled by websockets for the past couple of weeks, all I can say is that it's been pretty painful. - -I just can't wait until someone comes up with `webmittens` next. \ No newline at end of file diff --git a/documentation/blog/authors.yml b/documentation/blog/authors.yml deleted file mode 100644 index 4fddcad..0000000 --- a/documentation/blog/authors.yml +++ /dev/null @@ -1,11 +0,0 @@ -albert: - name: Albert Kochaphum - title: API Guy - url: https://github.com/albertkun - image_url: https://avatars.githubusercontent.com/u/8574425?v=4 - -nina: - name: Nina Kin - title: Head Digital Honcho - url: https://github.com/matikin9 - image_url: https://avatars.githubusercontent.com/u/1873072?v=4