Skip to content

Commit 92aecc4

Browse files
committed
add exception and verify success of API calls
1 parent aa2c295 commit 92aecc4

File tree

3 files changed

+180
-15
lines changed

3 files changed

+180
-15
lines changed

README.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,48 @@ MAILJET_APIKEY=YOUR_APIKEY
5454
MAILJET_APISECRET=YOUR_APISECRET
5555
```
5656

57+
## Full configuration
58+
59+
```php
60+
'mailjet' => [
61+
'key' => env('MAILJET_APIKEY'),
62+
'secret' => env('MAILJET_APISECRET'),
63+
'transactionnal' => [
64+
'call' => true,
65+
'options' => [
66+
'url' => 'api.mailjet.com',
67+
'version' => 'v31',
68+
'call' => true,
69+
'secured' => true
70+
]
71+
],
72+
'common' => [
73+
'call' => true,
74+
'options' => [
75+
'url' => 'api.mailjet.com',
76+
'version' => 'v3',
77+
'call' => true,
78+
'secured' => true
79+
]
80+
]
81+
]
82+
```
83+
You can pass settings to [MailjetClient](https://github.com/mailjet/mailjet-apiv3-php#new--version-120-of-the-php-wrapper-).
84+
85+
* `transactionnal`: settings to sendAPI client
86+
* `common`: setting to MailjetClient accessible throught the Facade Mailjet
87+
5788
## Mail driver configuration
5889

59-
In order to use Mailjet as Mail driver, you need to change the mail driver in your `config/mail.php` or your `.env` file to `mailjet`, and make sure you have a valid and authorised from-address configured.
90+
In order to use Mailjet as Mail driver, you need to change the mail driver in your `config/mail.php` or your `.env` file to `MAIL_DRIVER=mailjet`, and make sure you have a valid and authorised from-address configured on your Mailjet account.
6091

6192
For usage, check the [Laravel mail documentation](https://laravel.com/docs/master/mail)
6293

6394
## Usage
6495

6596
To use it, you need to import Mailjet Facade in your file
6697

67-
use Mailjet;
98+
use Mailjet\LaravelMailjet\Facades\Mailjet;
6899

69100

70101
Then, in your code you can use one of the method available in the MailjetServices :
@@ -88,8 +119,11 @@ High level API methods:
88119

89120
For more informations about the filters you can use in each methods, refer to the [Mailjet API documentation](https://dev.mailjet.com/email-api/v3/apikey/)
90121

122+
All method return the data array or throw a MailjetException in case of API error.
123+
124+
You can also get the client with the method `getClient()` and make your own request to Mailjet API.
91125

92126
## ToDo
93127

94-
* Client Call/Options (common api call and transactionnal mail)
95-
* Better \Mailjet\Client injection
128+
* Create Mailjet contract (Laravel 5.4 - DepInject <https://laravel.com/docs/5.4/contracts>)
129+
* New feature: EventAPI (webhook), Campaign, stats, ...

src/Exception/MailjetException.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
namespace Mailjet\LaravelMailjet\Exception;
3+
4+
use \Mailjet\Response;
5+
6+
/**
7+
* Handle Mailjet API errors
8+
*/
9+
class MailjetException extends \Exception
10+
{
11+
/**
12+
* @var int
13+
*/
14+
private $statusCode;
15+
/**
16+
* @var string
17+
*/
18+
private $errorInfo;
19+
/**
20+
* @var string
21+
*/
22+
private $errorMessage;
23+
/**
24+
* @var string
25+
*/
26+
private $errorIdentifier;
27+
/**
28+
* https://dev.mailjet.com/guides/#about-the-mailjet-restful-api
29+
* @param Response $response
30+
* @param \Throwable $previous
31+
*/
32+
public function __construct($statusCode=0, $message=null, Response $response=null, \Throwable $previous=null)
33+
{
34+
// if you pass a Mailjet\Response
35+
if ($response) {
36+
$statusCode = $response->getStatus();
37+
$message = sprintf('%s: %s', $message, $response->getReasonPhrase());
38+
$this->setErrorFromResponse($response);
39+
}
40+
parent::__construct($message, $statusCode, $previous);
41+
}
42+
/**
43+
* Configure MailjetException from Mailjet\Response
44+
* @method setErrorFromResponse
45+
* @param Response $response
46+
*/
47+
private function setErrorFromResponse(Response $response)
48+
{
49+
$this->statusCode = $response->getStatus();
50+
$body = $response->getBody();
51+
if (isset($body['ErrorInfo'])) {
52+
$this->errorInfo = $body['ErrorInfo'];
53+
}
54+
if (isset($body['ErrorMessage'])) {
55+
$this->errorMessage = $body['ErrorMessage'];
56+
}
57+
if (isset($body['ErrorIdentifier'])) {
58+
$this->errorIdentifier = $body['ErrorIdentifier'];
59+
}
60+
}
61+
/**
62+
* @return string
63+
*/
64+
public function getErrorInfo()
65+
{
66+
return $this->errorInfo;
67+
}
68+
/**
69+
* @return string
70+
*/
71+
public function getErrorMessage()
72+
{
73+
return $this->errorMessage;
74+
}
75+
/**
76+
* @return string
77+
*/
78+
public function getErrorIdentifier()
79+
{
80+
return $this->errorIdentifier;
81+
}
82+
}

src/Services/MailjetService.php

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use \Mailjet\Resources;
66
use \Mailjet\Client;
77

8+
use Mailjet\LaravelMailjet\Exception\MailjetException;
9+
810
class MailjetService
911
{
1012
/**
@@ -32,7 +34,11 @@ public function __construct($key, $secret, $call = true, array $settings = [])
3234
*/
3335
public function post($resource, array $args = [], array $options = [])
3436
{
35-
return $this->client->post($resource, $args, $options);
37+
$response = $this->client->post($resource, $args, $options);
38+
if (!$response->success()) {
39+
$this->throwError("MailjetService:post() failed", $response);
40+
}
41+
return $response->getData();
3642
}
3743

3844
/**
@@ -46,7 +52,11 @@ public function post($resource, array $args = [], array $options = [])
4652
*/
4753
public function get($resource, array $args = [], array $options = [])
4854
{
49-
return $this->client->get($resource, $args, $options);
55+
$response = $this->client->get($resource, $args, $options);
56+
if (!$response->success()) {
57+
$this->throwError("MailjetService:get() failed", $response);
58+
}
59+
return $response->getData();
5060
}
5161

5262
/**
@@ -60,7 +70,11 @@ public function get($resource, array $args = [], array $options = [])
6070
*/
6171
public function put($resource, array $args = [], array $options = [])
6272
{
63-
return $this->client->put($resource, $args, $options);
73+
$response = $this->client->put($resource, $args, $options);
74+
if (!$response->success()) {
75+
$this->throwError("MailjetService:put() failed", $response);
76+
}
77+
return $response->getData();
6478
}
6579

6680
/**
@@ -74,7 +88,11 @@ public function put($resource, array $args = [], array $options = [])
7488
*/
7589
public function delete($resource, array $args = [], array $options = [])
7690
{
77-
return $this->client->delete($resource, $args, $options);
91+
$response = $this->client->delete($resource, $args, $options);
92+
if (!$response->success()) {
93+
$this->throwError("MailjetService:delete() failed", $response);
94+
}
95+
return $response->getData();
7896
}
7997

8098
/**
@@ -85,7 +103,10 @@ public function delete($resource, array $args = [], array $options = [])
85103
public function getAllLists($filters)
86104
{
87105
$response = $this->client->get(Resources::$Contactslist, ['filters' => $filters]);
88-
return $response;
106+
if (!$response->success()) {
107+
$this->throwError("MailjetService:getAllLists() failed", $response);
108+
}
109+
return $response->getData();
89110
}
90111

91112
/**
@@ -96,7 +117,10 @@ public function getAllLists($filters)
96117
public function createList($body)
97118
{
98119
$response = $this->client->post(Resources::$Contactslist, ['body' => $body]);
99-
return $response;
120+
if (!$response->success()) {
121+
$this->throwError("MailjetService:createList() failed", $response);
122+
}
123+
return $response->getData();
100124
}
101125

102126
/**
@@ -107,7 +131,10 @@ public function createList($body)
107131
public function getListRecipients($filters)
108132
{
109133
$response = $this->client->get(Resources::$Listrecipient, ['filters' => $filters]);
110-
return $response;
134+
if (!$response->success()) {
135+
$this->throwError("MailjetService:getListRecipients() failed", $response);
136+
}
137+
return $response->getData();
111138
}
112139

113140
/**
@@ -118,7 +145,10 @@ public function getListRecipients($filters)
118145
public function getSingleContact($id)
119146
{
120147
$response = $this->client->get(Resources::$Contact, ['id' => $id]);
121-
return $response;
148+
if (!$response->success()) {
149+
$this->throwError("MailjetService:getSingleContact() failed", $response);
150+
}
151+
return $response->getData();
122152
}
123153

124154
/**
@@ -129,7 +159,10 @@ public function getSingleContact($id)
129159
public function createContact($body)
130160
{
131161
$response = $this->client->post(Resources::$Contact, ['body' => $body]);
132-
return $response;
162+
if (!$response->success()) {
163+
$this->throwError("MailjetService:createContact() failed", $response);
164+
}
165+
return $response->getData();
133166
}
134167

135168
/**
@@ -140,7 +173,10 @@ public function createContact($body)
140173
public function createListRecipient($body)
141174
{
142175
$response = $this->client->post(Resources::$Listrecipient, ['body' => $body]);
143-
return $response;
176+
if (!$response->success()) {
177+
$this->throwError("MailjetService:createListRecipient() failed", $response);
178+
}
179+
return $response->getData();
144180
}
145181

146182
/**
@@ -152,7 +188,10 @@ public function createListRecipient($body)
152188
public function editListrecipient($id, $body)
153189
{
154190
$response = $this->client->put(Resources::$Listrecipient, ['id'=>$id, 'body' => $body]);
155-
return $response;
191+
if (!$response->success()) {
192+
$this->throwError("MailjetService:editListrecipient() failed", $response);
193+
}
194+
return $response->getData();
156195
}
157196

158197
/**
@@ -163,4 +202,14 @@ public function getClient()
163202
{
164203
return $this->client;
165204
}
205+
206+
/**
207+
* Helper to throw error
208+
* @param string $title
209+
* @param Response $response
210+
*/
211+
private function throwError($title, Response $response)
212+
{
213+
throw new MailjetException(0, $title, $response);
214+
}
166215
}

0 commit comments

Comments
 (0)