Skip to content

Commit

Permalink
Merge pull request #3 from 77web/fix/actual-api-access
Browse files Browse the repository at this point in the history
Fixed for actual api access
  • Loading branch information
77web authored Nov 13, 2023
2 parents d7358ac + c157b1d commit 92769fd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
14 changes: 7 additions & 7 deletions src/Campaign/CreateCampaignRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
readonly class CreateCampaignRequest
{
/**
* @param array<int> $listIds
* @param array<int> $segmentIds
* @param array<string> $categories
* @param null|array<int> $listIds
* @param null|array<int> $segmentIds
* @param null|array<string> $categories
*/
public function __construct(
public string $title,
Expand All @@ -18,10 +18,10 @@ public function __construct(
public int $suppressionGroupId,
public string|null $htmlContent = null,
public string|null $plainContent = null,
public string $customUnsubscribeUrl = '',
public array $listIds = [],
public array $segmentIds = [],
public array $categories = [],
public string|null $customUnsubscribeUrl = null,
public array|null $listIds = null,
public array|null $segmentIds = null,
public array|null $categories = null,
public string|null $ipPool = null,
) {
}
Expand Down
10 changes: 5 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function createContactList(
CreateContactListRequest $request,
): CreateContactListResponse {
return $this->requester->post(
'/contactdb/lists',
'contactdb/lists',
$request,
CreateContactListResponse::class,
);
Expand All @@ -43,7 +43,7 @@ public function createContactList(
public function createRecipients(CreateRecipientsRequest $request): CreateRecipientsResponse
{
return $this->requester->post(
'/contactdb/recipients',
'contactdb/recipients',
$request,
CreateRecipientsResponse::class,
);
Expand All @@ -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,
);
Expand All @@ -71,7 +71,7 @@ public function addMultipleRecipientsToContactList(
public function createCampaign(CreateCampaignRequest $request): CreateCampaignResponse
{
return $this->requester->post(
'/campaigns',
'campaigns',
$request,
CreateCampaignResponse::class,
);
Expand All @@ -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,
);
Expand Down
9 changes: 7 additions & 2 deletions src/SendgridApiRequester.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
],
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/SerializerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,6 +35,9 @@ public function create(): SerializerInterface
listExtractors: [$reflectionExtractor],
typeExtractors: [$reflectionExtractor],
),
defaultContext: [
AbstractObjectNormalizer::SKIP_NULL_VALUES => true,
],
),
],
[
Expand Down

0 comments on commit 92769fd

Please sign in to comment.