publish.py
1.1 kB · python · 38 lines
1from automator.core.api import check_errors, shopify_request2from automator.core.config import SHOPIFY_HEADLESS_ID, SHOPIFY_ONLINE_STORE_ID3from automator.core.models import Task4from automator.core.steps import Step5from datetime import datetime, timezone67DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"89MUTATION = """10mutation publishablePublish($id: ID!, $input: [PublicationInput!]!) {11 publishablePublish(id: $id, input: $input) {12 publishable {13 ... on Product {14 id15 status16 publishedAt17 }18 }19 userErrors {20 field21 message22 }23 }24}25"""2627CHANNELS = [SHOPIFY_ONLINE_STORE_ID, SHOPIFY_HEADLESS_ID]2829def set_input() -> list[dict]:30 now = datetime.now(timezone.utc).strftime(DATETIME_FORMAT)31 return [{"publicationId": one, "publishDate": now} for one in CHANNELS if one]3233def mrly_publish(task: Task) -> Task:34 variables = {"id": task.product.shopify_id, "input": set_input()}35 result = shopify_request(task, MUTATION, variables)36 check_errors(result, "publishablePublish")37 task.place(Step.COMPLETE)38 return task