|
1 | | -# Open Bus Stride API |
| 1 | +# 🚌 Open Bus Stride API |
2 | 2 |
|
3 | | -API For the [Open Bus Stride project](https://open-bus-map-search.hasadna.org.il/dashboard) |
| 3 | +**An API for the [Open Bus Stride Project](https://open-bus-map-search.hasadna.org.il/dashboard)** |
4 | 4 |
|
5 | | -* Please report issues and feature requests [here](https://github.com/hasadna/open-bus/issues/new) |
6 | | -* To get updates about the system status and for general help join Hasadna's Slack #open-bus channel ([Hasadna Slack signup link](https://join.slack.com/t/hasadna/shared_invite/zt-167h764cg-J18ZcY1odoitq978IyMMig)) |
| 5 | +## 📢 Get Involved |
7 | 6 |
|
8 | | -See [our contributing docs](https://github.com/hasadna/open-bus-pipelines/blob/main/CONTRIBUTING.md) if you want to suggest changes to this repository. |
| 7 | +- 💬 For general help and system updates, join the Hasadna Slack: [#open-bus channel](https://join.slack.com/t/hasadna/shared_invite/zt-167h764cg-J18ZcY1odoitq978IyMMig) |
| 8 | +- 🐞 Found a bug or have a feature request? [Open an issue](https://github.com/hasadna/open-bus-map-search/issues/new) |
| 9 | +- 🤝 Want to contribute? See our [contributing guidelines](https://github.com/hasadna/open-bus-pipelines/blob/main/CONTRIBUTING.md) |
9 | 10 |
|
| 11 | +## 🔗 Related Projects |
10 | 12 |
|
11 | | -## Tech Stack |
| 13 | +### Frontend |
12 | 14 |
|
13 | | -- [python](https://www.python.org/) |
14 | | -- [fastAPI framework](https://fastapi.tiangolo.com/) |
15 | | -- [postgresql](https://www.postgresql.org/) |
| 15 | +- [🗺️ Open Bus Map Search (Client App)](https://github.com/hasadna/open-bus-map-search) - [Live Website](https://open-bus-map-search.hasadna.org.il/dashboard) |
| 16 | +- [📦 Open Bus API Client (API Package Generator)](https://github.com/hasadna/open-bus-api-client) - [NPM Package](https://www.npmjs.com/package/@hasadna/open-bus-api-client) |
16 | 17 |
|
| 18 | +### Backend |
17 | 19 |
|
18 | | -## Development using the Docker Compose environment |
| 20 | +- [🏗️ Open Bus Pipelines](https://github.com/hasadna/open-bus-pipelines) |
| 21 | +- [🌐 Open Bus Stride API (REST)](https://github.com/hasadna/open-bus-stride-api) – [API Docs](https://open-bus-stride-api.hasadna.org.il/docs) |
| 22 | +- [🧾 Open Bus Stride DB](https://github.com/hasadna/open-bus-stride-db) |
| 23 | +- [🔧 Open Bus Stride ETL](https://github.com/hasadna/stride-etl) |
| 24 | +- [📚 Open Bus GTFS ETL](https://github.com/hasadna/gtfs-etl) |
| 25 | +- [📡 Open Bus SIRI Requester](https://github.com/hasadna/siri-requester) |
| 26 | +- [🧪 Open Bus SIRI ETL](https://github.com/hasadna/siri-etl) |
19 | 27 |
|
20 | | -This is the easiest option to start development, follow these instructions: https://github.com/hasadna/open-bus-pipelines/blob/main/README.md#stride-api |
| 28 | +## 🛠️ Tech Stack |
21 | 29 |
|
22 | | -For local development, see the additional functionality section: `Develop stride-api from a local clone` |
| 30 | +- [Python](https://www.python.org/) |
| 31 | +- [FastAPI](https://fastapi.tiangolo.com/) |
| 32 | +- [PostgreSQL](https://www.postgresql.org/) |
23 | 33 |
|
24 | | -## Local Development |
| 34 | +## 🚀 Getting Started |
25 | 35 |
|
26 | | -It's much easier to use the Docker Compose environment, but the following can be |
27 | | -refferd to for more details regarding the internal processes and for development |
28 | | -using your local Python interpreter. |
| 36 | +### Option 1: Using Docker Compose (Recommended) |
29 | 37 |
|
30 | | -### Install |
| 38 | +The easiest way to get up and running is with Docker Compose. |
| 39 | +Follow the instructions [here](https://github.com/hasadna/open-bus-pipelines/blob/main/README.md#stride-api). |
31 | 40 |
|
32 | | -Create a virtualenv (using Python 3.8) |
| 41 | +For local development (e.g. making changes to the code), also see: |
| 42 | +**`Develop stride-api from a local clone`** in the same document. |
33 | 43 |
|
34 | | -``` |
35 | | -python3.8 -m venv venv |
36 | | -``` |
| 44 | +### Option 2: Local Development (Manual Setup) |
37 | 45 |
|
38 | | -Update pip |
| 46 | +Use this if you prefer to run directly from your local Python environment. |
39 | 47 |
|
40 | | -``` |
41 | | -venv/bin/pip install --upgrade pip |
42 | | -``` |
| 48 | +#### 1. Clone and Setup |
43 | 49 |
|
44 | | -You should have a copy of open-bus-stride-db repository at ../open-bus-stride-db |
| 50 | +Ensure you have **Python 3.8** installed. |
45 | 51 |
|
46 | | -Install dependencies |
| 52 | +```bash |
| 53 | +python3.8 -m venv venv |
| 54 | +source venv/bin/activate # On Windows: venv\Scripts\activate |
| 55 | +pip install --upgrade pip |
| 56 | +pip install -r requirements-dev.txt |
| 57 | +``` |
| 58 | + |
| 59 | +Ensure a local clone of [open-bus-stride-db](https://github.com/hasadna/open-bus-stride-db) exists at: |
47 | 60 |
|
48 | 61 | ``` |
49 | | -venv/bin/pip install -r requirements-dev.txt |
| 62 | +../open-bus-stride-db |
50 | 63 | ``` |
51 | 64 |
|
52 | | -### Use |
| 65 | +#### 2. Database Connection |
53 | 66 |
|
54 | | -You need a DB to connect to, there are 2 options here: |
| 67 | +You’ll need a database. You have two options: |
55 | 68 |
|
56 | | -* Start the stride-db from open-bus-pipelines docker-compose environment |
57 | | -* Connect to the production DB using the Redash read-only credentials |
58 | | - * Create a `.env` file with the following, replacing the url to the production redash read-only url: `export SQLALCHEMY_URL=postgresql://postgres:123456@localhost` |
59 | | - * Source the .env: `. .env` |
| 69 | +- **Option A**: Start `stride-db` using the Docker Compose environment from [open-bus-pipelines](https://github.com/hasadna/open-bus-pipelines). |
| 70 | +- **Option B**: Connect to the production DB using Redash read-only credentials. |
60 | 71 |
|
61 | | -Activate virtualenv |
| 72 | +Create a `.env` file: |
62 | 73 |
|
63 | | -``` |
64 | | -. venv/bin/activate |
| 74 | +```env |
| 75 | +export SQLALCHEMY_URL=postgresql://postgres:123456@localhost |
65 | 76 | ``` |
66 | 77 |
|
67 | | -Start the FastAPI server with automatic reload on changes |
| 78 | +Then source it: |
68 | 79 |
|
| 80 | +```bash |
| 81 | +. .env |
69 | 82 | ``` |
| 83 | + |
| 84 | +#### 3. Run the API Server |
| 85 | + |
| 86 | +```bash |
70 | 87 | uvicorn open_bus_stride_api.main:app --reload |
71 | 88 | ``` |
72 | 89 |
|
73 | | -See the API docs at http://localhost:8000/docs |
| 90 | +Access the interactive API docs at: |
| 91 | +👉 [http://localhost:8000/docs](http://localhost:8000/docs) |
74 | 92 |
|
75 | | -### Manage APIs |
| 93 | +## 🧩 API Development |
76 | 94 |
|
77 | | -All existing APIs from the docs are defined under ```open_bus_stride_api/routers/<base_router_name>.py``` |
| 95 | +All routes are defined in: |
78 | 96 |
|
79 | | -Follow this example to create or edit a simple API for a specific table with filters: |
| 97 | +``` |
| 98 | +open_bus_stride_api/routers/<base_router_name>.py |
| 99 | +``` |
80 | 100 |
|
| 101 | +### ✍️ Example API |
81 | 102 |
|
82 | | -``` |
83 | | -@router.<http_method>("/<api_path>", tags=[<file_name>], response_model=<pydantic_response_model>) // |
84 | | -def name(<filtering_params>, limit, offset): # always include limit & offest to allow easy iteration on the data |
| 103 | +```python |
| 104 | +@router.<http_method>("/<api_path>", tags=["<tag_name>"], response_model=<ResponseModel>) |
| 105 | +def name(<filtering_params>, limit, offset): |
85 | 106 | return common.get_list( |
86 | | - <db_model>, limit, offset, |
87 | | - [ |
88 | | - {'type': <filter_type>, 'field': <db_stcruct>.<specific_id>, 'value': <filter_param>}, |
| 107 | + <DBModel>, limit, offset, |
| 108 | + [ |
| 109 | + {'type': <filter_type>, 'field': <DBModel>.<field>, 'value': <filter_param>}, |
89 | 110 | ] |
90 | 111 | ) |
91 | 112 | ``` |
92 | 113 |
|
93 | | -Filter types are defined at ```open_bus_stride_api/routers/common.py -> get_list_query_filter_<filter_type>``` (e.g.: 'equal', 'date_in_range') |
| 114 | +- **Filters**: Defined in `routers/common.py` → `get_list_query_filter_<filter_type>` (Examples: `'equal'`, `'date_in_range'`) |
94 | 115 |
|
95 | | -### Running Tests |
| 116 | +Always include `limit` and `offset` for pagination. |
96 | 117 |
|
97 | | -Install for local development as described above. |
| 118 | +## 🧪 Running Tests |
98 | 119 |
|
99 | | -Install test requirements: |
| 120 | +Set up your environment as described in the **Local Development** section above. |
100 | 121 |
|
101 | | -``` |
| 122 | +Install test dependencies: |
| 123 | + |
| 124 | +```bash |
102 | 125 | pip install -r tests/requirements.txt |
103 | 126 | ``` |
104 | 127 |
|
105 | | -To run the tests you need to connect to a DB with full stride data, |
106 | | -easiest way is to connect to the production DB as described above by |
107 | | -setting the SQLALCHEMY_URL env var accordingly. |
| 128 | +Ensure the DB contains full Stride data (production read-only access works). |
108 | 129 |
|
109 | | -Run all tests with full output, exiting on first error: |
| 130 | +Run tests: |
110 | 131 |
|
111 | | -``` |
| 132 | +```bash |
112 | 133 | pytest -svvx |
113 | 134 | ``` |
114 | | - |
115 | | -Pytest has many options, see the help message for details. |
116 | | - |
117 | | - |
118 | | -### Link To The Client Repo |
119 | | -- [client repo](https://github.com/hasadna/open-bus-map-search) |
|
0 commit comments