Skip to content

Commit

Permalink
feat: added data residency for eu and global region
Browse files Browse the repository at this point in the history
  • Loading branch information
tiwarishubham635 committed Nov 17, 2023
1 parent 9335dca commit 4c4b958
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
75 changes: 75 additions & 0 deletions examples/dataresidency/setregion.php
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;
}
25 changes: 25 additions & 0 deletions lib/BaseSendGridClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ abstract class BaseSendGridClientInterface
/** @var string SendGrid version */
public $version = self::VERSION;

/** @var allowedRegionsHostMap regions specific hosts */
public $allowedRegionsHostMap = [
"eu" => "https://api.eu.sendgrid.com",
"global" => "https://api.sendgrid.com",
];

/**
* Set up the HTTP Client.
*
Expand Down Expand Up @@ -62,4 +68,23 @@ public function send(Mail $email)
{
return $this->client->mail()->send()->post($email);
}

/*
* Client libraries contain setters for specifying region/edge.
* This allows support global and eu regions only. This set will likely expand in the future.
* Global should be the default
* Global region means the message should be sent through:
* HTTP: api.sendgrid.com
* EU region means the message should be sent through:
* HTTP: api.eu.sendgrid.com
*/
public function setDataResidency($region): void
{
if (array_key_exists($region, $this->allowedRegionsHostMap)) {
$this->client->setHost($this->allowedRegionsHostMap[$region]);
} else {
throw new InvalidArgumentException("region can only be \"eu\" or \"global\"");
}
}

}
72 changes: 72 additions & 0 deletions test/unit/DataResidencyTest.php
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());
}
}

0 comments on commit 4c4b958

Please sign in to comment.