1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace TgBotApi\BotApiBase; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Client\ClientInterface; |
8
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
9
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
10
|
|
|
use TgBotApi\BotApiBase\Type\InputFileType; |
11
|
|
|
|
12
|
|
|
class ApiClient implements ApiClientInterface |
13
|
|
|
{ |
14
|
|
|
private $client; |
15
|
|
|
private $botKey; |
16
|
|
|
private $endPoint; |
17
|
|
|
private $streamFactory; |
18
|
|
|
private $requestFactory; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* ApiApiClient constructor. |
22
|
|
|
* |
23
|
|
|
* @param RequestFactoryInterface $requestFactory |
24
|
|
|
* @param StreamFactoryInterface $streamFactory |
25
|
|
|
* @param ClientInterface $client |
26
|
|
|
*/ |
27
|
1 |
|
public function __construct( |
28
|
|
|
RequestFactoryInterface $requestFactory, |
29
|
|
|
StreamFactoryInterface $streamFactory, |
30
|
|
|
ClientInterface $client |
31
|
|
|
) { |
32
|
1 |
|
$this->streamFactory = $streamFactory; |
33
|
1 |
|
$this->requestFactory = $requestFactory; |
34
|
1 |
|
$this->client = $client; |
35
|
1 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $method |
39
|
|
|
* @param BotApiRequestInterface $apiRequest |
40
|
|
|
* |
41
|
|
|
* @throws \Psr\Http\Client\ClientExceptionInterface |
42
|
|
|
* |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
1 |
|
public function send(string $method, BotApiRequestInterface $apiRequest) |
46
|
|
|
{ |
47
|
1 |
|
$request = $this->requestFactory->createRequest('POST', $this->generateUri($method)); |
48
|
|
|
|
49
|
1 |
|
$boundary = \uniqid('', true); |
50
|
|
|
|
51
|
1 |
|
$stream = $this->streamFactory->createStream($this->createStreamBody($boundary, $apiRequest)); |
52
|
|
|
|
53
|
1 |
|
$response = $this->client->sendRequest($request |
54
|
1 |
|
->withHeader('Content-Type', 'multipart/form-data; boundary="' . $boundary . '"') |
55
|
1 |
|
->withBody($stream)); |
56
|
|
|
|
57
|
1 |
|
$content = $response->getBody()->getContents(); |
58
|
|
|
|
59
|
1 |
|
return \json_decode($content); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $botKey |
64
|
|
|
* |
65
|
|
|
* @return ApiClientInterface |
66
|
|
|
*/ |
67
|
1 |
|
public function setBotKey(string $botKey): ApiClientInterface |
68
|
|
|
{ |
69
|
1 |
|
$this->botKey = $botKey; |
70
|
|
|
|
71
|
1 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $endPoint |
76
|
|
|
* |
77
|
|
|
* @return ApiClientInterface |
78
|
|
|
*/ |
79
|
1 |
|
public function setEndpoint(string $endPoint): ApiClientInterface |
80
|
|
|
{ |
81
|
1 |
|
$this->endPoint = $endPoint; |
82
|
|
|
|
83
|
1 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $method |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
1 |
|
protected function generateUri(string $method): string |
92
|
|
|
{ |
93
|
1 |
|
return $this->endPoint . '/bot' . $this->botKey . '/' . $method; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param mixed $boundary |
98
|
|
|
* @param BotApiRequestInterface $request |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
1 |
|
protected function createStreamBody($boundary, BotApiRequestInterface $request): string |
103
|
|
|
{ |
104
|
1 |
|
$stream = ''; |
105
|
1 |
|
foreach ($request->getData() as $name => $value) { |
106
|
1 |
|
$stream .= $this->createDataStream($boundary, $name, $value); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
foreach ($request->getFiles() as $name => $file) { |
110
|
1 |
|
$stream .= $this->createFileStream($boundary, $name, $file); |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
if (\strlen($stream)) { |
114
|
1 |
|
$stream .= "--$boundary--\r\n"; |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
return $stream; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param $boundary |
122
|
|
|
* @param $name |
123
|
|
|
* @param InputFileType $file |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
1 |
|
protected function createFileStream($boundary, $name, InputFileType $file): string |
128
|
|
|
{ |
129
|
1 |
|
$headers = ''; |
130
|
1 |
|
$headers .= \sprintf( |
131
|
1 |
|
"Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n", |
132
|
1 |
|
$name, |
133
|
1 |
|
$file->getBasename() |
134
|
|
|
); |
135
|
1 |
|
$headers .= \sprintf("Content-Length: %s\r\n", (string) $file->getSize()); |
136
|
1 |
|
$headers .= \sprintf("Content-Type: %s\r\n", \mime_content_type($file->getRealPath())); |
137
|
|
|
|
138
|
1 |
|
$streams = ''; |
139
|
1 |
|
$streams .= "--$boundary\r\n$headers\r\n"; |
140
|
1 |
|
$streams .= \file_get_contents($file->getRealPath()); |
141
|
1 |
|
$streams .= "\r\n"; |
142
|
|
|
|
143
|
1 |
|
return $streams; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param $boundary |
148
|
|
|
* @param $name |
149
|
|
|
* @param $value |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
1 |
|
protected function createDataStream($boundary, $name, $value): string |
154
|
|
|
{ |
155
|
1 |
|
$headers = ''; |
156
|
1 |
|
$headers .= \sprintf("Content-Disposition: form-data; name=\"%s\"\r\n", $name); |
157
|
1 |
|
$headers .= \sprintf("Content-Length: %s\r\n", (string) \strlen((string) $value)); |
158
|
|
|
|
159
|
1 |
|
$streams = ''; |
160
|
1 |
|
$streams .= "--$boundary\r\n$headers\r\n"; |
161
|
1 |
|
$streams .= $value; |
162
|
1 |
|
$streams .= "\r\n"; |
163
|
|
|
|
164
|
1 |
|
return $streams; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|