Buffy Python Client API¶
buffy.buffypyclient.BuffyPyClient
¶
Python client to abstract communication with the Buffy-server REST API
Source code in buffy/buffypyclient/buffypyclient.py
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 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 |
|
__init__(url=None, host=None, port=8008, base_path=None, group_name=None, api_version='v1', ssl=False, http_auth=None, http_headers={}, local_download_fallback=True)
¶
A client to interact with a buffy server. It abstracts the Buffy http REST-API.
You can provide a complete url via the url
parameter (e.g. https://mydomain.org:3444/buffy
, http://mybuffy.org)
OR single components of the url to
ssl,
host,
port,
base_path`
You can not provide both.
To init your client via environment variables see Buffy.BuffyPyClient.from_env()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
A full URL to the Buffy Server (e.g. |
None
|
host |
str
|
Hostname/Domain of the the Buffy server (e.g. |
None
|
port |
int
|
Port of the Buffy Server. Not compatible with |
8008
|
base_path |
str
|
If your Buffy server is reachable under a subpath like |
None
|
group_name |
str
|
If you want to sandbox your request and not to interact with other buffy calls to the same url, define a group. Also handy for managing your requests. Defaults to None. |
None
|
api_version |
Literal['v1']
|
Reservers for future use. Always "v1". Defaults to "v1". |
'v1'
|
ssl |
bool
|
If your Buffy server is ssl encrypted. Not compatible with |
False
|
http_auth |
HTTPBasicAuth
|
If your Buffy server is protected by a http basic auth method. Defaults to None. |
None
|
http_headers |
Dict
|
Any extra http headers you want to send to your server connection. If your Buffy server is behind a complex reverse proxy setup for example. Defaults to {}. |
{}
|
local_download_fallback |
bool
|
If the server is not reachable, should the client just download a request localy or throw an error. Defaults to True. |
True
|
Source code in buffy/buffypyclient/buffypyclient.py
from_env(fallback_val_group_name=None, fallback_val_url='http://localhost:8008', fallback_val_http_auth_name=None, fallback_val_http_auth_password=None, fallback_value_local_download_fallback=True)
classmethod
¶
If you want to init your Buffy client by predetermined environment variables. e.g. in a container environment.
Following env variables are available
- BUFFY_SERVER_URL
- BUFFY_GROUP_NAME
- BUFFY_SERVER_HTTP_AUTH_NAME
- BUFFY_SERVER_HTTP_AUTH_PASSWORD
- BUFFY_LOCAL_DOWNLOAD_FALLBACK
You can also define default/fallback values, as function arguments, if a env variable is empty.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fallback_val_group_name |
str
|
description. Defaults to None. |
None
|
fallback_val_url |
_type_
|
description. Defaults to "http://localhost:8008". |
'http://localhost:8008'
|
fallback_val_http_auth_name |
str
|
description. Defaults to None. |
None
|
fallback_val_http_auth_password |
str
|
description. Defaults to None. |
None
|
fallback_value_local_download_fallback |
bool
|
description. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
BuffyPyClient |
BuffyPyClient
|
A initialized Buffy python client |
Source code in buffy/buffypyclient/buffypyclient.py
create_request(url, http_method='get', http_query_params={}, http_request_body={}, http_header_fields={}, info_description=None, info_link=None, hold_request_order=False, cache_configuration=None)
¶
Define a HTTP request to a remote resource.
example usage
import json
from buffy.buffypyclient import BuffyPyClient
c = BuffyPyClient()
req = c.create_request("https://wikipedia.org")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
The full URL to the remote resource. Example: "https://mydomain.org/myfile.txt" Optional: Query params can be supplied via a Dict to |
required |
http_method |
Literal['get', 'post', 'put']
|
HTTP Request method for the to be created request |
'get'
|
http_query_params |
Dict
|
{}
|
|
http_request_body |
Optional[Dict]
|
{}
|
|
http_header_fields |
Dict
|
{}
|
|
info_description |
str
|
This is a documentation string for your request. Buffy-server will save your request. For later traceability it can help to give your request some metadata. |
None
|
info_link |
str
|
Same as |
None
|
hold_request_order |
bool
|
The Buffy client will send the request to the Buffy-server instantly. If for any reasons you need to delay the request, set |
False
|
cache_configuration |
RequestCacheConfiguration
|
Buffy-server can be configured on when to (re-)download your request. An instance of |
None
|
Returns:
Type | Description |
---|---|
Request
|
BuffyPyClient.Request: An object to handle your request. this is needed to download the content of your request |
Source code in buffy/buffypyclient/buffypyclient.py
buffy.buffypyclient.buffypyclient.BuffyPyClient.Request
¶
Return object of Buffy.BuffyPyClient.create_request()
Parameters:
Name | Type | Description | Default |
---|---|---|---|
force_version |
str
|
The size of byte chunks you want to iterate. Defaults to 64 kibibyte. |
required |
Source code in buffy/buffypyclient/buffypyclient.py
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 629 630 631 632 633 634 635 636 637 638 639 640 641 642 |
|
next_version
property
¶
Query the server if there is a newer version available
prefere_tag: str = None
instance-attribute
¶
If a response version with this tag is available it will be preferred to be served to the client.
previous_version
property
¶
Query the server if there is a newer version available
download_response_content(chunk_size=65536)
¶
Stream/Iterate through the response content delivered by the Buffy-server.
example use
from buffy.buffypyclient import BuffyPyClient
c = BuffyPyClient()
req = c.create_request(
url="https://meowfacts.herokuapp.com/"
)
cat_fact = ""
for chunk in req.download_response_content():
cat_fact += chunk.decode("utf-8")
print(json.loads(cat_fact))
Parameters:
Name | Type | Description | Default |
---|---|---|---|
chunk_size |
int
|
The size of byte chunks you want to iterate. Defaults to 64 kibibyte. |
65536
|
Iterator[bytes] |
The reponse content in byte chunks |
required |
Source code in buffy/buffypyclient/buffypyclient.py
download_response_content_to(target=None, dir=None)
¶
Download the response content delivered by the Buffy-server.
example use
from buffy.buffypyclient import BuffyPyClient
c = BuffyPyClient()
req = c.create_request(url="https://www.foaas.com/awesome/:tim")
content_path = req.download_response_content_to(dir="/tmp")
print(content_path)
# > `/tmp/download.html`
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
Union[str, Path, FileIO]
|
Download target as a file like object or a file path. |
None
|
dir |
Union[str, Path]
|
Alternative to |
None
|
Returns:
Name | Type | Description |
---|---|---|
Path |
Path
|
The final path of the downloaded content. |
Source code in buffy/buffypyclient/buffypyclient.py
get_filename(fallback_filename='download')
¶
Try to evaluate the filename of the response content.
This is based on HTTP headers like Content-Disposition
and Content-Type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fallback_filename |
str
|
A filename to fallback if |
'download'
|
Returns:
Type | Description |
---|---|
Union[str, None]
|
The filename as string or |
Source code in buffy/buffypyclient/buffypyclient.py
list_cached_response_versions(status='ready', tag=None, skip=0, limit=10)
¶
Get a list of all cached responses of the request
from buffy.buffypyclient import BuffyPyClient
c = BuffyPyClient()
req = c.create_request(url="https://www.foaas.com/awesome/:tim")
cached_responses = req.list_cached_versions()
for resp in cached_responses:
print(resp.id)
Returns:
Type | Description |
---|---|
List[ApiResponse]
|
List[ApiResponse]: description |
Source code in buffy/buffypyclient/buffypyclient.py
order(prefer_cached=True, prefer_pinned=None, prefer_version=None, prefer_tag=None, force_pinned=None, force_version=None, force_tag=None)
¶
Send the request to the Buffy-server.
When calling Buffy.BuffyPyClient.create_request()
order
will be called automaticly.
If you want determine the moment when the Buffy-server is reached manually, call create_request()' with
hold_request_order=True` and use this function to call the Buffy-server whenever you are ready
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefer_cached |
bool
|
description. Defaults to True. |
True
|
prefer_pinned |
bool
|
description. Defaults to None. |
None
|
prefer_version |
str
|
description. Defaults to None. |
None
|
prefer_tag |
str
|
description. Defaults to None. |
None
|
force_pinned |
bool
|
description. Defaults to None. |
None
|
force_version |
str
|
description. Defaults to None. |
None
|
force_tag |
str
|
description. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
SystemError
|
description |
e
|
description |
Source code in buffy/buffypyclient/buffypyclient.py
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 |
|
pin(value=True, response_version=None, duration_sec=None)
¶
Pin the current response version.
Pinning means telling the server not to delete the response version while gargabe collecting.
If you have a response version that works with your code you can pin it and reuse it as a fallback, if newer reponse version failed with your code
To unpin a response set value=False
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response_version |
str
|
If not defined the current response version will be pinned. alternativly you can pin any other version by providing its name |
None
|
duration_sec |
int
|
If you only want to pin a version temporary, define a time span in seconds |
None
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Version name of the pinned response. |
Source code in buffy/buffypyclient/buffypyclient.py
tag(tag, response_version=None, remove_tag=False)
¶
Tag the current response version. You can attach user defined tags to cached responses. This way you can organize and search your responses.
A valid tag is any alphanummeric string without any whitespaces and minimal 3 chars and maximal 64 chars
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tag |
str
|
If not defined the current response version will be pinned. alternativly you can pin any other version by providing its name |
required |
response_version |
str
|
If not defined the current response version will be tagged. alternativly you can tag any other version by providing its name |
None
|
remove_tag |
bool
|
If |
False
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Version name of the pinned response. |
Source code in buffy/buffypyclient/buffypyclient.py
buffy.buffyserver.api.v1.models.RequestCacheConfiguration
¶
Bases: BaseModel
The request cache configuration can be attached to a request to give Buffy-server instruction how and when to cache a request. At the heart of the configuration is a ReCachingStrategy. the other option is how many versions of a request-response should be cached.
HINT: This is pydantic model. Every class-attribute
is also a parameter
example usage
import json
from buffy.buffypyclient import BuffyPyClient
c = BuffyPyClient()
# Define a strategy. Buffy-server has to re-cache the target url every 3601 seconds.
strategy = ReCachingStrategy.age(seconds=3601)
# Lets pack the strategy in a RequestCacheConfiguration config
config = RequestCacheConfiguration(recaching_strategy=strategy, max_cached_unpinned_versions=3)
# Lets attach the config to our request
req = c.create_request("https://wikipedia.org",cache_configuration=config)
Source code in buffy/buffyserver/api/v1/models.py
max_cached_pinned_versions: int = 10
class-attribute
¶
When more than max_cached_pinned_versions
request-responses are cached by the Buffy-server, any older cached version will be deleted.
max_cached_unpinned_versions: int = 2
class-attribute
¶
When more than max_cached_unpinned_versions
request-responses that are not pinned are cached by the Buffy-server, any older cached version will be deleted.