base
Base REST API ResourcesLink
Provides Flask‑RESTful resources that expose the PlantDB database through a well‑documented HTTP API. The module bundles endpoints for health checks, metadata queries, file access, and on‑demand database reloading, all protected by configurable rate‑limiting to safeguard the service.
Key FeaturesLink
- Health check endpoint - quickly verify that the service and underlying database are operational.
- Dynamic database refresh - reload a single scan or the entire dataset without restarting the server.
- Self‑describing root resource - returns API name, description, version information, and a list of all available routes.
Usage ExamplesLink
Hereafter is a minimal working example that:
- Creates a
Flaskapp - Sets up a local test database with a JSON Web Token session manager
- Registers the
LoginandLogoutresources to a REST API - Starts the app
>>> import logging
>>> from flask import Flask
>>> from flask_restful import Api
>>> from plantdb.server.api.base import Home, HealthCheck
>>> from plantdb.commons.auth.session import JWTSessionManager
>>> from plantdb.commons.fsdb.core import FSDB
>>> from plantdb.commons.test_database import setup_test_database
>>> # Create a Flask application
>>> app = Flask(__name__)
>>> # Create a logger
>>> logger = logging.getLogger("plantdb.base")
>>> logger.setLevel(logging.INFO)
>>> # Initialize a test database with a JWTSessionManager
>>> db_path = setup_test_database('real_plant')
>>> mgr = JWTSessionManager()
>>> db = FSDB(db_path, session_manager=mgr)
>>> db.connect()
>>> # RESTful API and resource registration
>>> api = Api(app)
>>> api.add_resource(Home, "/")
>>> api.add_resource(HealthCheck, "/health", resource_class_kwargs={"db": db})
>>> # Start the APP
>>> app.run(host='0.0.0.0', port=5000)
It may be used as follows (in another Python REPL): ```python
import requests
Check if the user exists (valid username):Link
response = requests.get("http://127.0.0.1:5000/") print(response.json()['name']) PlantDB REST API
Check server statusLink
response = requests.get("http://127.0.0.1:5000/health") print(response.json()['status']) healthy
HealthCheck
Link
Bases: Resource
Simple health‑check resource exposing an endpoint that verifies the API and its database connectivity.
Attributes:
| Name | Type | Description |
|---|---|---|
db |
FSDB
|
The database providing the resources to serve. |
logger |
Logger
|
The logger used to record operations and errors. |
Initialize the resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
A database instance providing the resources to serve. |
required |
|
Logger
|
A logger instance to record operations and errors. |
None
|
Source code in plantdb/server/api/base.py
195 196 197 198 199 200 201 202 203 204 205 206 | |
get
Link
get()
Simple test endpoint to verify the API is working correctly.
Raises:
| Type | Description |
|---|---|
HTTPException
|
If the rate limit is exceeded, it returns an HTTP 429 ("Too Many Requests") response to the client. |
Source code in plantdb/server/api/base.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
Home
Link
Bases: Resource
get
Link
get()
Return basic API information and documentation.
Raises:
| Type | Description |
|---|---|
HTTPException
|
If the rate limit is exceeded, it returns an HTTP 429 ("Too Many Requests") response to the client. |
Source code in plantdb/server/api/base.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
Refresh
Link
Bases: Resource
RESTful resource for reloading the database on demand.
A concrete implementation of Flask-RESTful Resource that provides an endpoint to force reload the plant database. This is useful when the underlying data has changed and needs to be refreshed in the running application.
Attributes:
| Name | Type | Description |
|---|---|---|
db |
FSDB
|
The database providing the resources to serve. |
logger |
Logger
|
The logger used to record operations and errors. |
Initialize the resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
FSDB
|
A database instance providing the resources to serve. |
required |
|
Logger
|
A logger instance to record operations and errors. |
None
|
Source code in plantdb/server/api/base.py
250 251 252 253 254 255 256 257 258 259 260 261 | |
get
Link
get()
Force the plant database to reload.
This endpoint triggers a reload of the plant database data. It can either reload the entire database or selectively reload data for a specific plant scan.
Returns:
| Type | Description |
|---|---|
Response
|
A Response object with:
|
Raises:
| Type | Description |
|---|---|
FilesetNotFoundError
|
If the specified scan_id refers to a non-existent fileset |
ScanNotFoundError
|
If the specified scan_id refers to a non-existent scan |
Exception
|
For any other unexpected errors during reload |
Notes
- In the URL, you can use the
scan_idparameter to reload a specific scan. - If no scan_id is provided, reloads the entire database.
- This endpoint has a request rate-limit to prevent excessive database reloads.
See Also
plantdb.server.core.security.rate_limit plantsb.fsdb.FSDB.reload
Examples:
>>> # Start the REST API server (in test mode)
>>> # fsdb_rest_api --test
>>> import requests
>>> # Refresh the entire database
>>> response = requests.get("http://127.0.0.1:5000/refresh")
>>> response.status_code
200
>>>
>>> # Refresh a specific scan
>>> response = requests.get("http://127.0.0.1:5000/refresh?scan_id=real_plant")
>>> response.status_code
200
Source code in plantdb/server/api/base.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | |
get_full_database
Link
get_full_database()
Reload the entire plant database.
Returns:
| Type | Description |
|---|---|
(dict, int)
|
A dictionary with a success message and HTTP status code 200, or an error message and status code 500 |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If the rate limit is exceeded, it returns an HTTP 429 ("Too Many Requests") response to the client. |
Source code in plantdb/server/api/base.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | |
get_specific_scan
Link
get_specific_scan(scan_id)
Reload data for a specific scan in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Identifier for the specific plant scan to reload |
required |
Returns:
| Type | Description |
|---|---|
(dict, int)
|
A dictionary with a success message and HTTP status code 200, or an error message and status code 500 |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If the rate limit is exceeded, it returns an HTTP 429 ("Too Many Requests") response to the client. |
Source code in plantdb/server/api/base.py
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | |