Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat uniform response #68

Merged
merged 20 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
vendor
Makefile
.envrc
.env
*.p8

### Linux ###
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ $messaging->send($message);
- [x] [Twilio](https://www.twilio.com/)
- [x] [Twilio Notify](https://www.twilio.com/notify)
- [x] [Telesign](https://www.telesign.com/)
- [x] [TextMagic](https://www.textmagic.com/)
- [x] [Textmagic](https://www.textmagic.com/)
- [x] [Msg91](https://msg91.com/)
- [x] [Vonage](https://www.vonage.com/)
- [x] [Plivo](https://www.plivo.com/)
Expand Down
171 changes: 86 additions & 85 deletions composer.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/sms-pricing.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ _In most providers when you're sending SMS to US numbers you must own a US phone
| Clickatell | 0.0075 | 75 $ |
| Twilio | 0.0079 | 79 $ |
| Sinch | 0.0120 | 120 $ |
| TextMagic | 0.0400 | 400 $ |
| Textmagic | 0.0400 | 400 $ |
| Telnyx | 0.0440 | 440 $ |
| Seven | 0.0810 | 810 $ |

Expand All @@ -45,7 +45,7 @@ _In most providers when you're sending SMS to US numbers you must own a US phone
| Seven | 0.0810 | 810 $ |
| Infobip | n/a | |
| Sinch | n/a | |
| TextMagic | n/a | |
| Textmagic | n/a | |

## Israel

Expand All @@ -61,4 +61,4 @@ _In most providers when you're sending SMS to US numbers you must own a US phone
| Twilio | 0.112 | 1120 $ |
| Clickatell | 0.13144 | 1314.4 $ |
| Sinch | n/a | |
| TextMagic | n/a | |
| Textmagic | n/a | |
20 changes: 6 additions & 14 deletions src/Utopia/Messaging/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ abstract public function getMaxMessagesPerRequest(): int;
*
* @param Message $message The message to send.
* @return string The response body.
*
* @throws \Exception
*/
public function send(Message $message): string
{
Expand All @@ -54,16 +52,14 @@ public function send(Message $message): string
* @param string $url The URL to send the request to.
* @param array<string> $headers An array of headers to send with the request.
* @param string|null $body The body of the request.
* @return string The response body.
*
* @throws \Exception If the request fails.
* @return array<string, mixed> The response body.
*/
protected function request(
string $method,
string $url,
array $headers = [],
string $body = null,
): string {
): array {
$ch = \curl_init();

if (! \is_null($body)) {
Expand All @@ -79,15 +75,11 @@ protected function request(

$response = \curl_exec($ch);

if (\curl_errno($ch)) {
throw new \Exception('Error: '.\curl_error($ch));
}
if (\curl_getinfo($ch, CURLINFO_HTTP_CODE) >= 400) {
throw new \Exception($response);
}

\curl_close($ch);

return $response;
return [
'response' => \json_decode($response, true),
abnegate marked this conversation as resolved.
Show resolved Hide resolved
'statusCode' => \curl_getinfo($ch, CURLINFO_HTTP_CODE),
abnegate marked this conversation as resolved.
Show resolved Hide resolved
];
}
}
20 changes: 16 additions & 4 deletions src/Utopia/Messaging/Adapter/Chat/Discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Utopia\Messaging\Adapter;
use Utopia\Messaging\Messages\Discord as DiscordMessage;
use Utopia\Messaging\Response;

class Discord extends Adapter
{
Expand Down Expand Up @@ -37,9 +38,6 @@ public function getMaxMessagesPerRequest(): int
return 1;
}

/**
* @throws \Exception
*/
protected function process(DiscordMessage $message): string
{
$query = [];
Expand All @@ -59,7 +57,8 @@ protected function process(DiscordMessage $message): string
$queryString .= $key.'='.$value;
}

return $this->request(
$response = new Response($this->getType());
$result = $this->request(
method: 'POST',
url: "https://discord.com/api/webhooks/{$this->webhookId}/{$this->webhookToken}{$queryString}",
headers: [
Expand All @@ -78,5 +77,18 @@ protected function process(DiscordMessage $message): string
'thread_name' => $message->getThreadName(),
]),
);

$statusCode = $result['statusCode'];

if ($statusCode >= 200 && $statusCode < 300) {
$response->setDeliveredTo(1);
$response->addToDetails($message->getThreadId() ?? '');
} elseif ($statusCode >= 400 && $statusCode < 500) {
$response->addToDetails($message->getThreadId() ?? '', 'Bad Request.');
fanatic75 marked this conversation as resolved.
Show resolved Hide resolved
} else {
$response->addToDetails($message->getThreadId() ?? '', 'Unknown Error.');
}

return \json_encode($response->toArray());
}
}
1 change: 0 additions & 1 deletion src/Utopia/Messaging/Adapter/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Utopia\Messaging\Adapter;

use Utopia\Messaging\Adapter;
use Utopia\Messaging\Message;
use Utopia\Messaging\Messages\Email as EmailMessage;

abstract class Email extends Adapter
Expand Down
26 changes: 20 additions & 6 deletions src/Utopia/Messaging/Adapter/Email/Mailgun.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace Utopia\Messaging\Adapter\Email;

use Utopia\Messaging\Adapter\Email as EmailAdapter;
use Utopia\Messaging\Messages\Email;
use Utopia\Messaging\Messages\Email as EmailMessage;
use Utopia\Messaging\Response;

class Mailgun extends EmailAdapter
{
Expand Down Expand Up @@ -36,17 +37,17 @@ public function getMaxMessagesPerRequest(): int

/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function process(Email $message): string
protected function process(EmailMessage $message): string
{
$usDomain = 'api.mailgun.net';
$euDomain = 'api.eu.mailgun.net';

$domain = $this->isEU ? $euDomain : $usDomain;

$response = $this->request(
$response = new Response($this->getType());

$result = $this->request(
method: 'POST',
url: "https://$domain/v3/{$this->domain}/messages",
headers: [
Expand All @@ -61,6 +62,19 @@ protected function process(Email $message): string
]),
);

return $response;
$statusCode = $result['statusCode'];

if ($statusCode >= 200 && $statusCode < 300) {
$response->setDeliveredTo(\count($message->getTo()));
foreach ($message->getTo() as $to) {
$response->addToDetails($to);
abnegate marked this conversation as resolved.
Show resolved Hide resolved
}
} elseif ($statusCode >= 400 && $statusCode < 500) {
foreach ($message->getTo() as $to) {
$response->addToDetails($to, $result['response']['message']);
}
}

return \json_encode($response->toArray());
}
}
19 changes: 11 additions & 8 deletions src/Utopia/Messaging/Adapter/Email/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Utopia\Messaging\Adapter\Email;

use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use Utopia\Messaging\Adapter\Email as EmailAdapter;
use Utopia\Messaging\Messages\Email;
use Utopia\Messaging\Messages\Email as EmailMessage;
use Utopia\Messaging\Response;

class Mock extends EmailAdapter
{
Expand All @@ -22,12 +22,10 @@ public function getMaxMessagesPerRequest(): int

/**
* {@inheritdoc}
*
* @throws Exception
* @throws \Exception
*/
protected function process(Email $message): string
protected function process(EmailMessage $message): string
{
$response = new Response($this->getType());
$mail = new PHPMailer();
$mail->isSMTP();
$mail->XMailer = 'Utopia Mailer';
Expand All @@ -51,9 +49,14 @@ protected function process(Email $message): string
}

if (! $mail->send()) {
throw new \Exception($mail->ErrorInfo);
$response->addToDetails('', $mail->ErrorInfo);
fanatic75 marked this conversation as resolved.
Show resolved Hide resolved
} else {
$response->setDeliveredTo(\count($message->getTo()));
foreach ($message->getTo() as $to) {
$response->addToDetails($to);
}
}

return \json_encode($message);
return \json_encode($response->toArray());
}
}
26 changes: 20 additions & 6 deletions src/Utopia/Messaging/Adapter/Email/Sendgrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace Utopia\Messaging\Adapter\Email;

use Utopia\Messaging\Adapter\Email as EmailAdapter;
use Utopia\Messaging\Messages\Email;
use Utopia\Messaging\Messages\Email as EmailMessage;
use Utopia\Messaging\Response;

class Sendgrid extends EmailAdapter
{
Expand Down Expand Up @@ -33,13 +34,11 @@ public function getMaxMessagesPerRequest(): int

/**
* {@inheritdoc}
*
*
* @throws \Exception
*/
protected function process(Email $message): string
protected function process(EmailMessage $message): string
{
return $this->request(
$response = new Response($this->getType());
$result = $this->request(
method: 'POST',
url: 'https://api.sendgrid.com/v3/mail/send',
headers: [
Expand Down Expand Up @@ -67,5 +66,20 @@ protected function process(Email $message): string
],
]),
);

$statusCode = $result['statusCode'];

if ($statusCode === 202) {
$response->setDeliveredTo(\count($message->getTo()));
foreach ($message->getTo() as $recipient) {
$response->addToDetails($recipient);
}
} else {
foreach ($message->getTo() as $recipient) {
$response->addToDetails($recipient, $result['response']['errors'][0]['message']);
}
}

return \json_encode($response->toArray());
}
}
1 change: 0 additions & 1 deletion src/Utopia/Messaging/Adapter/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Utopia\Messaging\Adapter;

use Utopia\Messaging\Adapter;
use Utopia\Messaging\Message;
use Utopia\Messaging\Messages\Push as PushMessage;

abstract class Push extends Adapter
Expand Down
Loading
Loading