API¶
- class msdss_base_api.core.API(api=<fastapi.applications.FastAPI object>, logger=<Logger uvicorn.error (WARNING)>)[source]¶
Base class for mSDSS related APIs.
- Parameters
api (
fastapi:fastapi.FastAPI
) – API object to use for creating routes.logger (
logging.Logger
) – Object for logging. Instantiated withlogging.getLogger()
.
- api¶
API object passed from parameter
api
.- Type
fastapi.FastAPI
- logger¶
Object for logging from parameter
logger
.- Type
- routes¶
List of dictionaries, where each dictionary represents arguments for adding a route:
method
(str): request method for routepath
(str): path for routefunc
(func): function for routeargs
(tuple): positional arguments passed to the FastAPI router from callingmsdss_base_api.core.add_route()
kwargs
(dict): keyword arguments passed to the FastAPI router from callingmsdss_base_api.core.add_route()
These arguments can be used to reproduce the added route to another app.
- routers¶
List of dictionaries, where each dictionary represents arguments for adding a router:
router
(fastapi:fastapi.routing.APIRouter
): FastAPI router object from callingmsdss_base_api.core.add_router()
args
(tuple): positional arguments passed to the FastAPI router from callingmsdss_base_api.core.add_router()
kwargs
(dict): keyword arguments passed to the FastAPI router from callingmsdss_base_api.core.add_router()
These arguments can be used to reproduce the added router to another app.
- events¶
List of dictionaries, where each dictionary represents arguments for adding an event:
event
(str): the event type to handlefunc
(func): the function to call for the event
These arguments can be used to reproduce the added event to another app.
Author
Richard Wen <rrwen.dev@gmail.com>
Example
from msdss_base_api.core import API app = API() # Add route via function def hello_world(): app.logger.info('/ accessed!') return "hello world!" app.add_route("GET", "/", hello_world) # Add route via decorator @app.route("GET", "/two") def hello_world2(): app.logger.info('/two accessed!') return "hello world 2!" # Run the app with app.start() # API is hosted at http://localhost:8000 # app.start()
add_apps¶
- API.add_apps(*apps, add_events=True, add_routes=True, add_routers=True)[source]¶
Combines multiple apps by:
Combining app events via
msdss_base_api.core.API.add_app_events()
Combining app routes via
msdss_base_api.core.API.add_app_routes()
Combining app routers via
msdss_base_api.core.API.add_app_routers()
- Parameters
*apps (
msdss_base_api.core.API
) – Apps to combine.add_events (bool) – Whether to combine events for each app or not.
add_routes (bool) – Whether to combine routes for each app or not.
add_routers (bool) – Whether to combine routers for each app or not.
Author
Richard Wen <rrwen.dev@gmail.com>
Example
from msdss_base_api.core import API from pprint import pprint # Create apps app1 = API() app2 = API() app3 = API() # Add routes @app1.route('GET', '/start1') def hello_world1(): return "hello world 1!" @app2.route('GET', '/start2') def hello_world2(): return "hello world 2!" @app3.route('GET', '/start3') def hello_world3(): return "hello world 3!" # Create routers router1 = app1.create_router( prefix='/helloworld1' ) router2 = app2.create_router( prefix='/helloworld2' ) router3 = app3.create_router( prefix='/helloworld3' ) # Add router routes @router1.get('/start1') def hello_world1(): return "hello world 1!" @router2.get('/start2') def hello_world2(): return "hello world 2!" @router3.get('/start3') def hello_world3(): return "hello world 3!" # Add routers app1.add_router(router1, tags=['helloworld1']) app2.add_router(router2, tags=['helloworld2']) app3.add_router(router3, tags=['helloworld3']) # Add events @app1.event("startup") def startup1(): print("startup 1!") @app2.event("startup") def startup2(): print("startup 2!") @app3.event("shutdown") def shutdown(): print("shutdown!") # Combine apps together into app1 app1.add_apps(app2, app3) # Check main app event arguments print('app1 combined events:\n') pprint(app1.events) print('\napp1 combined routes:\n') pprint(app1.routes) print('\napp1 combined routers:\n') pprint(app1.routers) # Run the app with app1.start() # API is hosted at http://localhost:8000 # app1.start()
app1 combined events: [{'event': 'startup', 'func': <function startup1 at 0x7fedc8680ee0>}, {'event': 'startup', 'func': <function startup2 at 0x7fedc8680f70>}, {'event': 'shutdown', 'func': <function shutdown at 0x7fedc869b040>}] app1 combined routes: [{'args': (), 'func': <function hello_world1 at 0x7fedc86803a0>, 'kwargs': {}, 'method': 'GET', 'path': '/start1'}, {'args': (), 'func': <function hello_world2 at 0x7fedc8680550>, 'kwargs': {}, 'method': 'GET', 'path': '/start2'}, {'args': (), 'func': <function hello_world3 at 0x7fedc8680310>, 'kwargs': {}, 'method': 'GET', 'path': '/start3'}] app1 combined routers: [{'args': (), 'kwargs': {'tags': ['helloworld1']}, 'router': <fastapi.routing.APIRouter object at 0x7fedc869f4c0>}, {'args': (), 'kwargs': {'tags': ['helloworld2']}, 'router': <fastapi.routing.APIRouter object at 0x7fedc869f430>}, {'args': (), 'kwargs': {'tags': ['helloworld3']}, 'router': <fastapi.routing.APIRouter object at 0x7fedc869f5e0>}]
add_app_events¶
- API.add_app_events(*apps)[source]¶
Combines event functions from several apps to the API via
msdss_base_api.core.API.add_event()
using theevents
attribute.- Parameters
*apps (
msdss_base_api.core.API
) – Apps to add events from.
Author
Richard Wen <rrwen.dev@gmail.com>
Example
from msdss_base_api.core import API from pprint import pprint # Create apps app1 = API() app2 = API() app3 = API() # Add events @app1.event("startup") def startup1(): print("startup 1!") @app2.event("startup") async def startup2(): print("startup 2!") @app3.event("shutdown") def shutdown(): print("shutdown!") # Add events from other apps to main app app1.add_app_events(app2, app3) # Check main app event arguments print('app1 startup event combined with app2 startup event:\n') pprint(app1.events[0]) print('\napp3 shutdown event added to app1:\n') pprint(app1.events[1]) # Run the app with app1.start() # API is hosted at http://localhost:8000 # app1.start()
app1 startup event combined with app2 startup event: {'event': 'startup', 'func': <function startup1 at 0x7fedc869bee0>} app3 shutdown event added to app1: {'event': 'startup', 'func': <function startup2 at 0x7fedc869bf70>}
add_app_routes¶
- API.add_app_routes(*apps)[source]¶
Adds routes from several apps to the API via
msdss_base_api.core.API.add_route()
.- Parameters
*apps (
msdss_base_api.core.API
) – Apps to add routes from.
Author
Richard Wen <rrwen.dev@gmail.com>
Example
from msdss_base_api.core import API from pprint import pprint # Create apps app1 = API() app2 = API() app3 = API() # Add routes @app1.route('GET', '/start1') def hello_world1(): return "hello world 1!" @app2.route('GET', '/start2') def hello_world2(): return "hello world 2!" @app3.route('GET', '/start3') def hello_world3(): return "hello world 3!" # Add routes from other apps to main app app1.add_app_routes(app2, app3) # Check main app route arguments print('app1 route:\n') pprint(app1.routes[0]) print('\napp2 route added to app1:\n') pprint(app1.routes[1]) print('\napp3 route added to app1:\n') pprint(app1.routes[2]) # Run the app with app1.start() # API is hosted at http://localhost:8000 # app1.start()
app1 route: {'args': (), 'func': <function hello_world1 at 0x7fedc869b9d0>, 'kwargs': {}, 'method': 'GET', 'path': '/start1'} app2 route added to app1: {'args': (), 'func': <function hello_world2 at 0x7fedc869bc10>, 'kwargs': {}, 'method': 'GET', 'path': '/start2'} app3 route added to app1: {'args': (), 'func': <function hello_world3 at 0x7fedc869bd30>, 'kwargs': {}, 'method': 'GET', 'path': '/start3'}
add_app_routers¶
- API.add_app_routers(*apps)[source]¶
Adds routers from several apps to the API via
msdss_base_api.core.API.add_router()
.- Parameters
*apps (
msdss_base_api.core.API
) – Apps to add routers from.
Author
Richard Wen <rrwen.dev@gmail.com>
Example
from msdss_base_api.core import API from pprint import pprint # Create apps app1 = API() app2 = API() app3 = API() # Create routers router1 = app1.create_router( prefix='/helloworld1' ) router2 = app2.create_router( prefix='/helloworld2' ) router3 = app3.create_router( prefix='/helloworld3' ) # Add routes @router1.get('/start1') def hello_world1(): return "hello world 1!" @router2.get('/start2') def hello_world2(): return "hello world 2!" @router3.get('/start3') def hello_world3(): return "hello world 3!" # Add routers app1.add_router(router1, tags=['helloworld1']) app2.add_router(router2, tags=['helloworld2']) app3.add_router(router3, tags=['helloworld3']) # Add routers from other apps to main app app1.add_app_routers(app2, app3) # Check main app router arguments print('app1 router:\n') pprint(app1.routers[0]) print('\napp2 router added to app1:\n') pprint(app1.routers[1]) print('\napp3 router added to app1:\n') pprint(app1.routers[2]) # Run the app with app1.start() # API is hosted at http://localhost:8000 # app1.start()
app1 router: {'args': (), 'kwargs': {'tags': ['helloworld1']}, 'router': <fastapi.routing.APIRouter object at 0x7fedc867e580>} app2 router added to app1: {'args': (), 'kwargs': {'tags': ['helloworld2']}, 'router': <fastapi.routing.APIRouter object at 0x7fedc867ef10>} app3 router added to app1: {'args': (), 'kwargs': {'tags': ['helloworld3']}, 'router': <fastapi.routing.APIRouter object at 0x7fedc86a2220>}
add_event¶
- API.add_event(event, func)[source]¶
Handles API events using a custom function.
Also adds the arguments passed to the attribute
events
.- Parameters
event (str) – The event to handle. Currently supports
startup
(before starting) andshutdown
(during shutdown).func (function) – Function to execute when the event occurs.
Author
Richard Wen <rrwen.dev@gmail.com>
Example
from msdss_base_api.core import API from pprint import pprint app = API() def before_startup(): print("This is run before startup.") app.add_event("startup", before_startup) def during_shutdown(): print("This is run during shutdown.") app.add_event("shutdown", during_shutdown) # Check router arguments print('startup\n') pprint(app.events[0]) print('\nshutdown\n') pprint(app.events[1]) # Run the app with app.start() # API is hosted at http://localhost:8000 # app.start()
startup {'event': 'startup', 'func': <function before_startup at 0x7fedc86b6f70>} shutdown {'event': 'shutdown', 'func': <function during_shutdown at 0x7fedc86b6ee0>}
add_route¶
- API.add_route(method, path, func, *args, **kwargs)[source]¶
Add a route to the API.
- Parameters
method (str) – HTTP request method (GET, POST, PUT, DELETE, etc).
path (str) – Path (e.g. “/”) of the route for the API.
func (function) – Function to execute when route is reached.
*args – Additional arguments passed to the
fastapi:fastapi.FastAPI.add_route()
method.**kwargs – Additional arguments passed to the
fastapi:fastapi.FastAPI.add_route()
method.
Author
Richard Wen <rrwen.dev@gmail.com>
Example
from msdss_base_api.core import API from pprint import pprint app = API() # Add route def hello_world(): return "hello world!" app.add_route("GET", "/", hello_world) # Check route arguments pprint(app.routes[0])
{'args': (), 'func': <function hello_world at 0x7fedc867bdc0>, 'kwargs': {}, 'method': 'GET', 'path': '/'}
add_router¶
- API.add_router(router, *args, **kwargs)[source]¶
Add a router to the API.
Also adds the arguments passed to the attribute
routers
.- Parameters
router (
fastapi:fastapi.routing.APIRouter
) – FastAPI router object to add.*args – Additional arguments passed to the meth:fastapi:fastapi.FastAPI.include_router method. See FastAPI bigger apps
**kwargs –
Additional arguments passed to the meth:fastapi:fastapi.FastAPI.include_router method. See FastAPI bigger apps
Author
Richard Wen <rrwen.dev@gmail.com>
Example
from msdss_base_api.core import API from pprint import pprint app = API() # Create the router router = app.create_router( prefix='/helloworld' ) # Add a route @router.get('/start') def hello_world(): return "hello world!" # Add router to app app.add_router(router, tags=['helloworld']) # Check router arguments pprint(app.routers[0]) # Run the app with app.start() # API is hosted at http://localhost:8000 # app.start()
{'args': (), 'kwargs': {'tags': ['helloworld']}, 'router': <fastapi.routing.APIRouter object at 0x7fedc86a4850>}
create_router¶
- API.create_router(*args, **kwargs)[source]¶
Create a router.
- Parameters
*args –
Additional arguments passed to the class:fastapi:fastapi.routing.APIRouter class. See FastAPI bigger apps
**kwargs –
Additional arguments passed to the class:fastapi:fastapi.routing.APIRouter class. See FastAPI bigger apps
- Returns
A router object used for organizing larger applications and for modularity.
- Return type
fastapi:fastapi.routing.APIRouter
Author
Richard Wen <rrwen.dev@gmail.com>
Example
from msdss_base_api.core import API app = API() # Create the router router = app.create_router( prefix='/helloworld', tags=['helloworld'] )
event¶
- API.event(event, *args, **kwargs)[source]¶
Decorator function for handling events.
- Parameters
event (str) – The event to handle. Currently supports
startup
(before starting) andshutdown
(during shutdown).*args – Additional arguments passed to the
fastapi:fastapi.FastAPI.add_event_handler()
method.**kwargs – Additional arguments passed to the
fastapi:fastapi.FastAPI.add_event_handler()
method.
Author
Richard Wen <rrwen.dev@gmail.com>
Example
from msdss_base_api.core import API app = API() @app.event("startup") def before_startup(): print("This is run before startup.") @app.event("shutdown") def during_shutdown(): print("This is run during shutdown.") # Run the app with app.start() # API is hosted at http://localhost:8000 # app.start()
route¶
- API.route(method, path, *args, **kwargs)[source]¶
Decorator function for adding routes.
Also adds the arguments passed to the attribute
routes
.- Parameters
Author
Richard Wen <rrwen.dev@gmail.com>
Example
from msdss_base_api.core import API app = API() @app.route("GET", "/") def hello_world(): return "hello world!"
start¶
- API.start(host='127.0.0.1', port=8000, log_level='info', *args, **kwargs)[source]¶
Starts a server to host the API.
- Parameters
Author
Richard Wen <rrwen.dev@gmail.com>
Example
from msdss_base_api.core import API app = API() def hello_world(): return "hello world!" app.add_route("GET", "/", hello_world) # Run the app with app.start() # API is hosted at http://localhost:8000 # app.start()