Skip to content

Commit

Permalink
Fix sending files via third-party clients
Browse files Browse the repository at this point in the history
  • Loading branch information
BoShurik committed Sep 26, 2023
1 parent 64a9b86 commit d6bc837
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:

-
name: Remove http client dependencies
run: composer remove psr/http-client psr/http-factory symfony/http-client guzzlehttp/guzzle --dev --no-update
run: composer remove psr/http-client psr/http-factory php-http/multipart-stream-builder symfony/http-client guzzlehttp/guzzle --dev --no-update

-
name: Install dependencies with composer
Expand Down
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
"vimeo/psalm": "^5.9",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0",
"php-http/multipart-stream-builder": "^1.0",
"symfony/http-client": "^4.3 | ^5.0 | ^6.0",
"guzzlehttp/guzzle": "^7.0"
},
"suggest": {
"psr/http-client": "To use psr/http-client",
"psr/http-factory": "To use psr/http-client",
"php-http/multipart-stream-builder": "To use psr/http-client",
"guzzlehttp/guzzle": "To use psr/http-client",
"symfony/http-client": "To use symfony/http-client"
},
Expand Down Expand Up @@ -63,5 +65,10 @@
"branch-alias": {
"dev-master": "2.6-dev"
}
},
"config": {
"allow-plugins": {
"php-http/discovery": false
}
}
}
43 changes: 41 additions & 2 deletions src/Http/PsrHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace TelegramBot\Api\Http;

use Http\Message\MultipartStream\MultipartStreamBuilder;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use TelegramBot\Api\HttpException;
use TelegramBot\Api\InvalidJsonException;

Expand All @@ -20,10 +22,19 @@ class PsrHttpClient extends AbstractHttpClient
*/
private $requestFactory;

public function __construct(ClientInterface $http, RequestFactoryInterface $requestFactory)
{
/**
* @var StreamFactoryInterface
*/
private $streamFactory;

public function __construct(
ClientInterface $http,
RequestFactoryInterface $requestFactory,
StreamFactoryInterface $streamFactory
) {
$this->http = $http;
$this->requestFactory = $requestFactory;
$this->streamFactory = $streamFactory;
}

/**
Expand All @@ -33,11 +44,39 @@ protected function doRequest($url, array $data = null)
{
if ($data) {
$method = 'POST';
$data = array_filter($data);
} else {
$method = 'GET';
}

$request = $this->requestFactory->createRequest($method, $url);
if ($method === 'POST') {
$multipart = false;

/** @var array $data */
foreach ($data as &$value) {
if ($value instanceof \CURLFile) {
$value = fopen($value->getFilename(), 'r');
$multipart = true;
}
}
unset($value);

if ($multipart) {
$builder = new MultipartStreamBuilder($this->streamFactory);
foreach ($data as $name => $value) {
$builder->addResource($name, $value);
}
$stream = $builder->build();
$request = $request->withHeader('Content-Type', "multipart/form-data; boundary={$builder->getBoundary()}");
} else {
$stream = $this->streamFactory->createStream(http_build_query($data));
$request = $request->withHeader('Content-Type', 'application/x-www-form-urlencoded');
}

$request = $request->withBody($stream);
}

try {
$response = $this->http->sendRequest($request);
} catch (ClientExceptionInterface $exception) {
Expand Down
6 changes: 6 additions & 0 deletions src/Http/SymfonyHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ protected function doRequest($url, array $data = null)
$options = [];
if ($data) {
$method = 'POST';
foreach ($data as &$value) {
if ($value instanceof \CURLFile) {
$value = fopen($value->getFilename(), 'r');
}
}

$options['body'] = $data;
} else {
$method = 'GET';
Expand Down

0 comments on commit d6bc837

Please sign in to comment.