Skip to main content

SWORDv3 Client

Documentation Status

This client library provides all the basic features of SWORDv3 as a Python API.

Example Usage

Create a new instance of the client

from sword3client import SWORD3Client
client = SWORD3Client()

See the object definitions for full details of all operations available. Examples of common usage would be:

  • Create an object with metadata only:
from sword3common import Metadata
from sword3client import SWORD3Client
client = SWORD3Client()

SERVICE = "http://example.com/service-document"
metadata = Metadata()
metadata.add_dc_field("creator", "Test")
response = client.create_object_with_metadata(SERVICE, metadata)
  • Create an object with a package:
from sword3common import constants
from sword3client import SWORD3Client
client = SWORD3Client()

package_path = "/path/to/file.zip"
digest = {constants.DIGEST_SHA_256: "digest...."}
SERVICE = "http://example.com/service-document"
with open(package_path, "rb") as stream:
    response = client.create_object_with_package(
                    SERVICE,
                    stream,
                    "test.zip",
                    digest,
                    content_type="application/zip",
                    packaging=constants.PACKAGE_SWORDBAGIT,
                )
  • Retrieve the Object's status:
from sword3client import SWORD3Client
client = SWORD3Client()

OBJ_URL = "http://example.com/object/1"
status = client.get_object(OBJ_URL)
  • Append a binary file:
from sword3client import SWORD3Client
client = SWORD3Client()

OBJ_URL = "http://example.com/object/1"
file_path = "/path/to/binary.bin"
digest = {sword3common.constants.DIGEST_SHA_256: "digest...."}
with open(file_path, "rb") as stream:
    response = client.add_binary(OBJ_URL, stream, "test.bin", digest)
  • Delete the object:
from sword3client import SWORD3Client
client = SWORD3Client()

OBJ_URL = "http://example.com/object/1"
response = client.delete_object(OBJ_URL)
  • Create an object by reference:
from sword3common import ByReference
from sword3client import SWORD3Client
client = SWORD3Client()

SERVICE = "http://example.com/service-document"

br = ByReference()
br.add_file("http://example.com/file.pdf",
            "file.pdf",
            "application/pdf",
            True)

response = client.create_object_by_reference(SERVICE, br)
  • Upload a large file by segments
from io import BytesIO
from sword3common import constants
from sword3client import SWORD3Client
client = SWORD3Client()

SERVICE = "http://example.com/service-document"
FILE_SIZE = 1000000
SEGMENT_COUNT = 10
SEGMENT_SIZE = 100000
DIGEST = {constants.DIGEST_SHA_256: "digest...."}
LARGE_FILE = "/path/to/large/file.zip"

# get the service document, which tells us important details on segmented uploads
service_document = client.get_service(SERVICE)

# initialise the upload, to get a temporary url
resp = client.initialise_segmented_upload(
    service_document,
    assembled_size=FILE_SIZE,
    segment_count=SEGMENT_COUNT,
    segment_size=SEGMENT_SIZE,
    digest=DIGEST
)
temporary_url = resp.location

# send each segment to the temporary url
with open(LARGE_FILE, "rb") as f:
    for i in range(SEGMENT_COUNT):
        segment = f.read(SEGMENT_SIZE)
        stream = BytesIO(segment)
        segment_response = client.upload_file_segment(temporary_url, stream, i)
  • Retrieve information about a segmented upload
from sword3client import SWORD3Client
client = SWORD3Client()

# Temporary URL obtained from initialisation of segmented upload step (see above)
TEMPORARY_URL = "http://example.com/temporary_url"

upload_status = client.segmented_upload_status(TEMPORARY_URL)

print(upload_status.received)
print(upload_status.expecting)
print(upload_status.size)
print(upload_status.segment_size)
  • Deposit a file uploaded by segments
from sword3client import SWORD3Client
client = SWORD3Client()

SERVICE = "http://example.com/service-document"

# Temporary URL obtained from initialisation of segmented upload step (see above)
TEMPORARY_URL = "http://example.com/temporary_url"

resp = client.create_object_with_temporary_file(SERVICE, 
                                                TEMPORARY_URL, 
                                                "test.zip", 
                                                "application/zip")

Release files for sword3client 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sword3client 0.1
File Size Uploaded
sword3client-0.1.tar.gz 20.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sword3client 0.1
File Interpreter ABI Platform
sword3client-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 46.8 kB

Release files / sword3client-0.1.tar.gz

Download URL sword3client-0.1.tar.gz
Size 20.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3bd452ec37afc6ef12068a6c7d11ca6601ab55debf2b96c2dbf1d028642949c8
BLAKE2b-256 checksum
How to use checksums
de14a4975b2cce068b964c71f44ff1e0ce6dffee199f2597843b31a5d11a89d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

Release files / sword3client-0.1-py3-none-any.whl

Download URL sword3client-0.1-py3-none-any.whl
Size 26.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a4a868ff394004c53b1fecc73ad98a99d87ae47ab80afb406a9e75353f8831ba
BLAKE2b-256 checksum
How to use checksums
e1526f93eced3059340c59cd3233b1b584135d56d43dc929208bdba5d570f5af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page