SWORDv3 Client Library
Project description
SWORDv3 Client
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")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sword3client-0.1.tar.gz
(20.2 kB
view details)
Built Distribution
File details
Details for the file sword3client-0.1.tar.gz
.
File metadata
- Download URL: sword3client-0.1.tar.gz
- Upload date:
- Size: 20.2 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3bd452ec37afc6ef12068a6c7d11ca6601ab55debf2b96c2dbf1d028642949c8 |
|
MD5 | 5b2e031fff81143bd011145059be70f1 |
|
BLAKE2b-256 | de14a4975b2cce068b964c71f44ff1e0ce6dffee199f2597843b31a5d11a89d2 |
File details
Details for the file sword3client-0.1-py3-none-any.whl
.
File metadata
- Download URL: sword3client-0.1-py3-none-any.whl
- Upload date:
- Size: 26.6 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4a868ff394004c53b1fecc73ad98a99d87ae47ab80afb406a9e75353f8831ba |
|
MD5 | 96ea8f46b3a3689cc316ef3fa0dde1ee |
|
BLAKE2b-256 | e1526f93eced3059340c59cd3233b1b584135d56d43dc929208bdba5d570f5af |