@connect
Overview
The @connect
decorator is used to wrap functions that handle connections or authentication tasks.
Syntax
@connect(*args, **kwargs)
Keyword Arguments
Argument | Type | Description |
---|---|---|
|
| A name to associate with the connection. If not provided, it defaults to the function name. |
|
| A unique label to identify the authentication method. If not provided, it will not be set. Examples include, but are not limited to:
|
Implementation Example
This is an example implementation of connecting to the OpenWeather API using an API key:
@connect(noun_phrase="api keys")
def connect(self, api_key: str):
"""
Authenticate to Open Weather API using the specified API key.
You can obtain your own API key by visiting Open Weather's
website at https://openweathermap.org/appid.
Arguments:
api_key: The API key to be used for connecting
Labels:
api_key: API Key
"""
api_key = os.getenv("API_KEY", api_key)
test_url = f"{self._base_url}?appid={api_key}&q=London"
response = requests.get(test_url, timeout=self._timeout)
if response.status_code == 401:
response_data = response.json()
if "Invalid API key" in response_data.get("message", ""):
raise ValueError("Invalid API key")
self._api_key = api_key
Refer to connections for additional context and usage examples.
Updated about 1 month ago
What’s Next