Skip to content

Commit

Permalink
Update TelegramClient.php (#1031)
Browse files Browse the repository at this point in the history
Co-authored-by: Irfaq Syed <irazasyed@users.noreply.github.com>
  • Loading branch information
Thajudecodes and irazasyed committed Mar 7, 2023
1 parent e61f1f8 commit 1af82e2
Showing 1 changed file with 20 additions and 76 deletions.
96 changes: 20 additions & 76 deletions src/TelegramClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,127 +8,71 @@
use Telegram\Bot\HttpClients\GuzzleHttpClient;
use Telegram\Bot\HttpClients\HttpClientInterface;

/**
* Class TelegramClient.
*/
class TelegramClient
{
/** @var string Telegram Bot API URL. */
const BASE_BOT_URL = 'https://api.telegram.org/bot';

/** @var HttpClientInterface|null HTTP Client. */
protected $httpClientHandler;
protected HttpClientInterface $httpClientHandler;

/** @var string|null base bot url. */
protected $baseBotUrl;
protected string $baseBotUrl;

/**
* Instantiates a new TelegramClient object.
*
* @param string|null $baseBotUrl
*/
public function __construct(HttpClientInterface $httpClientHandler = null, $baseBotUrl = null)
public function __construct(HttpClientInterface $httpClientHandler = null, string $baseBotUrl = null)
{
$this->httpClientHandler = $httpClientHandler ?? new GuzzleHttpClient();

$this->baseBotUrl = $baseBotUrl;
$this->baseBotUrl = $baseBotUrl ?? static::BASE_BOT_URL;
}

/**
* Returns the HTTP client handler.
*
* @return HttpClientInterface
*/
public function getHttpClientHandler()
public function getHttpClientHandler(): HttpClientInterface
{
return $this->httpClientHandler;
}

/**
* Sets the HTTP client handler.
*/
public function setHttpClientHandler(HttpClientInterface $httpClientHandler): self
{
$this->httpClientHandler = $httpClientHandler;

return $this;
}

/**
* Send an API request and process the result.
*
*
* @throws TelegramSDKException
*/
public function sendRequest(TelegramRequest $request): TelegramResponse
{
[$url, $method, $headers, $isAsyncRequest] = $this->prepareRequest($request);

$options = $this->getOption($request, $method);
$options = $this->getOptions($request, $method);

$rawResponse = $this->getHttpClientHandler()
->setTimeOut($request->getTimeOut())
->setConnectTimeOut($request->getConnectTimeOut())
->send(
$url,
$method,
$headers,
$options,
$isAsyncRequest
);

$returnResponse = $this->getResponse($request, $rawResponse);

if ($returnResponse->isError()) {
throw $returnResponse->getThrownException();
->send($url, $method, $headers, $options, $isAsyncRequest);

$response = $this->getResponse($request, $rawResponse);

if ($response->isError()) {
throw $response->getThrownException();
}

return $returnResponse;
return $response;
}

/**
* Prepares the API request for sending to the client handler.
*/
public function prepareRequest(TelegramRequest $request): array
{
$url = $this->getBaseBotUrl().$request->getAccessToken().'/'.$request->getEndpoint();

return [
$url,
$request->getMethod(),
$request->getHeaders(),
$request->isAsyncRequest(),
];

return [$url, $request->getMethod(), $request->getHeaders(), $request->isAsyncRequest()];
}

/**
* Returns the base Bot URL.
*/
public function getBaseBotUrl(): string
{
return $this->baseBotUrl ?? static::BASE_BOT_URL;
return $this->baseBotUrl;
}

/**
* Creates response object.
*
* @param ResponseInterface|PromiseInterface $response
*/
protected function getResponse(TelegramRequest $request, $response): TelegramResponse
{
return new TelegramResponse($request, $response);
}

/**
* @param string $method
* @return array
*/
private function getOption(TelegramRequest $request, $method)
private function getOptions(TelegramRequest $request, string $method): array
{
if ($method === 'POST') {
return $request->getPostParams();
}

return ['query' => $request->getParams()];
return $method === 'POST' ? $request->getPostParams() : ['query' => $request->getParams()];
}
}


0 comments on commit 1af82e2

Please sign in to comment.