-
Notifications
You must be signed in to change notification settings - Fork 11
[3.6.0] Contacts Batch API Import #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c96bb2e
add Contact Imports API functionality with tests and examples
gaalferov e4cab76
update readme in example
gaalferov 76e1cc1
validate input for importContacts method and add corresponding tests
gaalferov 7c44050
refactor toArray method in ImportContact to simplify return structure
gaalferov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| use Mailtrap\Config; | ||
| use Mailtrap\DTO\Request\Contact\CreateContact; | ||
| use Mailtrap\DTO\Request\Contact\ImportContact; | ||
| use Mailtrap\DTO\Request\Contact\UpdateContact; | ||
| use Mailtrap\Helper\ResponseHelper; | ||
| use Mailtrap\MailtrapGeneralClient; | ||
|
|
@@ -271,3 +272,51 @@ | |
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Import contacts in bulk with support for custom fields and list management. | ||
| * Existing contacts with matching email addresses will be updated automatically. | ||
| * You can import up to 50,000 contacts per request. | ||
| * The import process runs asynchronously - use the returned import ID to check the status and results. | ||
| * | ||
| * POST https://mailtrap.io/api/accounts/{account_id}/contacts/imports | ||
| */ | ||
| try { | ||
| $contactsToImport = [ | ||
| new ImportContact( | ||
| email: '[email protected]', | ||
| fields: ['first_name' => 'John', 'last_name' => 'Smith', 'zip_code' => 11111], | ||
| listIdsIncluded: [1, 2], | ||
| listIdsExcluded: [4, 5] | ||
| ), | ||
| new ImportContact( | ||
| email: '[email protected]', | ||
| fields: ['first_name' => 'Joe', 'last_name' => 'Doe', 'zip_code' => 22222], | ||
| listIdsIncluded: [1], | ||
| listIdsExcluded: [4] | ||
| ), | ||
| ]; | ||
|
|
||
| $response = $contacts->importContacts($contactsToImport); | ||
| // print the response body (array) | ||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Get the status of a contact import by ID. | ||
| * | ||
| * GET https://mailtrap.io/api/accounts/{account_id}/contacts/imports/{import_id} | ||
| */ | ||
| try { | ||
| $importId = 1; // Replace 1 with the actual import ID | ||
| $response = $contacts->getContactImport($importId); | ||
|
|
||
| // print the response body (array) | ||
| var_dump(ResponseHelper::toArray($response)); | ||
| } catch (Exception $e) { | ||
| echo 'Caught exception: ', $e->getMessage(), PHP_EOL; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Mailtrap\DTO\Request\Contact; | ||
|
|
||
| /** | ||
| * Class ImportContact | ||
| */ | ||
| final class ImportContact implements ContactInterface | ||
| { | ||
| public function __construct( | ||
| private string $email, | ||
| private array $fields = [], | ||
| private array $listIdsIncluded = [], | ||
| private array $listIdsExcluded = [] | ||
| ) { | ||
| } | ||
|
|
||
| public static function init( | ||
| string $email, | ||
| array $fields = [], | ||
| array $listIdsIncluded = [], | ||
| array $listIdsExcluded = [] | ||
| ): self { | ||
| return new self($email, $fields, $listIdsIncluded, $listIdsExcluded); | ||
| } | ||
|
|
||
| public function getEmail(): string | ||
| { | ||
| return $this->email; | ||
| } | ||
|
|
||
| public function getFields(): array | ||
| { | ||
| return $this->fields; | ||
| } | ||
|
|
||
| public function getListIdsIncluded(): array | ||
| { | ||
| return $this->listIdsIncluded; | ||
| } | ||
|
|
||
| public function getListIdsExcluded(): array | ||
| { | ||
| return $this->listIdsExcluded; | ||
| } | ||
|
|
||
| public function toArray(): array | ||
| { | ||
| return array_filter( | ||
| [ | ||
| 'email' => $this->getEmail(), | ||
| 'fields' => $this->getFields(), | ||
| 'list_ids_included' => $this->getListIdsIncluded(), | ||
| 'list_ids_excluded' => $this->getListIdsExcluded(), | ||
| ], | ||
| fn($value) => $value !== null | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| use Mailtrap\Api\General\Contact; | ||
| use Mailtrap\DTO\Request\Contact\CreateContact; | ||
| use Mailtrap\DTO\Request\Contact\UpdateContact; | ||
| use Mailtrap\DTO\Request\Contact\ImportContact; | ||
| use Mailtrap\Exception\HttpClientException; | ||
| use Mailtrap\Tests\MailtrapTestCase; | ||
| use Nyholm\Psr7\Response; | ||
|
|
@@ -476,6 +477,155 @@ public function testDeleteContactField(): void | |
| $this->assertEquals(204, $response->getStatusCode()); | ||
| } | ||
|
|
||
| public function testImportContacts(): void | ||
| { | ||
| $contacts = [ | ||
| new ImportContact( | ||
| email: '[email protected]', | ||
| fields: ['first_name' => 'John', 'last_name' => 'Smith', 'zip_code' => 11111], | ||
| listIdsIncluded: [1, 2, 3], | ||
| listIdsExcluded: [4, 5, 6] | ||
| ), | ||
| new ImportContact( | ||
| email: '[email protected]', | ||
| fields: ['first_name' => 'Joe', 'last_name' => 'Doe', 'zip_code' => 22222], | ||
| listIdsIncluded: [1], | ||
| listIdsExcluded: [4] | ||
| ), | ||
| ]; | ||
|
|
||
| $expectedResponse = [ | ||
| 'id' => 1, | ||
| 'status' => 'created', | ||
| ]; | ||
|
|
||
| $this->contact->expects($this->once()) | ||
| ->method('httpPost') | ||
| ->with( | ||
| AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/contacts/imports', | ||
| [], | ||
| ['contacts' => array_map(fn(ImportContact $contact) => $contact->toArray(), $contacts)] | ||
| ) | ||
| ->willReturn(new Response(201, ['Content-Type' => 'application/json'], json_encode($expectedResponse))); | ||
|
|
||
| $response = $this->contact->importContacts($contacts); | ||
| $responseData = ResponseHelper::toArray($response); | ||
|
|
||
| $this->assertInstanceOf(Response::class, $response); | ||
| $this->assertArrayHasKey('id', $responseData); | ||
| $this->assertEquals(1, $responseData['id']); | ||
| $this->assertEquals('created', $responseData['status']); | ||
| } | ||
|
|
||
| public function testGetContactImportInProgress(): void | ||
| { | ||
| $importId = 1; | ||
| $expectedResponse = [ | ||
| 'id' => $importId, | ||
| 'status' => 'created', | ||
| ]; | ||
|
|
||
| $this->contact->expects($this->once()) | ||
| ->method('httpGet') | ||
| ->with(AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/contacts/imports/' . $importId) | ||
| ->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($expectedResponse))); | ||
|
|
||
| $response = $this->contact->getContactImport($importId); | ||
| $responseData = ResponseHelper::toArray($response); | ||
|
|
||
| $this->assertInstanceOf(Response::class, $response); | ||
| $this->assertArrayHasKey('id', $responseData); | ||
| $this->assertEquals($importId, $responseData['id']); | ||
| $this->assertEquals('created', $responseData['status']); | ||
| } | ||
|
|
||
| public function testGetContactImportFinished(): void | ||
| { | ||
| $importId = 1; | ||
| $expectedResponse = [ | ||
| 'id' => $importId, | ||
| 'status' => 'finished', | ||
| 'created_contacts_count' => 2, | ||
| 'updated_contacts_count' => 0, | ||
| 'contacts_over_limit_count' => 0, | ||
| ]; | ||
|
|
||
| $this->contact->expects($this->once()) | ||
| ->method('httpGet') | ||
| ->with(AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/contacts/imports/' . $importId) | ||
| ->willReturn(new Response(200, ['Content-Type' => 'application/json'], json_encode($expectedResponse))); | ||
|
|
||
| $response = $this->contact->getContactImport($importId); | ||
| $responseData = ResponseHelper::toArray($response); | ||
|
|
||
| $this->assertInstanceOf(Response::class, $response); | ||
| $this->assertArrayHasKey('id', $responseData); | ||
| $this->assertEquals($importId, $responseData['id']); | ||
| $this->assertEquals('finished', $responseData['status']); | ||
| $this->assertEquals(2, $responseData['created_contacts_count']); | ||
| $this->assertEquals(0, $responseData['updated_contacts_count']); | ||
| $this->assertEquals(0, $responseData['contacts_over_limit_count']); | ||
| } | ||
|
|
||
| public function testGetContactImportNotFound(): void | ||
| { | ||
| $importId = 999; | ||
|
|
||
| $this->contact->expects($this->once()) | ||
| ->method('httpGet') | ||
| ->with(AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/contacts/imports/' . $importId) | ||
| ->willReturn( | ||
| new Response(404, ['Content-Type' => 'application/json'], json_encode(['error' => 'Not Found'])) | ||
| ); | ||
|
|
||
| $this->expectException(HttpClientException::class); | ||
| $this->expectExceptionMessage('Errors: Not Found.'); | ||
|
|
||
| $this->contact->getContactImport($importId); | ||
| } | ||
|
|
||
| public function testImportContactsValidationError(): void | ||
| { | ||
| $contacts = [ | ||
| new ImportContact( | ||
| email: 'invalid-email', | ||
| fields: ['first_name' => 'John'], | ||
| listIdsIncluded: [], | ||
| listIdsExcluded: [] | ||
| ), | ||
| ]; | ||
|
|
||
| $expectedResponse = [ | ||
| 'errors' => [ | ||
| [ | ||
| 'email' => 'invalid-email', | ||
| 'errors' => [ | ||
| 'email' => [ | ||
| 'is invalid', | ||
| 'top level domain is too short', | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ]; | ||
|
|
||
| $this->contact->expects($this->once()) | ||
| ->method('httpPost') | ||
| ->with( | ||
| AbstractApi::DEFAULT_HOST . '/api/accounts/' . self::FAKE_ACCOUNT_ID . '/contacts/imports', | ||
| [], | ||
| ['contacts' => array_map(fn(ImportContact $contact) => $contact->toArray(), $contacts)] | ||
| ) | ||
| ->willReturn( | ||
| new Response(422, ['Content-Type' => 'application/json'], json_encode($expectedResponse)) | ||
| ); | ||
|
|
||
| $this->expectException(HttpClientException::class); | ||
| $this->expectExceptionMessage('Errors: email -> invalid-email. errors -> email -> is invalid. top level domain is too short.'); | ||
|
|
||
| $this->contact->importContacts($contacts); | ||
| } | ||
|
|
||
| private function getExpectedContactFields(): array | ||
| { | ||
| return [ | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.