Shipper API
Overview
The shipper module is the write/delivery layer for GKC outputs.
For Wikibase-compatible targets (including Data Distillery and Wikidata), use WikibaseShipper.
Note: WikibaseShipper works with any Wikibase instance. Configure the target via the api_url parameter in your WikiverseAuth object.
Quick Start
from gkc import WikiverseAuth
from gkc.shipper import WikibaseShipper
# Example: Wikidata
auth = WikiverseAuth(
username="my_username",
password="my_password",
api_url="https://www.wikidata.org/w/api.php",
)
auth.login()
# Or Data Distillery
# auth = WikiverseAuth(
# username="my_username",
# password="my_password",
# api_url="https://datadistillery.wikibase.cloud/w/api.php",
# )
shipper = WikibaseShipper(auth=auth, dry_run_default=True)
result = shipper.write_item(
payload={
"labels": {"en": {"language": "en", "value": "Test item"}},
"descriptions": {"en": {"language": "en", "value": "Created from shipper docs"}},
},
summary="Create test item",
)
print(result.status)
Public API Quick Starts
WriteResult
from gkc.shipper import WriteResult
result = WriteResult(
entity_id="Q123",
revision_id=456,
status="submitted",
warnings=[],
api_response={"success": 1},
)
as_dict = result.to_dict()
as_json = result.to_json()
print(as_dict["entity_id"], as_json)
DiffOperation and DiffPlan
from gkc.shipper import DiffOperation, DiffPlan
operation = DiffOperation(
kind="item",
label="GKC Property Specification",
status="create",
reasons=["label_not_found"],
)
plan = DiffPlan(operations=[operation], summary={"total": 1, "create": 1})
print(operation.to_dict())
print(plan.to_dict())
Shipper (base interface)
from gkc.shipper import Shipper, ShipperError
class DemoShipper(Shipper):
def write(self, payload, **kwargs):
raise ShipperError("Demo write failure")
try:
DemoShipper().write({"foo": "bar"})
except ShipperError:
pass
WikibaseShipper.plan_batch()
from gkc import WikiverseAuth
from gkc.shipper import WikibaseShipper
auth = WikiverseAuth(
username="my_username",
password="my_password",
api_url="https://datadistillery.wikibase.cloud/w/api.php",
)
auth.login()
shipper = WikibaseShipper(auth=auth)
plan = shipper.plan_batch(
[
{
"kind": "item",
"label": "GKC Query Entity",
"payload": {
"labels": {"en": {"language": "en", "value": "GKC Query Entity"}},
"descriptions": {"en": {"language": "en", "value": "Classifier for query entities"}},
},
},
{
"kind": "property",
"label": "query reference",
"datatype": "wikibase-item",
"payload": {
"labels": {"en": {"language": "en", "value": "query reference"}},
"descriptions": {"en": {"language": "en", "value": "Links to query entities"}},
},
},
]
)
print(plan.summary)
for op in plan.operations:
print(op.status, op.kind, op.label)
WikibaseShipper.write_item()
from gkc import WikiverseAuth
from gkc.shipper import WikibaseShipper
auth = WikiverseAuth(
username="my_username",
password="my_password",
api_url="https://datadistillery.wikibase.cloud/w/api.php",
)
auth.login()
shipper = WikibaseShipper(auth=auth, dry_run_default=True)
# Validation-only call
validated = shipper.write_item(
payload={
"labels": {"en": {"language": "en", "value": "Validation sample"}},
"descriptions": {"en": {"language": "en", "value": "Validate item payload"}},
},
summary="Validate item payload",
validate_only=True,
)
# Dry-run update call
update_preview = shipper.write_item(
payload={"descriptions": {"en": {"language": "en", "value": "Updated description"}}},
summary="Preview item update",
entity_id="Q1",
dry_run=True,
)
print(validated.status, update_preview.status)
WikibaseShipper.write_property()
from gkc import WikiverseAuth
from gkc.shipper import WikibaseShipper
auth = WikiverseAuth(
username="my_username",
password="my_password",
api_url="https://datadistillery.wikibase.cloud/w/api.php",
)
auth.login()
shipper = WikibaseShipper(auth=auth, dry_run_default=True)
property_preview = shipper.write_property(
payload={
"labels": {"en": {"language": "en", "value": "has specification"}},
"descriptions": {"en": {"language": "en", "value": "Links a property to specification entities"}},
},
datatype="wikibase-item",
summary="Preview property create",
dry_run=True,
)
print(property_preview.status)
print(property_preview.request_payload)
CommonsShipper
from gkc import WikiverseAuth
from gkc.shipper import CommonsShipper
auth = WikiverseAuth(api_url="https://commons.wikimedia.org/w/api.php")
shipper = CommonsShipper(auth=auth)
try:
shipper.write(payload={"filename": "example.jpg"})
except NotImplementedError:
pass
OpenStreetMapShipper
from gkc.auth import OpenStreetMapAuth
from gkc.shipper import OpenStreetMapShipper
auth = OpenStreetMapAuth(username="my_osm_user", password="my_osm_password")
shipper = OpenStreetMapShipper(auth=auth)
try:
shipper.write(payload={"type": "node"})
except NotImplementedError:
pass
Data Distillery Write Contract Note
For Data Distillery property creation requests (new=property), datatype is embedded in serialized data payload JSON.
Use write_property() to preserve this request shape.
API Reference (mkdocstrings)
ShipperError
Bases: Exception
Raised when a shipper operation fails.
Plain meaning: A write or validation step failed.
Source code in gkc/shipper.py
83 84 85 86 87 | |
WriteResult
Result summary for write operations.
Plain meaning: A stable summary of what happened during a write.
Source code in gkc/shipper.py
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 | |
to_dict()
Return a JSON-serializable dictionary.
Plain meaning: Convert the result into a simple dict.
Source code in gkc/shipper.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
to_json()
Serialize the result to JSON.
Plain meaning: Turn the result into a JSON string.
Source code in gkc/shipper.py
121 122 123 124 125 126 127 | |
DiffOperation
Planned diff operation for a Wikibase entity or property.
Plain meaning: One create/update/no-op decision with payload details.
Source code in gkc/shipper.py
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 | |
DiffPlan
Aggregated planning result for a batch write operation.
Plain meaning: What will be created/updated/skipped before writing.
Source code in gkc/shipper.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
Shipper
Base class for shippers.
Shippers are responsible for executing write operations against external systems (Wikibase, Wikimedia Commons, OpenStreetMap, etc.). This base class defines the contract for all shipper implementations.
Implementing a New Shipper
To add a shipper for a new target system:
-
Subclass Shipper:
class MyShipper(Shipper): def __init__(self, auth: MyAuth, **kwargs): self.auth = auth -
Implement target-specific write methods:
- Methods should accept a payload dict and return WriteResult
- Support
dry_run,validate_only,summaryparameters where applicable -
Use target-appropriate logging, authentication, and API patterns
-
Return WriteResult from all write operations:
- Sets entity_id, revision_id, status appropriately
- Includes warnings, api_response, request_payload for introspection
-
Metadata dict for target-specific extra info
-
Raise ShipperError for operational failures:
- Network errors, authentication failures, invalid payloads
-
Include context about what operation failed and why
-
Log operations appropriately:
- Use Python logging module (import logging)
- Log at INFO for successful operations, DEBUG for details
-
Include operation type, entity_id, and outcome
-
Document your shipper:
- Docstring on class explaining target API
- Method docstrings with examples
- Update docs/gkc/api/shipper.md with quick start and examples
Examples
WikibaseShipper (Wikibase instances): - Implements write_item(), write_property(), plan_batch() - Works with Wikidata, Data Distillery, any wbeditentity API
Future CommonsShipper (Wikimedia Commons): - May reuse WikibaseShipper for structured data - Will add upload_file(), write_categories() methods
Future OpenStreetMapShipper (OpenStreetMap): - Different API (XML-based, not MediaWiki) - Will implement write_node(), write_way(), write_relation()
Plain meaning: A shared interface for writing Bottled output to targets.
Source code in gkc/shipper.py
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 | |
write(payload, **kwargs)
Write the payload to a target system.
Subclasses may implement this method, though target-specific methods (write_item, write_node, upload_file, etc.) are preferred.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
dict
|
Target-system-specific payload dict |
required |
**kwargs
|
Any
|
Target-specific parameters |
{}
|
Returns:
| Type | Description |
|---|---|
WriteResult
|
WriteResult with operation outcome and details |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always (must be implemented by subclasses) |
ShipperError
|
For operational failures |
Plain meaning: Deliver Bottled output to an external API.
Source code in gkc/shipper.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
WikibaseShipper
Bases: Shipper
Shipper for Wikibase write operations.
Plain meaning: Submit Bottled output to any Wikibase instance API.
Source code in gkc/shipper.py
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 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 | |
__init__(auth, api_url=None, dry_run_default=True)
Initialize the Wikibase shipper.
Plain meaning: Store auth details and default write behavior.
Source code in gkc/shipper.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
plan_batch(operations, *, language='en')
Build a create/update/no-op plan for a batch of Wikibase writes.
Each operation supports
- kind: "item" or "property"
- label: string label used for matching when entity_id not provided
- payload: desired wbeditentity JSON fragment
- entity_id: optional explicit target ID
- datatype: optional for property creation checks
Plain meaning: Preview what will be created or changed before writing.
Source code in gkc/shipper.py
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 | |
write_item(payload, summary, entity_id=None, dry_run=None, validate_only=False, tags=None, bot=False, metadata=None)
Create or update a Wikibase item.
Plain meaning: Build a request for wbeditentity, optionally submit it, and return a stable result summary.
Source code in gkc/shipper.py
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 | |
write_property(payload, summary, datatype, entity_id=None, dry_run=None, validate_only=False, tags=None, bot=False, metadata=None)
Create or update a Wikibase property.
Plain meaning: Build a request for wbeditentity (property variant), optionally submit it, and return a stable result summary.
Source code in gkc/shipper.py
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 | |
CommonsShipper
Bases: Shipper
Shipper scaffold for Wikimedia Commons.
Plain meaning: Reserved for future Commons write support.
Source code in gkc/shipper.py
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 | |
__init__(auth, api_url=None)
Initialize the Commons shipper.
Plain meaning: Store auth details for future Commons writes.
Source code in gkc/shipper.py
1051 1052 1053 1054 1055 1056 1057 1058 | |
write(payload, **kwargs)
Write payload to Wikimedia Commons.
Plain meaning: Placeholder for future Commons write support.
Source code in gkc/shipper.py
1060 1061 1062 1063 1064 1065 1066 | |
OpenStreetMapShipper
Bases: Shipper
Shipper scaffold for OpenStreetMap.
Plain meaning: Reserved for future OpenStreetMap write support.
Source code in gkc/shipper.py
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 | |
__init__(auth)
Initialize the OpenStreetMap shipper.
Plain meaning: Store auth details for future OpenStreetMap writes.
Source code in gkc/shipper.py
1075 1076 1077 1078 1079 1080 1081 | |
write(payload, **kwargs)
Write payload to OpenStreetMap.
Plain meaning: Placeholder for future OpenStreetMap write support.
Source code in gkc/shipper.py
1083 1084 1085 1086 1087 1088 1089 | |
Migration Note
Deprecated: WikidataShipper has been removed as of this version. Use WikibaseShipper instead—it works with all Wikibase instances including Wikidata.
Migration:
# Before (deprecated)
from gkc.shipper import WikidataShipper
shipper = WikidataShipper(auth=auth)
# After
from gkc.shipper import WikibaseShipper
shipper = WikibaseShipper(auth=auth)