| 55 | |
| 56 | |
| 57 | class API(Server): |
| 58 | def __init__( |
| 59 | self, |
| 60 | backend: SystemBackend, |
| 61 | reporter: "Reporter", |
| 62 | config: "Config", |
| 63 | addr: str = "0.0.0.0", |
| 64 | port: int = 0, |
| 65 | ) -> None: |
| 66 | super().__init__() |
| 67 | self.log = get_logger(__name__) |
| 68 | self.backend = backend |
| 69 | self.reporter = reporter |
| 70 | self.config = config |
| 71 | self.socket_port = ( |
| 72 | port if port else self.config.get("api", {}).get("port", 9456) |
| 73 | ) |
| 74 | self.socket_host = addr |
| 75 | self.subscribe() |
| 76 | |
| 77 | @cherrypy.expose |
| 78 | @cherrypy.tools.allow(methods=["GET"]) |
| 79 | @cherrypy.tools.json_out() |
| 80 | def memory(self) -> Dict[str, Any]: |
| 81 | return {"memory": self.backend.get_memory()} |
| 82 | |
| 83 | @cherrypy.expose |
| 84 | @cherrypy.tools.allow(methods=["GET"]) |
| 85 | @cherrypy.tools.json_out() |
| 86 | def network(self) -> Dict[str, Any]: |
| 87 | return {"network": self.backend.get_network()} |
| 88 | |
| 89 | @cherrypy.expose |
| 90 | @cherrypy.tools.allow(methods=["GET"]) |
| 91 | @cherrypy.tools.json_out() |
| 92 | def processors(self) -> Dict[str, Any]: |
| 93 | return {"processors": self.backend.get_processors()} |
| 94 | |
| 95 | @cherrypy.expose |
| 96 | @cherrypy.tools.allow(methods=["GET"]) |
| 97 | @cherrypy.tools.json_out() |
| 98 | def storage(self) -> Dict[str, Any]: |
| 99 | return {"storage": self.backend.get_storage()} |
| 100 | |
| 101 | @cherrypy.expose |
| 102 | @cherrypy.tools.allow(methods=["GET"]) |
| 103 | @cherrypy.tools.json_out() |
| 104 | def power(self) -> Dict[str, Any]: |
| 105 | return {"power": self.backend.get_power()} |
| 106 | |
| 107 | @cherrypy.expose |
| 108 | @cherrypy.tools.allow(methods=["GET"]) |
| 109 | @cherrypy.tools.json_out() |
| 110 | def fans(self) -> Dict[str, Any]: |
| 111 | return {"fans": self.backend.get_fans()} |
| 112 | |
| 113 | @cherrypy.expose |
| 114 | @cherrypy.tools.allow(methods=["GET"]) |