From c1de7efc851b3b10e2a50e66268fc8fb0ff648a8 Mon Sep 17 00:00:00 2001 From: Jonas L Date: Tue, 26 Sep 2023 13:20:29 +0200 Subject: [PATCH] fix: prevent api calls when printing bound models (#305) On large objects such as servers, we might generate more than 10 api calls just to print a single server object. This is because the `__repr__` method will recursively generate the `__repr__` for each bound model property and will trigger a `reload` on incomplete models to gather the information from the API. --- hcloud/core/client.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/hcloud/core/client.py b/hcloud/core/client.py index 1d4edfd..d213daf 100644 --- a/hcloud/core/client.py +++ b/hcloud/core/client.py @@ -90,3 +90,9 @@ def reload(self) -> None: bound_model = self._client.get_by_id(self.data_model.id) self.data_model = bound_model.data_model self.complete = True + + def __repr__(self) -> str: + # Override and reset hcloud.core.domain.BaseDomain.__repr__ method for bound + # models, as they will generate a lot of API call trying to print all the fields + # of the model. + return object.__repr__(self)