33from __future__ import annotations
44
55import os
6- from typing import Any , Union , Mapping
7- from typing_extensions import Self , override
6+ from typing import Any , Dict , Union , Mapping , cast
7+ from typing_extensions import Self , Literal , override
88
99import httpx
1010
3030 AsyncAPIClient ,
3131)
3232
33- __all__ = ["Timeout" , "Transport" , "ProxiesTypes" , "RequestOptions" , "Ade" , "AsyncAde" , "Client" , "AsyncClient" ]
33+ __all__ = [
34+ "ENVIRONMENTS" ,
35+ "Timeout" ,
36+ "Transport" ,
37+ "ProxiesTypes" ,
38+ "RequestOptions" ,
39+ "Ade" ,
40+ "AsyncAde" ,
41+ "Client" ,
42+ "AsyncClient" ,
43+ ]
44+
45+ ENVIRONMENTS : Dict [str , str ] = {
46+ "production" : "https://api.va.landing.ai" ,
47+ "eu-production" : "https://va.eu-west-1.landing.ai" ,
48+ }
3449
3550
3651class Ade (SyncAPIClient ):
@@ -41,11 +56,14 @@ class Ade(SyncAPIClient):
4156 # client options
4257 apikey : str
4358
59+ _environment : Literal ["production" , "eu-production" ] | NotGiven
60+
4461 def __init__ (
4562 self ,
4663 * ,
4764 apikey : str | None = None ,
48- base_url : str | httpx .URL | None = None ,
65+ environment : Literal ["production" , "eu-production" ] | NotGiven = NOT_GIVEN ,
66+ base_url : str | httpx .URL | None | NotGiven = NOT_GIVEN ,
4967 timeout : Union [float , Timeout , None , NotGiven ] = NOT_GIVEN ,
5068 max_retries : int = DEFAULT_MAX_RETRIES ,
5169 default_headers : Mapping [str , str ] | None = None ,
@@ -76,10 +94,31 @@ def __init__(
7694 )
7795 self .apikey = apikey
7896
79- if base_url is None :
80- base_url = os .environ .get ("ADE_BASE_URL" )
81- if base_url is None :
82- base_url = f"https://api.va.landing.ai"
97+ self ._environment = environment
98+
99+ base_url_env = os .environ .get ("ADE_BASE_URL" )
100+ if is_given (base_url ) and base_url is not None :
101+ # cast required because mypy doesn't understand the type narrowing
102+ base_url = cast ("str | httpx.URL" , base_url ) # pyright: ignore[reportUnnecessaryCast]
103+ elif is_given (environment ):
104+ if base_url_env and base_url is not None :
105+ raise ValueError (
106+ "Ambiguous URL; The `ADE_BASE_URL` env var and the `environment` argument are given. If you want to use the environment, you must pass base_url=None" ,
107+ )
108+
109+ try :
110+ base_url = ENVIRONMENTS [environment ]
111+ except KeyError as exc :
112+ raise ValueError (f"Unknown environment: { environment } " ) from exc
113+ elif base_url_env is not None :
114+ base_url = base_url_env
115+ else :
116+ self ._environment = environment = "production"
117+
118+ try :
119+ base_url = ENVIRONMENTS [environment ]
120+ except KeyError as exc :
121+ raise ValueError (f"Unknown environment: { environment } " ) from exc
83122
84123 super ().__init__ (
85124 version = __version__ ,
@@ -120,6 +159,7 @@ def copy(
120159 self ,
121160 * ,
122161 apikey : str | None = None ,
162+ environment : Literal ["production" , "eu-production" ] | None = None ,
123163 base_url : str | httpx .URL | None = None ,
124164 timeout : float | Timeout | None | NotGiven = NOT_GIVEN ,
125165 http_client : httpx .Client | None = None ,
@@ -155,6 +195,7 @@ def copy(
155195 return self .__class__ (
156196 apikey = apikey or self .apikey ,
157197 base_url = base_url or self .base_url ,
198+ environment = environment or self ._environment ,
158199 timeout = self .timeout if isinstance (timeout , NotGiven ) else timeout ,
159200 http_client = http_client ,
160201 max_retries = max_retries if is_given (max_retries ) else self .max_retries ,
@@ -209,11 +250,14 @@ class AsyncAde(AsyncAPIClient):
209250 # client options
210251 apikey : str
211252
253+ _environment : Literal ["production" , "eu-production" ] | NotGiven
254+
212255 def __init__ (
213256 self ,
214257 * ,
215258 apikey : str | None = None ,
216- base_url : str | httpx .URL | None = None ,
259+ environment : Literal ["production" , "eu-production" ] | NotGiven = NOT_GIVEN ,
260+ base_url : str | httpx .URL | None | NotGiven = NOT_GIVEN ,
217261 timeout : Union [float , Timeout , None , NotGiven ] = NOT_GIVEN ,
218262 max_retries : int = DEFAULT_MAX_RETRIES ,
219263 default_headers : Mapping [str , str ] | None = None ,
@@ -244,10 +288,31 @@ def __init__(
244288 )
245289 self .apikey = apikey
246290
247- if base_url is None :
248- base_url = os .environ .get ("ADE_BASE_URL" )
249- if base_url is None :
250- base_url = f"https://api.va.landing.ai"
291+ self ._environment = environment
292+
293+ base_url_env = os .environ .get ("ADE_BASE_URL" )
294+ if is_given (base_url ) and base_url is not None :
295+ # cast required because mypy doesn't understand the type narrowing
296+ base_url = cast ("str | httpx.URL" , base_url ) # pyright: ignore[reportUnnecessaryCast]
297+ elif is_given (environment ):
298+ if base_url_env and base_url is not None :
299+ raise ValueError (
300+ "Ambiguous URL; The `ADE_BASE_URL` env var and the `environment` argument are given. If you want to use the environment, you must pass base_url=None" ,
301+ )
302+
303+ try :
304+ base_url = ENVIRONMENTS [environment ]
305+ except KeyError as exc :
306+ raise ValueError (f"Unknown environment: { environment } " ) from exc
307+ elif base_url_env is not None :
308+ base_url = base_url_env
309+ else :
310+ self ._environment = environment = "production"
311+
312+ try :
313+ base_url = ENVIRONMENTS [environment ]
314+ except KeyError as exc :
315+ raise ValueError (f"Unknown environment: { environment } " ) from exc
251316
252317 super ().__init__ (
253318 version = __version__ ,
@@ -288,6 +353,7 @@ def copy(
288353 self ,
289354 * ,
290355 apikey : str | None = None ,
356+ environment : Literal ["production" , "eu-production" ] | None = None ,
291357 base_url : str | httpx .URL | None = None ,
292358 timeout : float | Timeout | None | NotGiven = NOT_GIVEN ,
293359 http_client : httpx .AsyncClient | None = None ,
@@ -323,6 +389,7 @@ def copy(
323389 return self .__class__ (
324390 apikey = apikey or self .apikey ,
325391 base_url = base_url or self .base_url ,
392+ environment = environment or self ._environment ,
326393 timeout = self .timeout if isinstance (timeout , NotGiven ) else timeout ,
327394 http_client = http_client ,
328395 max_retries = max_retries if is_given (max_retries ) else self .max_retries ,
0 commit comments