ebay_client/examples/media_workflows.py

91 lines
No EOL
2.9 KiB
Python

from __future__ import annotations
import os
from pathlib import Path
from ebay_client.client import EbayClient
from ebay_client.core.auth.models import EbayOAuthConfig
from ebay_client.generated.media.models import (
CreateDocumentRequest,
CreateImageFromUrlRequest,
CreateVideoRequest,
)
def build_client() -> EbayClient:
oauth_config = EbayOAuthConfig(
client_id=os.environ["EBAY_CLIENT_ID"],
client_secret=os.environ["EBAY_CLIENT_SECRET"],
default_scopes=["https://api.ebay.com/oauth/api_scope/sell.inventory"],
)
return EbayClient(oauth_config)
def upload_image_from_file(client: EbayClient, image_path: Path) -> None:
image = client.media.create_image_from_path(image_path)
print("image_url:", image.imageUrl)
def upload_image_from_url(client: EbayClient, image_url: str) -> None:
image = client.media.create_image_from_url(CreateImageFromUrlRequest(imageUrl=image_url))
print("image_url:", image.imageUrl)
def upload_document_and_wait(client: EbayClient, document_path: Path) -> None:
result = client.media.create_upload_and_wait_document(
CreateDocumentRequest(
documentType="USER_GUIDE_OR_MANUAL",
languages=["en-US"],
),
file_name=document_path.name,
content=document_path.read_bytes(),
content_type="application/pdf",
timeout_seconds=60.0,
)
print("document_id:", result.document_id)
print("document_final_status:", result.document.documentStatus)
def upload_document_and_wait_from_path(client: EbayClient, document_path: Path) -> None:
result = client.media.create_upload_and_wait_document_from_path(
CreateDocumentRequest(
documentType="USER_GUIDE_OR_MANUAL",
languages=["en-US"],
),
document_path,
timeout_seconds=60.0,
)
print("document_id:", result.document_id)
print("document_final_status:", result.document.documentStatus)
def upload_video_and_wait(client: EbayClient, video_path: Path) -> None:
result = client.media.create_upload_and_wait_video_from_path(
video_path,
description="Example upload from the ebay-rest-client workspace.",
timeout_seconds=120.0,
)
print("video_id:", result.video_id)
print("video_status:", result.video.status)
def main() -> None:
client = build_client()
image_file = os.environ.get("EBAY_MEDIA_IMAGE_FILE")
image_url = os.environ.get("EBAY_MEDIA_IMAGE_URL")
document_file = os.environ.get("EBAY_MEDIA_DOCUMENT_FILE")
video_file = os.environ.get("EBAY_MEDIA_VIDEO_FILE")
if image_file:
upload_image_from_file(client, Path(image_file))
if image_url:
upload_image_from_url(client, image_url)
if document_file:
upload_document_and_wait_from_path(client, Path(document_file))
if video_file:
upload_video_and_wait(client, Path(video_file))
if __name__ == "__main__":
main()