Skip to content

Commit

Permalink
Add .from_config() factory to ResourceBase and subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
randomir committed Sep 6, 2023
1 parent e7e5464 commit 259c616
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
20 changes: 8 additions & 12 deletions dwave/cloud/api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ class ResourceBase:
# endpoint path prefix (base path) specific to all methods on the resource
resource_path: str = None

def __init__(self, **config):
self.client = self.client_class(**config)
def __init__(self, client: Optional[DWaveAPIClient] = None, **config):
if client is not None:
self.client = client
else:
self.client = self.client_class(**config)

@property
def session(self):
Expand All @@ -86,16 +89,9 @@ def __exit__(self, exc_type, exc_value, traceback):
self.close()

@classmethod
def from_client_config(cls, client: Union[DWaveAPIClient, 'dwave.cloud.client.base.Client']):
"""Create Resource instance configured from a
:class:`~dwave.cloud.client.base.Client' instance.
"""
# TODO: also accept configuration dict/dataclass when config/client refactored
if isinstance(client, DWaveAPIClient):
return cls(**client.config)
else: # assume isinstance(client, dwave.cloud.Client), without importing
sapiclient = SolverAPIClient.from_client_config(client)
return cls(**sapiclient.config)
def from_config(cls, config=None, **kwargs):
client = cls.client_class.from_config(config, **kwargs)
return cls(client=client)


class Regions(ResourceBase):
Expand Down
10 changes: 10 additions & 0 deletions releasenotes/notes/refactor-config-696d28038c6b255f.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ deprecations:
Individual config options exposed as ``dwave.cloud.Client`` attributes are
deprecated since ``dwave-cloud-client==0.11.0`` and will be removed in
``dwave-cloud-client==0.12.0``. Use ``Client.config`` data model instead.
upgrade:
- |
Behavior of ``from_client_config()`` on all ``dwave.cloud.api.resources.ResourceBase``
resource classes changed. Previously a (cloud-)client instance was accepted and now
``dwave.cloud.config.models.ClientConfig`` is required.
To upgrade, replace ``{ResourceBase}.from_client_config(cloud_client)`` with
``{ResourceBase}.from_client_config(client.config)``,
and ``{ResourceBase}.from_client_config(dwave_api_client)`` with
``{ResourceBase}(dwave_api_client)``.

0 comments on commit 259c616

Please sign in to comment.