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 @@ -15,6 +15,9 @@ All notable changes to `fopost/sdk` are documented here.

### Added

- `validateBulkImport()` and `commitBulkImport()` 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.
- `$client->inbox()->resolve($workspaceId, $type, $accountId = null, $before = null)` 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
29 changes: 24 additions & 5 deletions src/Resource/PostsResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,17 @@ public function unpublish(string $postId): UnpublishResult
* Check a CSV of posts row by row without creating anything.
*
* @param string|array{filename: string, contents: string, mimeType?: string} $file A path or an in-memory file.
* @param array<int, string> $accounts Ids or platform names for rows whose accounts cell is empty.
*/
public function validateBulkImport(string $workspaceId, string|array $file): BulkImportValidation
{
public function validateBulkImport(
string $workspaceId,
string|array $file,
array $accounts = [],
): BulkImportValidation {
$body = $this->http->requestMultipart(
'POST',
'/posts/bulk-import/validate',
['workspace_id' => $workspaceId],
self::bulkImportFields($workspaceId, $accounts),
self::normalizeFiles([$file]),
'file',
);
Expand All @@ -356,20 +360,35 @@ public function validateBulkImport(string $workspaceId, string|array $file): Bul
* Create every valid row of the CSV as a post. Needs the `publish` scope as well.
*
* @param string|array{filename: string, contents: string, mimeType?: string} $file A path or an in-memory file.
* @param array<int, string> $accounts Ids or platform names for rows whose accounts cell is empty.
*/
public function commitBulkImport(string $workspaceId, string|array $file): BulkImportCommit
public function commitBulkImport(string $workspaceId, string|array $file, array $accounts = []): BulkImportCommit
{
$body = $this->http->requestMultipart(
'POST',
'/posts/bulk-import/commit',
['workspace_id' => $workspaceId],
self::bulkImportFields($workspaceId, $accounts),
self::normalizeFiles([$file]),
'file',
);

return BulkImportCommit::fromArray(self::unwrap($body));
}

/**
* @param array<int, string> $accounts
* @return array<string, string>
*/
private static function bulkImportFields(string $workspaceId, array $accounts): array
{
$fields = ['workspace_id' => $workspaceId];
if ($accounts !== []) {
$fields['accounts'] = implode(',', $accounts);
}

return $fields;
}

/** Delete every post a bulk import created. */
public function rollbackBulkImport(string $batchId): BulkImportRollback
{
Expand Down
7 changes: 6 additions & 1 deletion tests/PostsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function testBulkImportSendsTheCsvAsMultipart(): void

$csv = ['filename' => 'posts.csv', 'contents' => "text\nHi\n", 'mimeType' => 'text/csv'];
$check = $this->client()->posts()->validateBulkImport('w_1', $csv);
$commit = $this->client()->posts()->commitBulkImport('w_1', $csv);
$commit = $this->client()->posts()->commitBulkImport('w_1', $csv, ['twitter', 'linkedin']);
$rollback = $this->client()->posts()->rollbackBulkImport('b_1');

$first = $this->transport->requests[0];
Expand All @@ -248,10 +248,15 @@ public function testBulkImportSendsTheCsvAsMultipart(): void
$this->assertStringStartsWith('multipart/form-data; boundary=', $first['headers']['Content-Type']);
$this->assertStringContainsString('name="workspace_id"' . "\r\n\r\nw_1", (string) $first['body']);
$this->assertStringContainsString('name="file"; filename="posts.csv"', (string) $first['body']);
$this->assertStringNotContainsString('name="accounts"', (string) $first['body']);
$this->assertSame(1, $check->invalidRows);
$this->assertSame(['unknown account'], $check->rows[0]->errors);

$this->assertSame('https://api.fopost.com/v1/posts/bulk-import/commit', $this->transport->requests[1]['url']);
$this->assertStringContainsString(
'name="accounts"' . "\r\n\r\ntwitter,linkedin",
(string) $this->transport->requests[1]['body'],
);
$this->assertSame('b_1', $commit->batchId);
$this->assertSame('2026-03-01', $commit->posts[0]->scheduleAt?->format('Y-m-d'));

Expand Down
Loading