|
| 1 | +"""Kili Python SDK client.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +import warnings |
| 5 | +from functools import cached_property |
| 6 | +from typing import TYPE_CHECKING, Dict, Optional, Union |
| 7 | + |
| 8 | +from kili.client import Kili as KiliLegacy |
| 9 | +from kili.core.graphql.graphql_client import GraphQLClientName |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from kili.domain_api import ( |
| 13 | + AssetsNamespace, |
| 14 | + ExportNamespace, |
| 15 | + IssuesNamespace, |
| 16 | + LabelsNamespace, |
| 17 | + OrganizationsNamespace, |
| 18 | + ProjectsNamespace, |
| 19 | + QuestionsNamespace, |
| 20 | + StoragesNamespace, |
| 21 | + TagsNamespace, |
| 22 | + UsersNamespace, |
| 23 | + ) |
| 24 | + |
| 25 | +warnings.filterwarnings("default", module="kili", category=DeprecationWarning) |
| 26 | + |
| 27 | + |
| 28 | +class FilterPoolFullWarning(logging.Filter): |
| 29 | + """Filter out the specific urllib3 warning related to the connection pool.""" |
| 30 | + |
| 31 | + def filter(self, record) -> bool: |
| 32 | + """urllib3.connectionpool:Connection pool is full, discarding connection: ...""" |
| 33 | + return "Connection pool is full, discarding connection" not in record.getMessage() |
| 34 | + |
| 35 | + |
| 36 | +logging.getLogger("urllib3.connectionpool").addFilter(FilterPoolFullWarning()) |
| 37 | + |
| 38 | + |
| 39 | +class Kili: |
| 40 | + """Kili Client (domain mode).""" |
| 41 | + |
| 42 | + legacy_client: KiliLegacy |
| 43 | + |
| 44 | + def __init__( |
| 45 | + self, |
| 46 | + api_key: Optional[str] = None, |
| 47 | + api_endpoint: Optional[str] = None, |
| 48 | + verify: Optional[Union[bool, str]] = None, |
| 49 | + graphql_client_params: Optional[Dict[str, object]] = None, |
| 50 | + ) -> None: |
| 51 | + """Initialize Kili client (domain mode). |
| 52 | +
|
| 53 | + This client provides access to domain-based namespaces. |
| 54 | + For the legacy API with methods, use `from kili.client import Kili` instead. |
| 55 | +
|
| 56 | + Args: |
| 57 | + api_key: User API key generated |
| 58 | + from https://cloud.kili-technology.com/label/my-account/api-key. |
| 59 | + Default to `KILI_API_KEY` environment variable. |
| 60 | + If not passed, requires the `KILI_API_KEY` environment variable to be set. |
| 61 | + api_endpoint: Recipient of the HTTP operation. |
| 62 | + Default to `KILI_API_ENDPOINT` environment variable. |
| 63 | + If not passed, default to Kili SaaS: |
| 64 | + 'https://cloud.kili-technology.com/api/label/v2/graphql' |
| 65 | + verify: similar to `requests`' verify. |
| 66 | + Either a boolean, in which case it controls whether we verify |
| 67 | + the server's TLS certificate, or a string, in which case it must be a path |
| 68 | + to a CA bundle to use. Defaults to ``True``. When set to |
| 69 | + ``False``, requests will accept any TLS certificate presented by |
| 70 | + the server, and will ignore hostname mismatches and/or expired |
| 71 | + certificates, which will make your application vulnerable to |
| 72 | + man-in-the-middle (MitM) attacks. Setting verify to ``False`` |
| 73 | + may be useful during local development or testing. |
| 74 | + graphql_client_params: Parameters to pass to the graphQL client. |
| 75 | +
|
| 76 | + Returns: |
| 77 | + Instance of the Kili client. |
| 78 | +
|
| 79 | + Examples: |
| 80 | + ```python |
| 81 | + from kili.client_domain import Kili |
| 82 | +
|
| 83 | + # Domain API with namespaces |
| 84 | + kili = Kili() |
| 85 | + kili.assets # domain namespace (clean name) |
| 86 | + kili.projects.list() # domain methods |
| 87 | + ``` |
| 88 | + """ |
| 89 | + warnings.warn( |
| 90 | + "Client domain api is still a work in progress. Method names and return type will evolve.", |
| 91 | + stacklevel=1, |
| 92 | + ) |
| 93 | + self.legacy_client = KiliLegacy( |
| 94 | + api_key, |
| 95 | + api_endpoint, |
| 96 | + verify, |
| 97 | + GraphQLClientName.SDK_DOMAIN, |
| 98 | + graphql_client_params, |
| 99 | + ) |
| 100 | + |
| 101 | + # Domain API Namespaces - Lazy loaded properties |
| 102 | + @cached_property |
| 103 | + def assets(self) -> "AssetsNamespace": |
| 104 | + """Get the assets domain namespace. |
| 105 | +
|
| 106 | + Returns: |
| 107 | + AssetsNamespace: Assets domain namespace with lazy loading |
| 108 | +
|
| 109 | + Examples: |
| 110 | + ```python |
| 111 | + kili = Kili() |
| 112 | + # Namespace is instantiated on first access |
| 113 | + assets = kili.assets |
| 114 | + ``` |
| 115 | + """ |
| 116 | + from kili.domain_api import AssetsNamespace # pylint: disable=import-outside-toplevel |
| 117 | + |
| 118 | + return AssetsNamespace(self.legacy_client, self.legacy_client.kili_api_gateway) |
| 119 | + |
| 120 | + @cached_property |
| 121 | + def labels(self) -> "LabelsNamespace": |
| 122 | + """Get the labels domain namespace. |
| 123 | +
|
| 124 | + Returns: |
| 125 | + LabelsNamespace: Labels domain namespace with lazy loading |
| 126 | +
|
| 127 | + Examples: |
| 128 | + ```python |
| 129 | + kili = Kili() |
| 130 | + # Namespace is instantiated on first access |
| 131 | + labels = kili.labels |
| 132 | + ``` |
| 133 | + """ |
| 134 | + from kili.domain_api import LabelsNamespace # pylint: disable=import-outside-toplevel |
| 135 | + |
| 136 | + return LabelsNamespace(self.legacy_client, self.legacy_client.kili_api_gateway) |
| 137 | + |
| 138 | + @cached_property |
| 139 | + def projects(self) -> "ProjectsNamespace": |
| 140 | + """Get the projects domain namespace. |
| 141 | +
|
| 142 | + Returns: |
| 143 | + ProjectsNamespace: Projects domain namespace with lazy loading |
| 144 | +
|
| 145 | + Examples: |
| 146 | + ```python |
| 147 | + kili = Kili() |
| 148 | + # Namespace is instantiated on first access |
| 149 | + projects = kili.projects |
| 150 | + ``` |
| 151 | + """ |
| 152 | + from kili.domain_api import ProjectsNamespace # pylint: disable=import-outside-toplevel |
| 153 | + |
| 154 | + return ProjectsNamespace(self.legacy_client, self.legacy_client.kili_api_gateway) |
| 155 | + |
| 156 | + @cached_property |
| 157 | + def users(self) -> "UsersNamespace": |
| 158 | + """Get the users domain namespace. |
| 159 | +
|
| 160 | + Returns: |
| 161 | + UsersNamespace: Users domain namespace with lazy loading |
| 162 | +
|
| 163 | + Examples: |
| 164 | + ```python |
| 165 | + kili = Kili() |
| 166 | + # Namespace is instantiated on first access |
| 167 | + users = kili.users |
| 168 | + ``` |
| 169 | + """ |
| 170 | + from kili.domain_api import UsersNamespace # pylint: disable=import-outside-toplevel |
| 171 | + |
| 172 | + return UsersNamespace(self.legacy_client, self.legacy_client.kili_api_gateway) |
| 173 | + |
| 174 | + @cached_property |
| 175 | + def organizations(self) -> "OrganizationsNamespace": |
| 176 | + """Get the organizations domain namespace. |
| 177 | +
|
| 178 | + Returns: |
| 179 | + OrganizationsNamespace: Organizations domain namespace with lazy loading |
| 180 | +
|
| 181 | + Examples: |
| 182 | + ```python |
| 183 | + kili = Kili() |
| 184 | + # Namespace is instantiated on first access |
| 185 | + organizations = kili.organizations |
| 186 | + ``` |
| 187 | + """ |
| 188 | + from kili.domain_api import ( # pylint: disable=import-outside-toplevel |
| 189 | + OrganizationsNamespace, |
| 190 | + ) |
| 191 | + |
| 192 | + return OrganizationsNamespace(self.legacy_client, self.legacy_client.kili_api_gateway) |
| 193 | + |
| 194 | + @cached_property |
| 195 | + def issues(self) -> "IssuesNamespace": |
| 196 | + """Get the issues domain namespace. |
| 197 | +
|
| 198 | + Returns: |
| 199 | + IssuesNamespace: Issues domain namespace with lazy loading |
| 200 | +
|
| 201 | + Examples: |
| 202 | + ```python |
| 203 | + kili = Kili() |
| 204 | + # Namespace is instantiated on first access |
| 205 | + issues = kili.issues |
| 206 | + ``` |
| 207 | + """ |
| 208 | + from kili.domain_api import IssuesNamespace # pylint: disable=import-outside-toplevel |
| 209 | + |
| 210 | + return IssuesNamespace(self.legacy_client, self.legacy_client.kili_api_gateway) |
| 211 | + |
| 212 | + @cached_property |
| 213 | + def questions(self) -> "QuestionsNamespace": |
| 214 | + """Get the questions domain namespace. |
| 215 | +
|
| 216 | + Returns: |
| 217 | + QuestionsNamespace: Questions domain namespace with lazy loading |
| 218 | +
|
| 219 | + Examples: |
| 220 | + ```python |
| 221 | + kili = Kili() |
| 222 | + # Namespace is instantiated on first access |
| 223 | + questions = kili.questions |
| 224 | + ``` |
| 225 | + """ |
| 226 | + from kili.domain_api import QuestionsNamespace # pylint: disable=import-outside-toplevel |
| 227 | + |
| 228 | + return QuestionsNamespace(self.legacy_client, self.legacy_client.kili_api_gateway) |
| 229 | + |
| 230 | + @cached_property |
| 231 | + def tags(self) -> "TagsNamespace": |
| 232 | + """Get the tags domain namespace. |
| 233 | +
|
| 234 | + Returns: |
| 235 | + TagsNamespace: Tags domain namespace with lazy loading |
| 236 | +
|
| 237 | + Examples: |
| 238 | + ```python |
| 239 | + kili = Kili() |
| 240 | + # Namespace is instantiated on first access |
| 241 | + tags = kili.tags |
| 242 | + ``` |
| 243 | + """ |
| 244 | + from kili.domain_api import TagsNamespace # pylint: disable=import-outside-toplevel |
| 245 | + |
| 246 | + return TagsNamespace(self.legacy_client, self.legacy_client.kili_api_gateway) |
| 247 | + |
| 248 | + @cached_property |
| 249 | + def storages(self) -> "StoragesNamespace": |
| 250 | + """Get the storages domain namespace. |
| 251 | +
|
| 252 | + Returns: |
| 253 | + StoragesNamespace: Storages domain namespace with lazy loading |
| 254 | +
|
| 255 | + Examples: |
| 256 | + ```python |
| 257 | + kili = Kili() |
| 258 | + # Namespace is instantiated on first access |
| 259 | + storages = kili.storages |
| 260 | + # Access nested namespaces |
| 261 | + integrations = kili.storages.integrations |
| 262 | + connections = kili.storages.connections |
| 263 | + ``` |
| 264 | + """ |
| 265 | + from kili.domain_api import StoragesNamespace # pylint: disable=import-outside-toplevel |
| 266 | + |
| 267 | + return StoragesNamespace(self.legacy_client, self.legacy_client.kili_api_gateway) |
| 268 | + |
| 269 | + @cached_property |
| 270 | + def exports(self) -> "ExportNamespace": |
| 271 | + """Get the exports domain namespace. |
| 272 | +
|
| 273 | + Returns: |
| 274 | + ExportNamespace: Exports domain namespace with lazy loading |
| 275 | +
|
| 276 | + Examples: |
| 277 | + ```python |
| 278 | + kili = Kili() |
| 279 | + # Namespace is instantiated on first access |
| 280 | + exports = kili.exports |
| 281 | + ``` |
| 282 | + """ |
| 283 | + from kili.domain_api import ExportNamespace # pylint: disable=import-outside-toplevel |
| 284 | + |
| 285 | + return ExportNamespace(self.legacy_client, self.legacy_client.kili_api_gateway) |
0 commit comments