PyXMake Developer Guide: src/PyXMake/VTL/api.py Source File

| PyXMake Developer Guide 1.0 PyXMake

Loading...
Searching...
No Matches
api.py
1# -*- coding: utf-8 -*-
2# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3# % PyXMake - API setup environment for PyXMake %
4# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5"""
6API setup example for PyXMake.
7
8@note: Run HTML APIs of PyXMake (in a Docker container or locally).
9Created on 28.02.2020
10
11@version: 1.0
12----------------------------------------------------------------------------------------------
13@requires:
14 - PyXMake
15
16@change:
17 -
18
19@author: garb_ma [DLR-SY,STM Braunschweig]
20----------------------------------------------------------------------------------------------
21"""
22import os, sys
23import posixpath
24import platform
25
26try:
27 import PyXMake
28except ImportError:
29 # Script is executed as a plug-in
30 sys.path.insert(0,os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
31 import PyXMake
32finally:
33 from PyXMake.API import Frontend #@UnresolvedImport
34
35 def main(
36 handle,
37 # Host route and port information
38 Hostname=str(platform.node()), PortID=8020,
39 # Additional keyword arguments
40 **kwargs):
41 """
42 Main function to execute an API handle
43 """
44 # Local import of all required packages
45 import uvicorn
46
47 from PyXMake.Build.Make import Coverage
48
49 # Test if the API can be created. This does not test all functions.
50 if Coverage.show() or kwargs.get("dry_run",False):
51 # Backwards compatibility. TestClient requires non-default additional packages
52 from fastapi.testclient import TestClient
53 # Run the server in a test environment
54 client = TestClient(handle())
55 # Test if API can be reached
56 assert client.get(posixpath.sep).status_code == 200
57 # Finish
58 print("==================================")
59 print("Finished running API check")
60 print("==================================")
61 # Return success code
62 return 0
63 # Execute API directly
64 else:
65 # Run the supplied API
66 uvicorn.run(handle(), host=Hostname, port=PortID)
67 # Will run forever until quit by the user
68 pass
69
70 def handle():
71 """
72 Return current API's main instance as an sub API.
73 """
74 API = Frontend()
75 API.RedirectException(posixpath.sep.join(["",str(PyXMake.__name__),"api","documentation"]))
76 return API.create()
77
78# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79# % Access command line inputs %
80# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81if __name__ == '__main__':
82 """
83 API is initialized and run.
84 """
85 # Execute run command
86 main(handle); sys.exit()
PyXMake.API.Frontend
Class instance to define PyXMake's web API instance.
PyXMake.API
Contains all classes and functions to create a web application.
Definition __init__.py:1
PyXMake.Build.Make
Create a make object to define the building environment.
Definition Make.py:1
PyXMake.VTL.api.handle
handle()
Definition api.py:70
PyXMake.VTL.api.main
main(handle, Hostname=str(platform.node()), PortID=8020, **kwargs)
Definition api.py:40