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