Skip to content

Commit f62b6ee

Browse files
committed
Allow closing underlying requests session
Also allows the use of with statement for automatic close.
1 parent e63a4c8 commit f62b6ee

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

yamcs-client/src/yamcs/client/core.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ def from_environment():
214214

215215
return YamcsClient(url, credentials=credentials)
216216

217+
def __enter__(self):
218+
return self
219+
220+
def __exit__(self, exc_type, exc_value, traceback):
221+
self.close()
222+
217223
def get_time(self, instance) -> Optional[datetime.datetime]:
218224
"""
219225
Return the current mission time for the specified instance.
@@ -735,3 +741,7 @@ def create_event_subscription(
735741
subscription.reply(timeout=timeout)
736742

737743
return subscription
744+
745+
def close(self):
746+
"""Close this client session"""
747+
self.ctx.close()

yamcs-client/src/yamcs/core/context.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,7 @@ def request(self, method: str, path: str, **kwargs) -> requests.Response:
118118
raise YamcsError(f"{response.status_code} Client Error: {msg}")
119119
msg = getattr(exception_message, "msg")
120120
raise YamcsError(f"{response.status_code} Server Error: {msg}")
121+
122+
def close(self):
123+
"""Close this context"""
124+
self.session.close()

0 commit comments

Comments
 (0)