Skip to content

Commit 9467d50

Browse files
author
nazarfil
committed
feat(workspace): add method to expose configuration peroperty from workspace
1 parent d1dc1a4 commit 9467d50

6 files changed

Lines changed: 105 additions & 1 deletion

File tree

openhexa/graphql/graphql_client/client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ def workspace(self, slug: str, **kwargs: Any) -> Optional[WorkspaceWorkspace]:
558558
name
559559
description
560560
dockerImage
561+
configuration
561562
countries {
562563
code
563564
flag

openhexa/graphql/graphql_client/workspace.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Generated by ariadne-codegen
22
# Source: openhexa/graphql/queries.graphql
33

4-
from typing import List, Optional
4+
from typing import Any, List, Optional
55

66
from pydantic import Field
77

@@ -17,6 +17,7 @@ class WorkspaceWorkspace(BaseModel):
1717
name: str
1818
description: Optional[str]
1919
docker_image: Optional[str] = Field(alias="dockerImage")
20+
configuration: Optional[Any]
2021
countries: List["WorkspaceWorkspaceCountries"]
2122
permissions: "WorkspaceWorkspacePermissions"
2223

openhexa/graphql/queries.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ query Workspace($slug: String!) {
200200
name
201201
description
202202
dockerImage
203+
configuration
203204
countries {
204205
code
205206
flag

openhexa/graphql/schema.generated.graphql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4090,6 +4090,8 @@ Represents a workspace. A workspace is a shared environment where users can coll
40904090
type Workspace {
40914091
"""File storage of the workspace represented as a bucket"""
40924092
bucket: Bucket!
4093+
"""JSON field containing workspace configuration as key-value pairs"""
4094+
configuration: JSON
40934095
connections: [Connection!]!
40944096
countries: [Country!]!
40954097
createdAt: DateTime!

openhexa/sdk/workspaces/current_workspace.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,60 @@ def countries(self) -> list[WorkspaceWorkspaceCountries]:
6868
except KeyError:
6969
raise WorkspaceConfigError("The workspace countries are not available in this environment.")
7070

71+
@property
72+
def configuration(self) -> dict[str, str | dict] | None:
73+
"""The workspace configuration as a dictionary.
74+
75+
Returns a dictionary containing workspace configuration as key-value pairs,
76+
where keys are strings and values can be either strings or JSON objects.
77+
Returns None if no configuration is available or if not connected to the API.
78+
"""
79+
if not self._connected:
80+
return None
81+
try:
82+
workspace_data = OpenHexaClient().workspace(slug=self.slug)
83+
return workspace_data.configuration
84+
except KeyError:
85+
raise WorkspaceConfigError("The workspace configuration is not available in this environment.")
86+
87+
def get_config(self, key: str, default=None) -> str | dict | None:
88+
"""Get a specific configuration value by key.
89+
90+
Parameters
91+
----------
92+
key : str
93+
The configuration key to retrieve
94+
default : any, optional
95+
Default value to return if key is not found
96+
97+
Returns
98+
-------
99+
str | dict | None
100+
The configuration value, or default if key is not found
101+
"""
102+
config = self.configuration
103+
if config is None:
104+
return default
105+
return config.get(key, default)
106+
107+
def has_config(self, key: str) -> bool:
108+
"""Check if a configuration key exists.
109+
110+
Parameters
111+
----------
112+
key : str
113+
The configuration key to check
114+
115+
Returns
116+
-------
117+
bool
118+
True if the key exists in configuration, False otherwise
119+
"""
120+
config = self.configuration
121+
if config is None:
122+
return False
123+
return key in config
124+
71125
@property
72126
def database_host(self) -> str:
73127
"""The workspace database host."""

tests/test_workspace.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,48 @@ def test_connection_various_case(self, workspace):
543543
assert workspace.dhis2_connection("polio-123").url == url
544544
assert workspace.dhis2_connection("Polio-123").url == url
545545
assert workspace.dhis2_connection("POLIO-123").url == url
546+
547+
def test_workspace_configuration(self, workspace):
548+
mock_config = {
549+
"api_url": "https://api.example.com",
550+
"debug_mode": "true",
551+
"database_config": {"host": "localhost", "port": 5432},
552+
}
553+
554+
mock_workspace_data = mock.Mock()
555+
mock_workspace_data.configuration = mock_config
556+
557+
with mock.patch("openhexa.sdk.workspaces.current_workspace.OpenHexaClient") as mock_client:
558+
mock_client.return_value.workspace.return_value = mock_workspace_data
559+
assert workspace.configuration == mock_config
560+
561+
def test_workspace_get_config(self, workspace):
562+
mock_config = {
563+
"api_url": "https://api.example.com",
564+
"debug_mode": "true",
565+
"database_config": {"host": "localhost", "port": 5432},
566+
}
567+
568+
mock_workspace_data = mock.Mock()
569+
mock_workspace_data.configuration = mock_config
570+
571+
with mock.patch("openhexa.sdk.workspaces.current_workspace.OpenHexaClient") as mock_client:
572+
mock_client.return_value.workspace.return_value = mock_workspace_data
573+
574+
assert workspace.get_config("api_url") == "https://api.example.com"
575+
assert workspace.get_config("database_config") == {"host": "localhost", "port": 5432}
576+
assert workspace.get_config("missing_key", "default_value") == "default_value"
577+
assert workspace.get_config("missing_key") is None
578+
579+
def test_workspace_has_config(self, workspace):
580+
mock_config = {"api_url": "https://api.example.com", "debug_mode": "true"}
581+
582+
mock_workspace_data = mock.Mock()
583+
mock_workspace_data.configuration = mock_config
584+
585+
with mock.patch("openhexa.sdk.workspaces.current_workspace.OpenHexaClient") as mock_client:
586+
mock_client.return_value.workspace.return_value = mock_workspace_data
587+
588+
assert workspace.has_config("api_url") is True
589+
assert workspace.has_config("debug_mode") is True
590+
assert workspace.has_config("missing_key") is False

0 commit comments

Comments
 (0)