diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ccf3cf..6e58dc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ All notable changes to `fopost` are documented here. ### Added +- `posts.validate_bulk_import` and `posts.commit_bulk_import` take an optional `accounts` + list (account ids or platform names), sent comma-separated and applied to every CSV row + whose own `accounts` cell is empty. - `inbox.resolve(workspace_id=, type=, account_id=None, before=None)` resolves every unanswered item of one type, optionally only on some accounts or only items dated before `before`, and returns how many changed. Nothing is sent to a platform. diff --git a/src/fopost/resources/posts.py b/src/fopost/resources/posts.py index 520d7cd..354fbf5 100644 --- a/src/fopost/resources/posts.py +++ b/src/fopost/resources/posts.py @@ -107,6 +107,13 @@ def _normalize_content(content: str | Sequence[ContentInput]) -> list[dict[str, return out +def _bulk_fields(workspace_id: str, accounts: Sequence[str] | None) -> dict[str, str]: + fields = {"workspace_id": workspace_id} + if accounts: + fields["accounts"] = ",".join(accounts) + return fields + + def _normalize_accounts(accounts: Sequence[AccountInput]) -> list[str]: """The API takes bare account ids; also accept account objects or ``{"id": ...}``.""" out: list[str] = [] @@ -391,22 +398,40 @@ def unpublish(self, post_id: str) -> UnpublishResult: def deliveries(self, post_id: str) -> builtins.list[Delivery]: return parse_list(Delivery, unwrap(self._http.get(f"/posts/{post_id}/deliveries"))) - def validate_bulk_import(self, *, workspace_id: str, file: FileInput) -> BulkImportValidation: - """Dry-run a CSV/TSV import and return per-row results without creating anything.""" + def validate_bulk_import( + self, + *, + workspace_id: str, + file: FileInput, + accounts: Sequence[str] | None = None, + ) -> BulkImportValidation: + """Dry-run a CSV/TSV import and return per-row results without creating anything. + + ``accounts`` (ids or platform names) applies to rows whose own accounts cell is empty. + """ body = self._http.send_multipart( "POST", "/posts/bulk-import/validate", - fields={"workspace_id": workspace_id}, + fields=_bulk_fields(workspace_id, accounts), files=[("file", _read_file(file))], ) return BulkImportValidation.model_validate(unwrap(body)) - def commit_bulk_import(self, *, workspace_id: str, file: FileInput) -> BulkImportResult: - """Create one scheduled post per row, all-or-nothing, stamped with a batch id.""" + def commit_bulk_import( + self, + *, + workspace_id: str, + file: FileInput, + accounts: Sequence[str] | None = None, + ) -> BulkImportResult: + """Create one scheduled post per row, all-or-nothing, stamped with a batch id. + + ``accounts`` (ids or platform names) applies to rows whose own accounts cell is empty. + """ body = self._http.send_multipart( "POST", "/posts/bulk-import/commit", - fields={"workspace_id": workspace_id}, + fields=_bulk_fields(workspace_id, accounts), files=[("file", _read_file(file))], ) return BulkImportResult.model_validate(unwrap(body)) diff --git a/tests/test_posts.py b/tests/test_posts.py index 46b6106..cc836ba 100644 --- a/tests/test_posts.py +++ b/tests/test_posts.py @@ -302,9 +302,13 @@ def test_bulk_import_validate_commit_and_rollback(client: Fopost) -> None: assert request.headers["content-type"].startswith("multipart/form-data") assert b'name="workspace_id"\r\n\r\nws_1' in request.content assert b'filename="posts.csv"' in request.content + assert b'name="accounts"' not in request.content - done = client.posts.commit_bulk_import(workspace_id="ws_1", file=csv) + done = client.posts.commit_bulk_import( + workspace_id="ws_1", file=csv, accounts=["twitter", "linkedin"] + ) assert commit.called and done.batch_id == "batch_1" and done.posts[0].id == "post_9" + assert b'name="accounts"\r\n\r\ntwitter,linkedin' in commit.calls.last.request.content assert client.posts.rollback_bulk_import("batch_1").deleted == 1 assert rollback.called