ebay_client/examples/media_workflows.py

86 lines
No EOL
2.7 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_file(
file_name=image_path.name,
content=image_path.read_bytes(),
content_type="image/jpeg",
)
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:
accepted = 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_final_status:", accepted.documentStatus)
def upload_video_and_wait(client: EbayClient, video_path: Path) -> None:
live_video = client.media.create_upload_and_wait_video(
CreateVideoRequest(
title=video_path.stem,
size=video_path.stat().st_size,
classification=["ITEM"],
description="Example upload from the ebay-rest-client workspace.",
),
content=video_path.read_bytes(),
timeout_seconds=120.0,
)
print("video_status:", live_video.status)
print("video_id:", live_video.videoId)
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(client, Path(document_file))
if video_file:
upload_video_and_wait(client, Path(video_file))
if __name__ == "__main__":
main()