-
Notifications
You must be signed in to change notification settings - Fork 626
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added data residency for eu and global region
- Loading branch information
1 parent
9335dca
commit 4c4b958
Showing
3 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,75 @@ | ||
<?php | ||
// index.php | ||
require_once __DIR__ . '/../../sendgrid-php.php'; | ||
|
||
use SendGrid\Mail\Mail; | ||
use SendGrid\Mail\Personalization; | ||
use SendGrid\Mail\To; | ||
$exceptionMessage = 'Caught exception: '; | ||
|
||
//////////////////////////////////////////////////// | ||
// Set data residency to navigate to a region/edge. # | ||
// sending to global data residency | ||
|
||
$email = buildHelloEmail(); | ||
$sendgrid = buildSendgridObject("global"); | ||
|
||
try { | ||
$response = $sendgrid->client->mail()->send()->post($email); | ||
print $response->statusCode() . "\n"; | ||
print_r($response->headers()); | ||
print $response->body() . "\n"; | ||
} catch (Exception $e) { | ||
echo $exceptionMessage, $e->getMessage(), "\n"; | ||
} | ||
|
||
//////////////////////////////////////////////////// | ||
// sending to EU data residency | ||
|
||
$sendgrid_eu = buildSendgridObject("eu"); | ||
|
||
try { | ||
$response = $sendgrid_eu->client->mail()->send()->post($email); | ||
print $response->statusCode() . "\n"; | ||
print_r($response->headers()); | ||
print $response->body() . "\n"; | ||
} catch (Exception $e) { | ||
echo $exceptionMessage, $e->getMessage(), "\n"; | ||
} | ||
|
||
//////////////////////////////////////////////////// | ||
// not configuring any region defaults to global | ||
$sendgrid_default = new \SendGrid(getenv('SENDGRID_API_KEY')); | ||
try { | ||
$response = $sendgrid_default->client->mail()->send()->post($email); | ||
print $response->statusCode() . "\n"; | ||
print_r($response->headers()); | ||
print $response->body() . "\n"; | ||
} catch (Exception $e) { | ||
echo $exceptionMessage, $e->getMessage(), "\n"; | ||
} | ||
|
||
function buildHelloEmail(): Mail | ||
{ | ||
$email = new Mail(); | ||
$email->setFrom("[email protected]", "Shb"); | ||
$email->setSubject("Sending with Twilio SendGrid is Fun"); | ||
$email->addTo("[email protected]", "Shb"); | ||
$email->addContent("text/plain", "and easy to do anywhere, even with PHP"); | ||
$email->addContent( | ||
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>" | ||
); | ||
$objPersonalization = new Personalization(); | ||
|
||
$objTo = new To('[email protected]', 'foo bar'); | ||
$objPersonalization->addTo($objTo); | ||
$email->addPersonalization($objPersonalization); | ||
return $email; | ||
} | ||
|
||
function buildSendgridObject($region): SendGrid | ||
{ | ||
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')); | ||
$sendgrid->setDataResidency($region); | ||
return $sendgrid; | ||
} |
This file contains 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 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,72 @@ | ||
<?php | ||
/** | ||
* This file tests email address encoding. | ||
*/ | ||
|
||
namespace SendGrid\Tests\Unit; | ||
|
||
use InvalidArgumentException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
|
||
/** | ||
* This class tests Data residency | ||
* | ||
* @package SendGrid\Tests | ||
*/ | ||
class DataResidencyTest extends TestCase | ||
{ | ||
public function testSetResidencyEu() | ||
{ | ||
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')); | ||
$sendgrid->setDataResidency("eu"); | ||
self::assertEquals("https://api.eu.sendgrid.com", $sendgrid->client->getHost()); | ||
} | ||
|
||
public function testSetResidencyGlobal() | ||
{ | ||
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')); | ||
$sendgrid->setDataResidency("global"); | ||
self::assertEquals("https://api.sendgrid.com", $sendgrid->client->getHost()); | ||
} | ||
|
||
public function testSetResidencyOverrideHost() | ||
{ | ||
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')); | ||
$sendgrid->client->setHost("https://test.api.com"); | ||
$sendgrid->setDataResidency("eu"); | ||
self::assertEquals("https://api.eu.sendgrid.com", $sendgrid->client->getHost()); | ||
} | ||
|
||
public function testSetResidencyOverrideDataResidency() | ||
{ | ||
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')); | ||
$sendgrid->setDataResidency("eu"); | ||
$sendgrid->client->setHost("https://test.api.com"); | ||
self::assertEquals("https://test.api.com", $sendgrid->client->getHost()); | ||
} | ||
|
||
public function testSetResidencyIncorrectRegion() | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage("region can only be \"eu\" or \"global\""); | ||
|
||
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')); | ||
$sendgrid->setDataResidency("foo"); | ||
} | ||
|
||
public function testSetResidencyNullRegion() | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage("region can only be \"eu\" or \"global\""); | ||
|
||
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')); | ||
$sendgrid->setDataResidency(""); | ||
} | ||
|
||
public function testSetResidencyDefaultRegion() | ||
{ | ||
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')); | ||
self::assertEquals("https://api.sendgrid.com", $sendgrid->client->getHost()); | ||
} | ||
} |