From 397d19f3ecd9da10fc4768cbef789a7172324046 Mon Sep 17 00:00:00 2001 From: 77web Date: Mon, 13 Nov 2023 10:42:10 +0900 Subject: [PATCH 1/6] fixed path to preserve base_uri path --- src/Client.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Client.php b/src/Client.php index 78aab54..8b41dc2 100644 --- a/src/Client.php +++ b/src/Client.php @@ -30,7 +30,7 @@ public function createContactList( CreateContactListRequest $request, ): CreateContactListResponse { return $this->requester->post( - '/contactdb/lists', + 'contactdb/lists', $request, CreateContactListResponse::class, ); @@ -43,7 +43,7 @@ public function createContactList( public function createRecipients(CreateRecipientsRequest $request): CreateRecipientsResponse { return $this->requester->post( - '/contactdb/recipients', + 'contactdb/recipients', $request, CreateRecipientsResponse::class, ); @@ -58,7 +58,7 @@ public function addMultipleRecipientsToContactList( AddMultipleRecipientsRequest $request, ): AddMultipleRecipientsResponse { return $this->requester->post( - sprintf('/contactdb/lists/%d/recipients', $listId), + sprintf('contactdb/lists/%d/recipients', $listId), $request, AddMultipleRecipientsResponse::class, ); @@ -71,7 +71,7 @@ public function addMultipleRecipientsToContactList( public function createCampaign(CreateCampaignRequest $request): CreateCampaignResponse { return $this->requester->post( - '/campaigns', + 'campaigns', $request, CreateCampaignResponse::class, ); @@ -84,7 +84,7 @@ public function createCampaign(CreateCampaignRequest $request): CreateCampaignRe public function sendCampaign(int $campaignId, SendCampaignRequest $request): SendCampaignResponse { return $this->requester->post( - sprintf('/campaigns/%d/schedules/now', $campaignId), + sprintf('campaigns/%d/schedules/now', $campaignId), $request, SendCampaignResponse::class, ); From 4988f6f4ea8bb101ac411bca2dcbada3994ceb57 Mon Sep 17 00:00:00 2001 From: 77web Date: Mon, 13 Nov 2023 10:42:59 +0900 Subject: [PATCH 2/6] fixed response json body to accept empty response --- src/SendgridApiRequester.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/SendgridApiRequester.php b/src/SendgridApiRequester.php index 45bd96b..68352c4 100644 --- a/src/SendgridApiRequester.php +++ b/src/SendgridApiRequester.php @@ -22,7 +22,7 @@ public function __construct( SerializerInterface|null $serializer = null, ) { $this->httpClient = $httpClient ?? new HttpClient([ - 'base_uri' => 'https://api.sendgrid.com/v3', + 'base_uri' => 'https://api.sendgrid.com/v3/', 'headers' => [ 'Authorization' => sprintf('Bearer %s', $apiKey), ], @@ -110,7 +110,12 @@ private function mutate(string $method, string $path, object $request, string $r throw new SendgridApiServerException($e->getMessage(), code: $e->getCode(), previous: $e); } - $result = $this->serializer->deserialize($response->getBody()->getContents(), $responseClass, 'json'); + $responseJson = $response->getBody()->getContents(); + if ($responseJson === '') { + $responseJson = '{}'; + } + + $result = $this->serializer->deserialize($responseJson, $responseClass, 'json'); \assert(\is_object($result) && $result instanceof $responseClass); return $result; From 978cc18f76e13b64445c084f96eeda70478982da Mon Sep 17 00:00:00 2001 From: 77web Date: Mon, 13 Nov 2023 11:47:14 +0900 Subject: [PATCH 3/6] disabled serializing null field --- src/SerializerFactory.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/SerializerFactory.php b/src/SerializerFactory.php index d7c764b..3da4cad 100644 --- a/src/SerializerFactory.php +++ b/src/SerializerFactory.php @@ -8,6 +8,7 @@ use Symfony\Component\PropertyInfo\PropertyInfoExtractor; use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; +use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer; use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer; use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer; use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; @@ -34,6 +35,9 @@ public function create(): SerializerInterface listExtractors: [$reflectionExtractor], typeExtractors: [$reflectionExtractor], ), + defaultContext: [ + AbstractObjectNormalizer::SKIP_NULL_VALUES => true, + ] ), ], [ From 2e9b7924dadf7e2811e704d9d4cdd2fd6b174904 Mon Sep 17 00:00:00 2001 From: 77web Date: Mon, 13 Nov 2023 11:49:25 +0900 Subject: [PATCH 4/6] made CreateCampaignRequest::$customUnsubscribeUrl nullable --- src/Campaign/CreateCampaignRequest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Campaign/CreateCampaignRequest.php b/src/Campaign/CreateCampaignRequest.php index d8ec8d9..5e22879 100644 --- a/src/Campaign/CreateCampaignRequest.php +++ b/src/Campaign/CreateCampaignRequest.php @@ -18,7 +18,7 @@ public function __construct( public int $suppressionGroupId, public string|null $htmlContent = null, public string|null $plainContent = null, - public string $customUnsubscribeUrl = '', + public string|null $customUnsubscribeUrl = null, public array $listIds = [], public array $segmentIds = [], public array $categories = [], From 93600c92dc60e39de605e0ed93c2dd419080b360 Mon Sep 17 00:00:00 2001 From: 77web Date: Mon, 13 Nov 2023 12:38:55 +0900 Subject: [PATCH 5/6] set listIds, segmentIds, categories nullable not to include in request body if they are empty --- src/Campaign/CreateCampaignRequest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Campaign/CreateCampaignRequest.php b/src/Campaign/CreateCampaignRequest.php index 5e22879..ba89e4a 100644 --- a/src/Campaign/CreateCampaignRequest.php +++ b/src/Campaign/CreateCampaignRequest.php @@ -7,9 +7,9 @@ readonly class CreateCampaignRequest { /** - * @param array $listIds - * @param array $segmentIds - * @param array $categories + * @param null|array $listIds + * @param null|array $segmentIds + * @param null|array $categories */ public function __construct( public string $title, @@ -19,9 +19,9 @@ public function __construct( public string|null $htmlContent = null, public string|null $plainContent = null, public string|null $customUnsubscribeUrl = null, - public array $listIds = [], - public array $segmentIds = [], - public array $categories = [], + public array|null $listIds = null, + public array|null $segmentIds = null, + public array|null $categories = null, public string|null $ipPool = null, ) { } From c157b1d39dd95c0257b04eab1c0b51905b929cfc Mon Sep 17 00:00:00 2001 From: 77web Date: Mon, 13 Nov 2023 12:41:59 +0900 Subject: [PATCH 6/6] comopser fix:all --- src/SerializerFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SerializerFactory.php b/src/SerializerFactory.php index 3da4cad..d34ea35 100644 --- a/src/SerializerFactory.php +++ b/src/SerializerFactory.php @@ -37,7 +37,7 @@ public function create(): SerializerInterface ), defaultContext: [ AbstractObjectNormalizer::SKIP_NULL_VALUES => true, - ] + ], ), ], [