Skip to content

Commit d62a361

Browse files
committed
feat: add a "country" property to the current_workspace
1 parent 9083d1f commit d62a361

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

openhexa/sdk/workspaces/current_workspace.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@
2121
S3Connection,
2222
)
2323

24+
class Country:
25+
"""Represents a country with its code, name, alpha3 code and flag."""
2426

27+
def __init__(self, code: str, name: str, alpha3: str, flag: str):
28+
self.code = code
29+
self.name = name
30+
self.alpha3 = alpha3
31+
self.flag = flag
32+
33+
def __repr__(self):
34+
return f"Country(code={self.code}, name={self.name}, alpha3={self.alpha3}, flag={self.flag})"
2535
class WorkspaceConfigError(Exception):
2636
"""Raised whenever the system cannot find an environment variable required to configure the current workspace."""
2737

@@ -59,6 +69,50 @@ def slug(self) -> str:
5969
except KeyError:
6070
raise WorkspaceConfigError("The workspace slug is not available in this environment.")
6171

72+
@property
73+
def country(self) -> Country:
74+
"""The country of the workspace.
75+
76+
"""
77+
try:
78+
if self._connected:
79+
response = graphql(
80+
"""
81+
query getWorkspaceCountry($slug: String!) {
82+
workspace(slug: $slug) {
83+
countries {
84+
code
85+
name
86+
alpha3
87+
flag
88+
}
89+
}
90+
}
91+
""",
92+
{"slug": self.slug},
93+
)
94+
countries = response["workspace"]["countries"]
95+
if not countries:
96+
raise WorkspaceConfigError("The workspace does not have a country configured.")
97+
if len(response["workspace"]["countries"]) > 1:
98+
warn(
99+
"The workspace has multiple countries configured. The first one will be used.",
100+
UserWarning,
101+
stacklevel=2,
102+
)
103+
# return the first country
104+
first_country = response["workspace"]["countries"][0]
105+
return Country(
106+
code=first_country["code"],
107+
name=first_country["name"],
108+
alpha3=first_country["alpha3"],
109+
flag=first_country["flag"],
110+
)
111+
else:
112+
return os.environ["WORKSPACE_COUNTRY"]
113+
except KeyError:
114+
raise WorkspaceConfigError("The workspace country is not available in this environment.")
115+
62116
@property
63117
def database_host(self) -> str:
64118
"""The workspace database host."""

0 commit comments

Comments
 (0)