Add media content type inference and path-based upload methods for images and documents
This commit is contained in:
parent
937cd86c8b
commit
6d54c5900c
4 changed files with 202 additions and 16 deletions
|
|
@ -36,7 +36,7 @@ from ebay_client.generated.notification.models import (
|
|||
UpdateSubscriptionRequest,
|
||||
)
|
||||
from ebay_client.inventory.client import InventoryClient
|
||||
from ebay_client.media.client import CreatedMediaResource, MediaClient, extract_resource_id
|
||||
from ebay_client.media.client import CreatedMediaResource, MediaClient, extract_resource_id, guess_media_content_type
|
||||
from ebay_client.notification.client import NotificationClient
|
||||
|
||||
|
||||
|
|
@ -720,4 +720,116 @@ def test_media_convenience_methods_raise_when_required_ids_are_missing(monkeypat
|
|||
except RuntimeError:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError("Expected convenience method to raise when eBay omits the required identifier")
|
||||
raise AssertionError("Expected convenience method to raise when eBay omits the required identifier")
|
||||
|
||||
|
||||
def test_guess_media_content_type_uses_filename_extension() -> None:
|
||||
assert guess_media_content_type("photo.jpg") == "image/jpeg"
|
||||
assert guess_media_content_type("guide.pdf") == "application/pdf"
|
||||
assert guess_media_content_type("unknown.custom") == "application/octet-stream"
|
||||
|
||||
|
||||
def test_media_create_image_from_path_reads_file_and_infers_content_type(tmp_path, monkeypatch) -> None:
|
||||
client = MediaClient(build_transport())
|
||||
image_path = tmp_path / "photo.png"
|
||||
image_path.write_bytes(b"png-data")
|
||||
|
||||
captured: dict[str, object] = {}
|
||||
|
||||
monkeypatch.setattr(
|
||||
client,
|
||||
"create_image_from_file",
|
||||
lambda **kwargs: captured.update(kwargs) or ImageResponse(imageUrl="https://example.test/image"),
|
||||
)
|
||||
|
||||
result = client.create_image_from_path(image_path)
|
||||
|
||||
assert result.imageUrl == "https://example.test/image"
|
||||
assert captured == {
|
||||
"file_name": "photo.png",
|
||||
"content": b"png-data",
|
||||
"content_type": "image/png",
|
||||
}
|
||||
|
||||
|
||||
def test_media_upload_document_from_path_reads_file_and_infers_content_type(tmp_path, monkeypatch) -> None:
|
||||
client = MediaClient(build_transport())
|
||||
document_path = tmp_path / "guide.pdf"
|
||||
document_path.write_bytes(b"%PDF-1.7")
|
||||
|
||||
captured: dict[str, object] = {}
|
||||
|
||||
monkeypatch.setattr(
|
||||
client,
|
||||
"upload_document",
|
||||
lambda document_id, **kwargs: captured.update({"document_id": document_id, **kwargs})
|
||||
or DocumentResponse(documentId=document_id, documentStatus="SUBMITTED"),
|
||||
)
|
||||
|
||||
result = client.upload_document_from_path("DOC-42", document_path)
|
||||
|
||||
assert result.documentId == "DOC-42"
|
||||
assert captured == {
|
||||
"document_id": "DOC-42",
|
||||
"file_name": "guide.pdf",
|
||||
"content": b"%PDF-1.7",
|
||||
"content_type": "application/pdf",
|
||||
}
|
||||
|
||||
|
||||
def test_media_create_upload_and_wait_document_from_path_reads_file_and_delegates(tmp_path, monkeypatch) -> None:
|
||||
client = MediaClient(build_transport())
|
||||
document_path = tmp_path / "guide.pdf"
|
||||
document_path.write_bytes(b"%PDF-1.7")
|
||||
|
||||
captured: dict[str, object] = {}
|
||||
|
||||
monkeypatch.setattr(
|
||||
client,
|
||||
"create_upload_and_wait_document",
|
||||
lambda payload, **kwargs: captured.update({"payload": payload, **kwargs})
|
||||
or DocumentResponse(documentId="DOC-77", documentStatus="ACCEPTED"),
|
||||
)
|
||||
|
||||
result = client.create_upload_and_wait_document_from_path(
|
||||
CreateDocumentRequest(documentType="USER_GUIDE_OR_MANUAL", languages=["en-US"]),
|
||||
document_path,
|
||||
poll_interval_seconds=0.0,
|
||||
)
|
||||
|
||||
assert result.documentStatus == "ACCEPTED"
|
||||
assert captured["file_name"] == "guide.pdf"
|
||||
assert captured["content"] == b"%PDF-1.7"
|
||||
assert captured["content_type"] == "application/pdf"
|
||||
assert captured["poll_interval_seconds"] == 0.0
|
||||
|
||||
|
||||
def test_media_create_upload_and_wait_video_from_path_builds_payload_and_delegates(tmp_path, monkeypatch) -> None:
|
||||
client = MediaClient(build_transport())
|
||||
video_path = tmp_path / "demo.mp4"
|
||||
video_path.write_bytes(b"video-data")
|
||||
|
||||
captured: dict[str, object] = {}
|
||||
|
||||
monkeypatch.setattr(
|
||||
client,
|
||||
"create_upload_and_wait_video",
|
||||
lambda payload, **kwargs: captured.update({"payload": payload, **kwargs})
|
||||
or Video(videoId="VIDEO-88", status="LIVE"),
|
||||
)
|
||||
|
||||
result = client.create_upload_and_wait_video_from_path(
|
||||
video_path,
|
||||
description="demo video",
|
||||
poll_interval_seconds=0.0,
|
||||
)
|
||||
|
||||
assert result.videoId == "VIDEO-88"
|
||||
payload = captured["payload"]
|
||||
assert isinstance(payload, CreateVideoRequest)
|
||||
assert payload.title == "demo"
|
||||
assert payload.size == len(b"video-data")
|
||||
assert payload.classification == ["ITEM"]
|
||||
assert payload.description == "demo video"
|
||||
assert captured["content"] == b"video-data"
|
||||
assert captured["poll_interval_seconds"] == 0.0
|
||||
Loading…
Add table
Add a link
Reference in a new issue