Skip to main content

Python client for waybiller.com external API

Project description

PyWaybiller

Installation

poetry add pywaybiller

Usage

Get all waybills

import pywaybiller
import datetime

end_date = datetime.datetime.now()
start_date = end_date - datetime.timedelta(days=7)


client = pywaybiller.WaybillerClient(
    api_key="xxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    host="https://staging.app.waybiller.com/external-api",
)

for waybill in client.waybills.all(
    dispatcher_timestamp__gt=start_date.strftime("%Y-%m-%dT%H:%M:%S"),
    dispatcher_timestamp__lt=end_date.strftime("%Y-%m-%dT%H:%M:%S"),
):
    print(waybill)

All endpoint uses generator. It will load data page by page, querying later pages only if you need them.

Saving data for later

import pywaybiller
from pywaybiller.openapi_client import ExternalAPIVehicle
from my_project.models import SomeDjangoModel

instance = SomeDjangoModel()
# Sanitize data for serialization for later use
instance.json_field = pywaybiller.WaybillerClient.sanitize_for_serialization(
    ExternalAPIVehicle(
        id=1,
        reg_number="839PVF",
        trailer_reg_number="042TST",
        inactive=False,
        company_name="Test company",
        company_reg_code="10000042"
    )
)

instance.save()
instance.refresh_from_db()

# Rehydrate the data saved earlier
api_model = pywaybiller.WaybillerClient.deserialize_data(
    instance.json_field, ExternalAPIVehicle
)

Deserialize data method calls pydantic from_dict internally, but hides implementation details that may change in future from the code using the API client.

Updating schema and testing

To update schema from waybiller, run make openapi. It is possible that this command will fail if waybiller schema changed significantly and the patch can not be applied anymore. In this case, you will have to follow the steps below to create a new patch

It is possible that Waybiller schema is not actually compliant with the data that waybiller system sends. This causes problems, as server response will not validate and exception will be raised. To work around this, we patch the schema.

Creating new schema patch

To create a schema patch, get the fresh schema from WB and patch it with the existing patch:

make openapi-fetch
# The next may fail, but then you would need to manually port existing
# patch to the new patched schema
make openapi-apply-patch

Then, copy over the schema from pywaybiller/openapi/waybiller_schema.json to pywaybiller/openapi/waybiller_schema-patched.json and edit it to make it consistent with what server sends (usually this involves removing validation for strings to be non-empty, as the most common issue with schema is that server actually sends empty strings where schema says it can not).

Finally run

make openapi-patch

This will update schema-fixes.patch, which should be commited to the repository.

Testing with waybiller data

Waybiller team can make a dump of all entities for particular endpoint into a big json file, so that we try to deserialize it and see if there are no validation errors. As this kind of dump contains sensitive data, it is not possible to include it in this repository.

To perform the manual test with this data dump, use manual_test_json_dump.py:

# in Waybiller - the management command is not merged to master yet, and may be gone in future
make docker-manage cmd="external_api_export --view waybill --endpoint list" > /tmp/waybills-list.json

# in pywaybiller
poetry run python tests/manual_test_json_dump.py --filename /tmp/waybills-list.json  --skip-lines 1 --model ExternalAPIWaybillList

To perform the manual test by direct API calls, use manual_test_api.py:

poetry run python tests/manual_test_api.py --model ExternalAPIWaybillList --api-key 'SECRET'

Note that you probably need to separately test list and detail endpoints, as schema is different.

This needs to be repeated for all endpoints you want to check. Currently, only waybill list and retrieve endpoints are tested thoroughly with both staging and production data in this way.

Credits

This project is generated with help of dockerized openapi generator: https://hub.docker.com/r/openapitools/openapi-generator-cli

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pywaybiller-0.2.0.tar.gz (141.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pywaybiller-0.2.0-py3-none-any.whl (439.1 kB view details)

Uploaded Python 3

File details

Details for the file pywaybiller-0.2.0.tar.gz.

File metadata

  • Download URL: pywaybiller-0.2.0.tar.gz
  • Upload date:
  • Size: 141.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.13.2 Linux/6.13.8-arch1-1

File hashes

Hashes for pywaybiller-0.2.0.tar.gz
Algorithm Hash digest
SHA256 53f6ec08f9a32f71712054ad6eff9a16475e5ff40722f74a7ddd1e43f4534bf3
MD5 19854fe7d4164accb04a0ecf4c2c5ea9
BLAKE2b-256 6e04559a7a7fc5d87420e44b4fc14c9be2f544a51811602d5b412a53df4fcb90

See more details on using hashes here.

File details

Details for the file pywaybiller-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pywaybiller-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 439.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.13.2 Linux/6.13.8-arch1-1

File hashes

Hashes for pywaybiller-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 43afd197b53502e5a21ef0b6ec9eb8e162503bea7eafa42821ba629f182088dd
MD5 d0650ad62682e68a6438074596f47d7a
BLAKE2b-256 a608642f3becc27948634a93b16014723290896c67e22e278ec3db43a26746aa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page