forked from utopia-php/messaging
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdapter.php
More file actions
87 lines (73 loc) · 2.18 KB
/
Adapter.php
File metadata and controls
87 lines (73 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace Utopia\Messaging;
abstract class Adapter
{
/**
* Get the name of the adapter.
*
* @return string
*/
abstract public function getName(): string;
/**
* Get the type of the adapter.
*
* @return string
*/
abstract public function getType(): string;
/**
* Get the type of the message the adapter can send.
*
* @return string
*/
abstract public function getMessageType(): string;
/**
* Get the maximum number of messages that can be sent in a single request.
*
* @return int
*/
abstract public function getMaxMessagesPerRequest(): int;
/**
* Send a message.
*
* @param Message $message The message to send.
* @return string The response body.
*/
abstract public function send(Message $message): string;
/**
* Send an HTTP request.
*
* @param string $method The HTTP method to use.
* @param string $url The URL to send the request to.
* @param array $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.
*/
protected function request(
string $method,
string $url,
array $headers = [],
mixed $body = null,
): string {
$headers[] = 'Content-length: '.\strlen($body);
$ch = \curl_init();
\curl_setopt($ch, CURLOPT_URL, $url);
\curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
\curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
\curl_setopt($ch, CURLOPT_USERAGENT, "Appwrite {$this->getName()} Message Sender");
if (! is_null($body)) {
\curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
$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;
}
}