diff --git a/README.md b/README.md index 7615644..7728bcb 100644 --- a/README.md +++ b/README.md @@ -1 +1,27 @@ # simple php client for sendgrid marketing campaign apis + +## installation + +```shell +composer require linkage/sendgrid-marketing-campaign-api-client +``` + +## usage + +```php + +$apiKey = 'get your api key from sendgrid admin screen'; + +$sendgridClient = new \Linkage\SendgridMarketingCampaignApiClient\Client( + new \Linkage\SendgridMarketingCampaignApiClient\SendgridApiRequester($apiKey), +); +try { + $sendgridClient->createContactList( + new \Linkage\SendgridMarketingCampaignApiClient\ContactList\CreateContactListRequest('my new contact list'), + ); +} catch (\Linkage\SendgridMarketingCampaignApiClient\SendgridApiClientException $e) { + // handle client error +} catch (\Linkage\SendgridMarketingCampaignApiClient\SendgridApiServerException $e) { + // handle server error +} +``` diff --git a/src/Client.php b/src/Client.php index 990b169..78aab54 100644 --- a/src/Client.php +++ b/src/Client.php @@ -18,7 +18,7 @@ readonly class Client implements ClientInterface { public function __construct( - private HttpRequester $requester, + private SendgridApiRequester $requester, ) { } diff --git a/src/HttpRequester.php b/src/SendgridApiRequester.php similarity index 62% rename from src/HttpRequester.php rename to src/SendgridApiRequester.php index 8705ac7..45bd96b 100644 --- a/src/HttpRequester.php +++ b/src/SendgridApiRequester.php @@ -4,23 +4,58 @@ namespace Linkage\SendgridMarketingCampaignApiClient; -use GuzzleHttp\ClientInterface as HttpClient; +use GuzzleHttp\Client as HttpClient; +use GuzzleHttp\ClientInterface as HttpClientInterface; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\ServerException; use Symfony\Component\Serializer\SerializerInterface; -readonly class HttpRequester +readonly class SendgridApiRequester { + private HttpClientInterface $httpClient; private SerializerInterface $serializer; public function __construct( - private HttpClient $httpClient, + string $apiKey, + HttpClientInterface|null $httpClient = null, SerializerInterface|null $serializer = null, ) { + $this->httpClient = $httpClient ?? new HttpClient([ + 'base_uri' => 'https://api.sendgrid.com/v3', + 'headers' => [ + 'Authorization' => sprintf('Bearer %s', $apiKey), + ], + ]); $this->serializer = $serializer ?? (new SerializerFactory())->create(); } + /** + * @template T of object + * + * @param class-string $responseClass + * + * @return T + * + * @throws SendgridApiClientException + * @throws SendgridApiServerException + */ + public function get(string $path, string $responseClass) + { + try { + $response = $this->httpClient->request('GET', $path); + } catch (ClientException $e) { + throw new SendgridApiClientException($e->getResponse()->getBody()->getContents(), previous: $e); + } catch (ServerException|GuzzleException $e) { + throw new SendgridApiServerException($e->getMessage(), code: $e->getCode(), previous: $e); + } + + $result = $this->serializer->deserialize($response->getBody()->getContents(), $responseClass, 'json'); + \assert(\is_object($result) && $result instanceof $responseClass); + + return $result; + } + /** * @template T of object * diff --git a/tests/HttpRequesterTest.php b/tests/SendgridApiRequesterTest.php similarity index 92% rename from tests/HttpRequesterTest.php rename to tests/SendgridApiRequesterTest.php index f99e498..7f5c1a5 100644 --- a/tests/HttpRequesterTest.php +++ b/tests/SendgridApiRequesterTest.php @@ -11,13 +11,13 @@ use GuzzleHttp\Psr7\Response; use Linkage\SendgridMarketingCampaignApiClient\ContactList\CreateContactListRequest; use Linkage\SendgridMarketingCampaignApiClient\ContactList\CreateContactListResponse; -use Linkage\SendgridMarketingCampaignApiClient\HttpRequester; use Linkage\SendgridMarketingCampaignApiClient\SendgridApiClientException; +use Linkage\SendgridMarketingCampaignApiClient\SendgridApiRequester; use Linkage\SendgridMarketingCampaignApiClient\SendgridApiServerException; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -class HttpRequesterTest extends TestCase +class SendgridApiRequesterTest extends TestCase { private MockObject&ClientInterface $guzzleClientMock; @@ -81,9 +81,10 @@ public function testPostServerError(): void ); } - private function getSUT(): HttpRequester + private function getSUT(): SendgridApiRequester { - return new HttpRequester( + return new SendgridApiRequester( + 'test', $this->guzzleClientMock, ); }