This document explains the refactoring of the python-takserver-api module, specifically how the code structure was modernized to use classes and adhere to PEP-8 standards.
Previously, the Server class utilized functions imported directly from separate topic files (Mixins). To improve modularity and state management, these files have been reimplemented as distinct classes (Components).
- Class Encapsulation: Each topic file now contains a class (e.g.,
MissionApi,HomeApi) that encapsulates related functionality. - Naming Conventions: Converted names to
snake_casefor functions/variables andCapWordsfor classes, complying with PEP-8. - Asynchronous Design: Maintained
asyncdefinitions for non-blocking operations.
To allow these new component classes to interact with the main Server instance (e.g., to access shared configuration, the network session, or the base URL), we use a pattern where the Server instance is passed to the components upon initialization.
-
In the Component Class: The
__init__method accepts aserverargument. This is stored as an instance variable (self.server), allowing methods within the component to access the main server.class MissionApi: def __init__(self, server): self.server = server # Store reference to the main Server instance async def get_mission(self, name): # Access main server attributes via self.server url = self.server.api_base_url + f"/Marti/api/missions/{name}" # Access the shared connection helper via self.server await self.server.connection.request("get", url)
-
In the Main
ServerClass: When theServerinitializes, it instantiates these component classes, passingself(the currentServerinstance) to them.class Server: def __init__(self, host, cert, key): # ... initialization ... # Initialize components and pass 'self' self.connection = ConnectionHelper(self) self.mission = MissionApi(self) # Now you can access mission methods via: # await server.mission.get_mission("name")
This approach (Composition) keeps the code organized while ensuring all parts of the application can communicate effectively.