Skip to content

Commit faa4c2b

Browse files
author
nazarfil
committed
fix: use codegen
1 parent 74b07ea commit faa4c2b

5 files changed

Lines changed: 78 additions & 38 deletions

File tree

openhexa/cli/graphql/graphql_client/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@
134134
GraphQLClientHttpError,
135135
GraphQLClientInvalidResponseError,
136136
)
137+
from .get_countries import (
138+
GetCountries,
139+
GetCountriesWorkspace,
140+
GetCountriesWorkspaceCountries,
141+
)
137142
from .get_workspace_pipelines import (
138143
GetWorkspacePipelines,
139144
GetWorkspacePipelinesPipelines,
@@ -377,6 +382,9 @@
377382
"GeneratePipelineWebhookUrlInput",
378383
"GenerateWorkspaceTokenError",
379384
"GenerateWorkspaceTokenInput",
385+
"GetCountries",
386+
"GetCountriesWorkspace",
387+
"GetCountriesWorkspaceCountries",
380388
"GetWorkspacePipelines",
381389
"GetWorkspacePipelinesPipelines",
382390
"GetWorkspacePipelinesPipelinesItems",

openhexa/cli/graphql/graphql_client/client.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from .base_client import BaseClient
77
from .base_model import UNSET, UnsetType
8+
from .get_countries import GetCountries
89
from .get_workspace_pipelines import GetWorkspacePipelines
910

1011

@@ -60,3 +61,25 @@ def get_workspace_pipelines(
6061
)
6162
data = self.get_data(response)
6263
return GetWorkspacePipelines.model_validate(data)
64+
65+
def get_countries(self, workspace_slug: str, **kwargs: Any) -> GetCountries:
66+
query = gql(
67+
"""
68+
query getCountries($workspaceSlug: String!) {
69+
workspace(slug: $workspaceSlug) {
70+
countries {
71+
code
72+
name
73+
flag
74+
alpha3
75+
}
76+
}
77+
}
78+
"""
79+
)
80+
variables: Dict[str, object] = {"workspaceSlug": workspace_slug}
81+
response = self.execute(
82+
query=query, operation_name="getCountries", variables=variables, **kwargs
83+
)
84+
data = self.get_data(response)
85+
return GetCountries.model_validate(data)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Generated by ariadne-codegen
2+
# Source: openhexa/cli/graphql/queries.graphql
3+
4+
from typing import List, Optional
5+
6+
from pydantic import Field
7+
8+
from .base_model import BaseModel
9+
10+
11+
class GetCountries(BaseModel):
12+
workspace: Optional["GetCountriesWorkspace"]
13+
14+
15+
class GetCountriesWorkspace(BaseModel):
16+
countries: List["GetCountriesWorkspaceCountries"]
17+
18+
19+
class GetCountriesWorkspaceCountries(BaseModel):
20+
code: str
21+
name: str
22+
flag: str
23+
alpha_3: str = Field(alias="alpha3")
24+
25+
26+
GetCountries.model_rebuild()
27+
GetCountriesWorkspace.model_rebuild()

openhexa/cli/graphql/queries.graphql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@ query getWorkspacePipelines($workspaceSlug: String!, $name: String, $page: Int =
1313
}
1414
}
1515
}
16+
}
17+
18+
query getCountries($workspaceSlug:String!){
19+
workspace(slug: $workspaceSlug) {
20+
countries {
21+
code
22+
name
23+
flag
24+
alpha3
25+
}
26+
}
1627
}

openhexa/sdk/workspaces/current_workspace.py

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
PostgreSQLConnection,
2121
S3Connection,
2222
)
23+
from ...cli.api import OpenHexaClient
24+
2325

2426
class Country:
2527
"""Represents a country with its code, name, alpha3 code and flag."""
@@ -32,6 +34,8 @@ def __init__(self, code: str, name: str, alpha3: str, flag: str):
3234

3335
def __repr__(self):
3436
return f"Country(code={self.code}, name={self.name}, alpha3={self.alpha3}, flag={self.flag})"
37+
38+
3539
class WorkspaceConfigError(Exception):
3640
"""Raised whenever the system cannot find an environment variable required to configure the current workspace."""
3741

@@ -70,46 +74,13 @@ def slug(self) -> str:
7074
raise WorkspaceConfigError("The workspace slug is not available in this environment.")
7175

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

11485
@property
11586
def database_host(self) -> str:

0 commit comments

Comments
 (0)