Base API Client
Generic HTTP REST API client.
Source code in reportconnectors/api_client/base_api_client/base_api_client.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | |
KeyNames
Can be extended by clients' implementations if needed
Source code in reportconnectors/api_client/base_api_client/base_api_client.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
__init__(url, **kwargs)
Client initialization
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
API Url |
required |
Other Parameters:
| Name | Type | Description |
|---|---|---|
proxies |
Dict
|
Dictionary with proxy configuration. Following keys are supported:
|
store_request |
bool
|
If True, all obtained request-response pairs will be stored in memory.
They might be dumped to a JSON file by calling |
cache_file_path |
Union[Path, str]
|
Path to the SQLite database file used as a permanent storage. If provided, then all request-response pairs will be cached in the SQLite DB. It is a useful feature for development purposes, but it's not recommended for production use since currently the cache is never invalidated. |
auto_refresh |
bool
|
If True and |
api_version |
str
|
Version of the API to use. It is optional parameter. If provided then the version is
attached to each request as a |
cert_path |
Union[Path, str, bool]
|
Path to the certificate file or |
unescape_html |
bool
|
If True, the HTML content will be unescaped before decoding it to JSON. |
Source code in reportconnectors/api_client/base_api_client/base_api_client.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
authenticate(*args, **kwargs)
abstractmethod
Source code in reportconnectors/api_client/base_api_client/base_api_client.py
114 115 116 | |
logout()
Logs out the client by resetting authentication data.
Source code in reportconnectors/api_client/base_api_client/base_api_client.py
178 179 180 181 182 | |
dump_stored_requests(**kwargs)
Dumps all already stored request-response pairs into a JSON object represented as a string.
Other Parameters:
| Name | Type | Description |
|---|---|---|
kwargs |
dict
|
JSON dumps method arguments. |
Returns:
| Type | Description |
|---|---|
str
|
JSON object that holds a list or request-response objects as a string. |
Source code in reportconnectors/api_client/base_api_client/base_api_client.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
_validate_url(url)
Validates provided url. It checks if the url has the correct format and scheme.
Allowed schemes should be listed in allowed_schemes attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL to be validated |
required |
Returns:
| Type | Description |
|---|---|
str
|
Valid Url |
Raises:
| Type | Description |
|---|---|
Value Error
|
Empty URL, wrong scheme, or invalid URL. |
Source code in reportconnectors/api_client/base_api_client/base_api_client.py
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |
_initialize_session(retries=3, backoff_factor=1)
Helper function that returns requests.Session object that can be used to perform all the requests by the client.
When communicating with single HTTP server, like we do in case of this client, using session object over the typical requests.get() etc. methods improves performance. Disadvantage of using session is that it comes with no retry policy by default. It has to be explicitly added by mounting adapter with desired retry strategy.
If the client has specified minimum TLS version, then TLSAdapter is used to mount the session. Otherwise, the default HTTPAdapter is used.
Session object details: https://docs.python-requests.org/en/latest/user/advanced/#session-objects Retry configuration details: https://findwork.dev/blog/advanced-usage-python-requests-timeouts-retries-hooks/ Transport Adapters details: https://requests.readthedocs.io/en/latest/user/advanced/#transport-adapters
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
retries
|
int
|
Number of retries |
3
|
backoff_factor
|
int
|
Backoff factor |
1
|
Returns:
| Type | Description |
|---|---|
Session
|
Configured Sessions object. |
Source code in reportconnectors/api_client/base_api_client/base_api_client.py
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | |
_make_access_token_refresh(force=False)
This method should be implemented by each API Client that has the automatic access token refresh logic.
By default, no logic is provided, therefore this method returns False.
Returns:
| Type | Description |
|---|---|
bool
|
In the BaseAPIClient automatic access token refresh can't be implemented so this always returns False. |
Source code in reportconnectors/api_client/base_api_client/base_api_client.py
145 146 147 148 149 150 151 152 153 154 | |
_make_request(method, url=None, endpoint=None, params=None, data=None, json_data=None, **kwargs)
Prepares and executes an HTTP request call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
MethodType
|
HTTP method to use. |
required |
endpoint
|
Optional[str]
|
Endpoint to call. Will be joined with base url to make the final url to call. |
None
|
url
|
Optional[str]
|
Full URL to call. If provided, then the |
None
|
params
|
Optional[Dict[str, Any]]
|
Query parameters that will be added to url. Default: nothing. |
None
|
data
|
Optional[Any]
|
Data to be added as request body. It is added in the format 'as given'.
In most of the cases with real APIs, one would prefer to use |
None
|
json_data
|
Optional[JSONType]
|
JSON data to be added to the request body. It should be provided as an input that can be encoded as a JSON object. Default: nothing. |
None
|
Other Parameters:
| Name | Type | Description |
|---|---|---|
extra_headers |
Dict[str, str]
|
Extra headers to be added to the request. By default, 'User-Agent', 'Content-Type', and 'Authorization' headers are added. But sometimes some extra headers needs to be added as well. |
feature_code |
str
|
Custom Feature Code to be added to headers.
If not provided, the client will try to find an appropriate feature code
based on internal |
content_type |
str
|
Custom Content-Type header. If not provided, then the 'application/json' is used. |
api_version |
str
|
API version to be added to the URL. If not provided, nothing is added. |
timeout |
float
|
Custom Timeout for this request.
If not provided, then the |
auth |
Any
|
Custom authentication method to be used for this request. If provided, then the client's authorization method is NOT added to the request. |
add_authorization |
bool
|
If True, then the client's authorization method is added to the request.
This is ignored if |
Returns:
| Type | Description |
|---|---|
Optional[Response]
|
Optional Response object |
Source code in reportconnectors/api_client/base_api_client/base_api_client.py
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 | |
_prepare_headers(feature_code=None, content_type=None, api_version=None, extra_headers=None)
Prepares headers for a request. Two are added by default:
- 'User-Agent' - identifies the originator of the request. Client name, version and OS is used to build it.
- 'Content-Type' - identifies the MIME type of the request body
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_code
|
Optional[str]
|
Feature code to be added to headers. Will be added as 'FeatureCode' header. Default: |
None
|
content_type
|
Optional[str]
|
Custom Content type headers. Default: |
None
|
api_version
|
Optional[str]
|
API version to be added to the headers. Default: |
None
|
extra_headers
|
Optional[Dict]
|
Dictionary with extra headers to be added. |
None
|
Returns:
| Type | Description |
|---|---|
Dict
|
Dictionary with headers. |
Source code in reportconnectors/api_client/base_api_client/base_api_client.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |
_get_feature_code(requested_endpoint)
Returns a feature code to be used for a given endpoint value. It looks for the feature code value in the
_feature_code attribute of a client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
requested_endpoint
|
str
|
Requested endpoint |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Feature Code value if endpoint found in the client data. Otherwise, |
Source code in reportconnectors/api_client/base_api_client/base_api_client.py
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
_add_authorization_to_request(request)
Adds authorization the provided request object.
The default action is to add Bearer Authorization Token to the headers.
However, if the client need some different authorization method this method should be overwritten.
Example
For clients that communicate with Azure Function based APIs it should rather add code to URL params.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
Request object to be updated. |
required |
Returns:
| Type | Description |
|---|---|
Request
|
Request object with authorization rules added. |
Source code in reportconnectors/api_client/base_api_client/base_api_client.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |
_process_request(request, timeout)
Produces a response based on the provided request. If DB caching is enabled and conditions are met then the response is read from cache. Otherwise, the request is sent and the response is received from web. Finally, if the memory based response storage is enabled, response object is stored there.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
Request object. |
required |
timeout
|
float
|
Timeout in seconds. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Response]
|
Response object if request was successful. Otherwise, |
Source code in reportconnectors/api_client/base_api_client/base_api_client.py
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 | |
_send_request(request, timeout=None, no_of_retries=None)
Sends the provided request and return the response.
It handles timeouts with linear backoff (each try is one times longer). If it fails for no_of_retries times,
then None is returned.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
Request object |
required |
timeout
|
Optional[float]
|
Custom Timeout for this request. If not provided, then |
None
|
no_of_retries
|
Optional[int]
|
Custom number of retries. If not provided, then |
None
|
Returns:
| Type | Description |
|---|---|
Optional[Response]
|
Optional response object. |
Source code in reportconnectors/api_client/base_api_client/base_api_client.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
_decode_response(response, default)
Decodes the response object into JSON object. That means, it is assumed that the content of the response is encoded in JSON format.
default argument is mandatory, and it is returned when there is any problem with the response
(error status code, error while decoding).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Optional[Response]
|
Response object |
required |
default
|
T
|
Default value that should be returned when response is not valid
or the response's status code indicates error. If |
required |
Returns:
| Type | Description |
|---|---|
T
|
JSON decoded response if the response object was valid and of correct type.
Otherwise, |
Source code in reportconnectors/api_client/base_api_client/base_api_client.py
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 | |
_get_status_code(response)
staticmethod
Gets the status code from the response object
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
Optional[Response]
|
Response object |
required |
Returns:
| Type | Description |
|---|---|
Optional[HTTPStatus]
|
HTTPStatus enum with response's status code. If not possible to get it |
Source code in reportconnectors/api_client/base_api_client/base_api_client.py
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | |