-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add session details method * add reno * support iqp urls * update details mehotd, add status() * update status() to use enum * revert previous change, wait for impl details * update fields returned * update status method * update docstring & reno * update docstrings for both methods * fix docs build * address comments * fix docs build * fix typo * docs build again * fix indent * fix indent again * fix indent 3rd time
- Loading branch information
Showing
6 changed files
with
191 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2021. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
"""Base REST adapter.""" | ||
|
||
from ..session import RetrySession | ||
|
||
|
||
class RestAdapterBase: | ||
"""Base class for REST adapters.""" | ||
|
||
URL_MAP = {} # type: ignore[var-annotated] | ||
"""Mapping between the internal name of an endpoint and the actual URL.""" | ||
|
||
_HEADER_JSON_CONTENT = {"Content-Type": "application/json"} | ||
|
||
def __init__(self, session: RetrySession, prefix_url: str = "") -> None: | ||
"""RestAdapterBase constructor. | ||
Args: | ||
session: Session to be used in the adapter. | ||
prefix_url: String to be prepend to all URLs. | ||
""" | ||
self.session = session | ||
self.prefix_url = prefix_url | ||
|
||
def get_url(self, identifier: str) -> str: | ||
"""Return the resolved URL for the specified identifier. | ||
Args: | ||
identifier: Internal identifier of the endpoint. | ||
Returns: | ||
The resolved URL of the endpoint (relative to the session base URL). | ||
""" | ||
return "{}{}".format(self.prefix_url, self.URL_MAP[identifier]) | ||
|
||
def get_prefixed_url(self, prefix: str, identifier: str) -> str: | ||
"""Return an adjusted URL for the specified identifier. | ||
Args: | ||
prefix: string to be prepended to the URL. | ||
identifier: Internal identifier of the endpoint. | ||
Returns: | ||
The resolved facade URL of the endpoint. | ||
""" | ||
return "{}{}{}".format(prefix, self.prefix_url, self.URL_MAP[identifier]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (C) Copyright IBM 2022. | ||
# | ||
# This code is licensed under the Apache License, Version 2.0. You may | ||
# obtain a copy of this license in the LICENSE.txt file in the root directory | ||
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
# | ||
# Any modifications or derivative works of this code must retain this | ||
# copyright notice, and modified files need to carry a notice indicating | ||
# that they have been altered from the originals. | ||
|
||
"""Runtime Session REST adapter.""" | ||
|
||
from typing import Dict, Any | ||
from .base import RestAdapterBase | ||
from ..session import RetrySession | ||
|
||
|
||
class RuntimeSession(RestAdapterBase): | ||
"""Rest adapter for session related endpoints.""" | ||
|
||
URL_MAP = { | ||
"self": "", | ||
"close": "/close", | ||
} | ||
|
||
def __init__(self, session: RetrySession, session_id: str, url_prefix: str = "") -> None: | ||
"""Job constructor. | ||
Args: | ||
session: RetrySession to be used in the adapter. | ||
session_id: Job ID of the first job in a runtime session. | ||
url_prefix: Prefix to use in the URL. | ||
""" | ||
super().__init__(session, "{}/sessions/{}".format(url_prefix, session_id)) | ||
|
||
def close(self) -> None: | ||
"""Close this session.""" | ||
url = self.get_url("close") | ||
self.session.delete(url) | ||
|
||
def details(self) -> Dict[str, Any]: | ||
"""Return the details of this session.""" | ||
try: | ||
return self.session.get(self.get_url("self")).json() | ||
# return None if API is not supported | ||
except: # pylint: disable=bare-except | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
releasenotes/notes/expose-session-details-c4a44316d30dad33.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
features: | ||
- | | ||
Added a new method, :meth:`~qiskit_ibm_runtime.Session.details` that returns information | ||
about a session, including: maximum session time, active time remaining, the current state, | ||
and whether or not the session is accepting jobs. | ||
Also added :meth:`~qiskit_ibm_runtime.Session.status`, which returns the current status of | ||
the session. | ||