Skip to content

Commit 5aad471

Browse files
CallumEddisfordChris Smith
authored andcommitted
Fixes #6 invalid endpoint issue (#7)
* Fixes #6 invalid endpoint issue * Corrected exception handling. Uses Content-Type header vs Accept * Fixed how Personalization object is built * Fixed sniff violation * Fixes recipients personalisation formatting * Fixed how the ApiClient forms the requests * Fixed test failures
1 parent 443f327 commit 5aad471

File tree

8 files changed

+28
-28
lines changed

8 files changed

+28
-28
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ composer require xedi/sendgrid
4848
use Xedi\SendGrid\SendGrid;
4949

5050
SendGrid::setClient(
51-
SendGrid::getApiClient($api_key);
51+
SendGrid::getApiClient($api_key)
5252
);
5353

5454
($mailable = SendGrid::prepareMail())
5555
->setSender('you@email.com', 'Joe Blogs')
56-
->setSubject('Checkout XEDI\'s SendGrid libray!')
56+
->setSubject('Checkout XEDI\'s SendGrid library!')
5757
->addTextContent('Hey,\n\rI found this great SendGrid library!')
5858
->addHtmlContent('<body>Hey,<br/>I found this great SendGrid library!</body>')
5959
->addRecipient('john.smith@email.com', 'John Smith');

lib/Clients/ApiClient.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(string $api_key, array $options = [])
4141
$this->client = new GuzzleClient(
4242
array_merge(
4343
[
44-
'base_uri' => 'https://api.sendgrid.com/v3',
44+
'base_uri' => 'https://api.sendgrid.com',
4545
'headers' => [
4646
'Authorization' => "Bearer $api_key",
4747
'Content-Type' => 'application/json'
@@ -139,16 +139,14 @@ protected function makeRequest(
139139
$response = $this->client->request(
140140
$method,
141141
$uri,
142-
array_merge(
143-
[
144-
'headers' => $headers
145-
],
146-
$data
147-
)
142+
[
143+
'headers' => $headers,
144+
'json' => $data
145+
]
148146
);
149147

150148
return new HttpResponse(
151-
(string) $response->getBody(),
149+
(string)$response->getBody(),
152150
$response->getStatusCode(),
153151
$response->getHeaders()
154152
);

lib/Clients/Concerns/HandlesExceptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private function handleBadRequestException(ClientException $exception)
7070
{
7171
$response = $exception->getResponse();
7272

73-
if (! in_array('application/json', $response->getHeader('Accept'))) {
73+
if (! in_array('application/json', $response->getHeader('Content-Type'))) {
7474
return new UndecodedClientException($exception);
7575
}
7676

lib/Mail/Concerns/HasRecipients.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,17 @@ public function validateRecipients()
107107
*/
108108
public function buildRecipients(): array
109109
{
110-
return [
110+
return [
111111
'personalizations' => [
112-
'to' => array_map(
113-
function ($recipient) {
114-
return $recipient->toArray();
115-
},
116-
$this->recipients
117-
)
112+
[
113+
"to" => array_map(
114+
function ($recipient) {
115+
return $recipient->toArray();
116+
},
117+
$this->recipients
118+
)
119+
]
118120
]
119-
];
121+
];
120122
}
121123
}

lib/Mail/Mail.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ public function send(Client $client): Response
5555
$this->buildSubject()
5656
);
5757

58-
return $client->post('mail/send', $data);
58+
return $client->post('v3/mail/send', $data);
5959
}
6060
}

tests/Integration/SendEmailTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private function getMockErrorResponse(array $errors)
146146
return new Response(
147147
400,
148148
[
149-
'Accept' => 'application/json',
149+
'Content-Type' => 'application/json',
150150
],
151151
json_encode(
152152
[

tests/Unit/Clients/ApiClient/MakeRequestTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function catchesConnectionExceptions()
4343
($mock_guzzle_client = Mockery::mock(GuzzleClient::class))
4444
->shouldReceive('request')
4545
->once()
46-
->with('GET', 'an/endpoint', [ 'headers' => [] ])
46+
->with('GET', 'an/endpoint', [ 'headers' => [], 'json' => [] ])
4747
->andThrows($mock_exception);
4848

4949
$api_client = (new ReflectionClass(ApiClient::class))
@@ -75,7 +75,7 @@ public function catchesThrowableExceptions()
7575
($mock_guzzle_client = Mockery::mock(GuzzleClient::class))
7676
->shouldReceive('request')
7777
->once()
78-
->with('GET', 'an/endpoint', [ 'headers' => [] ])
78+
->with('GET', 'an/endpoint', [ 'headers' => [], 'json' => [] ])
7979
->andThrows($mock_exception);
8080

8181
$api_client = (new ReflectionClass(ApiClient::class))

tests/Unit/Clients/Concerns/HandlesExceptions/HandleBadRequestExceptionTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function unableToDecodeException()
2727
($mock_response = Mockery::mock(ResponseInterface::class))
2828
->shouldReceive('getHeader')
2929
->once()
30-
->with('Accept')
30+
->with('Content-Type')
3131
->andReturn(['text/plain']);
3232

3333
($mock_exception = Mockery::mock(ClientException::class))
@@ -49,7 +49,7 @@ public function failedToDecodeException()
4949
($mock_response = Mockery::mock(ResponseInterface::class))
5050
->shouldReceive('getHeader')
5151
->once()
52-
->with('Accept')
52+
->with('Content-Type')
5353
->andReturn(['application/json']);
5454

5555
$mock_response->shouldReceive('getBody')
@@ -75,7 +75,7 @@ public function handlesUnknownException()
7575
($mock_response = Mockery::mock(ResponseInterface::class))
7676
->shouldReceive('getHeader')
7777
->once()
78-
->with('Accept')
78+
->with('Content-Type')
7979
->andReturn(['application/json']);
8080

8181
$mock_response->shouldReceive('getBody')
@@ -102,7 +102,7 @@ public function handlesSendGridError($error, $exception_class)
102102
($mock_response = Mockery::mock(ResponseInterface::class))
103103
->shouldReceive('getHeader')
104104
->once()
105-
->with('Accept')
105+
->with('Content-Type')
106106
->andReturn(['application/json']);
107107

108108
$mock_response->shouldReceive('getBody')
@@ -158,7 +158,7 @@ public function handlesMultipleDomainErrors()
158158
($mock_response = Mockery::mock(ResponseInterface::class))
159159
->shouldReceive('getHeader')
160160
->once()
161-
->with('Accept')
161+
->with('Content-Type')
162162
->andReturn(['application/json']);
163163

164164
$errors = array_map(

0 commit comments

Comments
 (0)