Skip to content

Commit

Permalink
Merge pull request #8 from Doncode/master
Browse files Browse the repository at this point in the history
Optimize performance. For example send 3 message before~0.98sec, after~0.53sec
  • Loading branch information
unreal4u committed Apr 25, 2016
2 parents bef0b2c + be7ef18 commit 4bbbeba
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/TgLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace unreal4u;

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use unreal4u\Abstracts\TelegramTypes;
use unreal4u\InternalFunctionality\DummyLogger;
use unreal4u\InternalFunctionality\TelegramDocument;
Expand All @@ -17,6 +18,11 @@
*/
class TgLog
{
/**
* @var ClientInterface
*/
protected $httpClient;

/**
* Stores the token
* @var string
Expand Down Expand Up @@ -63,6 +69,7 @@ public function __construct(string $botToken, LoggerInterface $logger = null)
$logger = new DummyLogger();
}
$this->logger = $logger;
$this->httpClient = new Client();

$this->constructApiUrl();
}
Expand Down Expand Up @@ -97,9 +104,8 @@ public function downloadFile(File $file): TelegramDocument
{
$this->logger->debug('Downloading file from Telegram, creating URI');
$url = 'https://api.telegram.org/file/bot' . $this->botToken . '/' . $file->file_path;
$client = new Client();
$this->logger->debug('About to perform request');
return new TelegramDocument($client->get($url));
return new TelegramDocument($this->httpClient->get($url));
}

/**
Expand All @@ -123,8 +129,7 @@ final private function constructApiUrl(): TgLog
protected function sendRequestToTelegram(TelegramMethods $method, array $formData): array
{
$this->logger->debug('About to instantiate HTTP Client');
$client = new Client();
$response = $client->post($this->composeApiMethodUrl($method), $formData);
$response = $this->httpClient->post($this->composeApiMethodUrl($method), $formData);
$this->logger->debug('Got response back from Telegram, applying json_decode');
return json_decode((string)$response->getBody(), true);
}
Expand Down

0 comments on commit 4bbbeba

Please sign in to comment.