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 from plantdb.commons import api_endpoints
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
242 243 244 245 246 247 248 249 250 251 252 253 | |
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. |
Examples:
>>> # Start the REST API server (in test mode)
>>> from plantdb.server.test_rest_api import TestRestApiServer
>>> # Create a test database and start the Flask App serving a REST API
>>> server = TestRestApiServer(test=True)
>>> server.start()
>>> import requests
>>> from plantdb.commons import api_endpoints
>>> response = requests.get("http://127.0.0.1:5000" + api_endpoints.health())
>>> response.ok
True
>>> # Stop the test server
>>> server.stop()
Source code in plantdb/server/api/base.py
255 256 257 258 259 260 261 262 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 289 290 291 292 293 294 295 296 | |
Home
Link
Home(db, logger=None, deploy_prefix='')
Bases: Resource
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
|
|
str
|
Deployment (reverse-proxy) prefix prepended before |
''
|
Source code in plantdb/server/api/base.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
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
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
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
314 315 316 317 318 319 320 321 322 323 324 325 | |
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)
>>> from plantdb.server.test_rest_api import TestRestApiServer
>>> # Create a test database and start the Flask App serving a REST API
>>> server = TestRestApiServer(test=True)
>>> server.start()
>>> import requests
>>> from plantdb.commons import api_endpoints
>>> response = requests.get("http://127.0.0.1:5000" + api_endpoints.refresh())
>>> response.ok
True
>>> response = requests.get("http://127.0.0.1:5000" + api_endpoints.refresh('real_plant'))
>>> response.ok
True
>>> # Stop the test server
>>> server.stop()
Source code in plantdb/server/api/base.py
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | |
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
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | |
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
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 | |