|
21 | 21 | S3Connection, |
22 | 22 | ) |
23 | 23 |
|
| 24 | +class Country: |
| 25 | + """Represents a country with its code, name, alpha3 code and flag.""" |
24 | 26 |
|
| 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})" |
25 | 35 | class WorkspaceConfigError(Exception): |
26 | 36 | """Raised whenever the system cannot find an environment variable required to configure the current workspace.""" |
27 | 37 |
|
@@ -59,6 +69,50 @@ def slug(self) -> str: |
59 | 69 | except KeyError: |
60 | 70 | raise WorkspaceConfigError("The workspace slug is not available in this environment.") |
61 | 71 |
|
| 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 | + |
62 | 116 | @property |
63 | 117 | def database_host(self) -> str: |
64 | 118 | """The workspace database host.""" |
|
0 commit comments