Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
37 changes: 31 additions & 6 deletions src/fopost/resources/posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand Down Expand Up @@ -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))
Expand Down
6 changes: 5 additions & 1 deletion tests/test_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading