Base ABC
wingpy.base
¶
RestApiBaseClass
¶
RestApiBaseClass(
*,
base_url: str,
verify: SSLContext | bool = True,
backoff_initial: int = 1,
backoff_multiplier: float = 2.0,
retries: int = 3,
auth_lifetime: int = 0,
auth_refresh_percentage: float = 1,
rate_limit_period: int = 0,
rate_limit_max_requests: int = 0,
headers: dict | None = None,
timeout: int = 10
)
Bases: ABC
An abstract base class for REST API clients.
This class provides a common interface and functionality for interacting with REST APIs. It handles requests, headers, throttling, path parameters, logging, retries, errors and session lifetime. It also defines the abstract methods that must be implemented by clients.
Source code in src/wingpy/base.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
|
MAX_CONNECTIONS
class-attribute
¶
The maximum number of concurrent connections opened to the API server.
If the number is documented in official documentation, it should be used to limit the number of connections. In other cases we may need to limit the number of connections to avoid overwhelming the server or the client machine.
RETRY_RESPONSES
class-attribute
¶
HTTP status codes and response text that should trigger a retry.
Some APIs have specific responses that require a retry, even if the status code is not 429.
auth_lifetime
instance-attribute
¶
The lifetime in seconds of authentication token.
auth_refresh_percentage
instance-attribute
¶
The percentage of the authentication token lifetime at which the token should be refreshed.
This is used to avoid token expiration during long-running requests. The value should be between 0 and 1. For example, if the token lifetime is 3600 seconds (1 hour) and the refresh percentage is 0.8, the token will be refreshed after 2880 seconds (48 minutes).
auth_timestamp
instance-attribute
¶
A timestamp indicating when the authentication token was last refreshed.
In combination with auth_lifetime and auth_refresh_percentage it is used to determine when the token should be refreshed again.
base_url
instance-attribute
¶
A string containing the base URL of the API server.
The base URL path must include:
- Scheme (http / https)
- Hostname / IP address
- TCP port
- Base path, if any (MUST NOT end with a
/
)
Examples:
- https://api.example.com/api/v1
- http://api.example.com:8080
client
instance-attribute
¶
An httpx Client instance used to send requests to the API server.
This client is created when the first request is made and is reused for all subsequent requests. The opened TCP connection is reused for multiple requests to the same server.
headers
instance-attribute
¶
A dictionary of HTTP headers to be sent with each request.
These headers will be merged with any headers
dict passed to an individual request.
path_params
instance-attribute
¶
A dictionary of path parameters to be used in the API path of each request.
These parameters will be merged with any path_params
dict passed to the request.
request_index
instance-attribute
¶
An index to keep track of the number of requests made.
request_log
instance-attribute
¶
A list of requests made to the API server.
Each entry contains the request URL, status code, and timestamp.
retries
instance-attribute
¶
The number of times a request will be retried in case of failures.
The first attempt is not counted as a retry.
tasks
instance-attribute
¶
Manages concurrent requests to the API server.
The number of concurrent requests is limited by the MAX_CONNECTIONS property:
- 1 connection is reserved for the main thread used for authentication and synchronous requests.
- The remaining connections are used for concurrent requests.
See Also
wingpy.scheduling.TaskRunner
Schedule and run asynchronous tasks in parallel.
throttler
instance-attribute
¶
throttler: RequestThrottler = RequestThrottler(
backoff_initial=backoff_initial,
backoff_multiplier=backoff_multiplier,
rate_limit_period=rate_limit_period,
rate_limit_max_requests=rate_limit_max_requests,
)
" Manages request throttling and rate limiting to the API server.
timeout
instance-attribute
¶
The timeout in seconds for each request to the API server.
verify
instance-attribute
¶
Controls the verification of the API server SSL certificate.
It can simply be enabled or disabled using boolean values, or a custom SSLContext can be passed to the constructor to use a custom certificate authority.
Examples:
True
: Verify the server's SSL certificate using the system's CA certificates.False
: Disable SSL certificate verification.ssl.create_default_context(cafile="my-custom-ca.pem")
: Use a custom CA certificate for verification.
__enter__
¶
When a context manager is used, this method is called to enter the runtime context.
This method is called when the with
statement is used with this class.
Returns:
Type | Description |
---|---|
self
|
The instance of the class itself. |
Source code in src/wingpy/base.py
__exit__
¶
This method is called when exiting the runtime context. It closes the client and handles any exceptions that occurred during the context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
exc_type
|
The type of the exception that occurred, if any. |
required | |
exc_value
|
The value of the exception that occurred, if any. |
required | |
traceback
|
The traceback object of the exception that occurred, if any. |
required |
Returns:
Type | Description |
---|---|
bool
|
This ensures that any exceptions that occurred during the context are propagated. |
Source code in src/wingpy/base.py
authenticate
¶
Executes the API-specific authentication process and records timestamps for session tracking.
Notes
Authentication will automatically be carried out just-in-time.
Only call this method directly if you need to authenticate proactively, outside of normal request flow.
Source code in src/wingpy/base.py
build_url
¶
Constructs the full URL for a request.
Combines the base URL with the provided path and substituting any path parameters.
Path parameters are variables embedded into the URL path using {} braces.
Example: /organizations/{organizationId}/firmware/upgrades
Example: /api/fmc_config/v1/domain/{domainUUID}/policy/accesspolicies/{objectId}
Reusable path parameters can be added to the class instance using the path_params
attribute.
Single-use path parameters can be passed as a dictionary to the path_params
argument.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
The URL path, which may include placeholders for path parameters (e.g., "/organizations/{organizationId}/firmware/upgrades"). |
required |
path_params
|
dict
|
A dictionary of path parameters to be used in the API path.
Is merged with |
required |
Returns:
Type | Description |
---|---|
str
|
The fully constructed URL with all path parameters substituted. |
Raises:
Type | Description |
---|---|
InvalidEndpointError
|
If the resulting URL contains unsubstituted path parameters. |
Source code in src/wingpy/base.py
close
¶
Close the httpx client and release any resources. This method should be called when the client is no longer needed. It is automatically called when exiting the context manager.
Source code in src/wingpy/base.py
delete
abstractmethod
¶
Abstract method to send a DELETE request to the API server.
Applies any API-specific pre- or post-processing.
get
abstractmethod
¶
Abstract method to send a GET request to the API server.
Applies any API-specific pre- or post-processing.
is_authenticated
abstractmethod
¶
Abstract method to check if the client is authenticated with the API server.
Returns:
Type | Description |
---|---|
bool
|
|
Source code in src/wingpy/base.py
is_retry_response
¶
Source code in src/wingpy/base.py
patch
abstractmethod
¶
Abstract method to send a PATCH request to the API server.
Applies any API-specific pre- or post-processing.
post
abstractmethod
¶
Abstract method to send a POST request to the API server.
Applies any API-specific pre- or post-processing.
put
abstractmethod
¶
Abstract method to send a PUT request to the API server.
Applies any API-specific pre- or post-processing.
request
¶
request(
method: str,
path: str,
data: dict | list | _Element | str | None,
params: dict,
path_params: dict,
headers: dict,
timeout: int,
is_auth_endpoint: bool,
auth: Auth | None,
) -> httpx.Response
Send any type of HTTP request and receive response.
Handles any preprocessing of authentication, parameters, payload, and URL
Parameters:
Name | Type | Description | Default |
---|---|---|---|
method
|
str
|
The HTTP method to use for the request ( |
required |
path
|
str
|
URL endpoint path. Is combined with |
required |
params
|
dict
|
Query parameters to include in the request. |
required |
path_params
|
dict
|
A dictionary of path parameters to be used in the API path.
Is merged with |
required |
headers
|
dict
|
A dictionary of HTTP headers to be sent with the request.
Is merged with |
required |
timeout
|
int
|
Number of seconds to wait for HTTP responses before raising httpx.TimeoutException exception. |
required |
is_auth_endpoint
|
bool
|
A boolean indicating if the request is a dedicated authentication request. If |
required |
auth
|
Auth | None
|
The authentication object to be used for the request. |
required |
Returns:
Type | Description |
---|---|
Response
|
The response object returned by the API server. |