Skip to content

Commit

Permalink
Added logging for rate limits / calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ThijsdejongSNC committed Mar 17, 2020
1 parent a7ada09 commit 7b42280
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Picqer/Financials/Exact/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,15 @@ private function createRequest($method, $endpoint, $body = null, array $params =
*/
public function get($url, array $params = [], array $headers = [])
{
$this->log("get ".$url);
$url = $this->formatUrl($url, $url !== 'current/Me', $url == $this->nextUrl);

try {
$request = $this->createRequest('GET', $url, null, $params, $headers);
$response = $this->client()->send($request);

$this->extractRateLimits($response);

return $this->parseResponse($response, $url != $this->nextUrl);
} catch (Exception $e) {
$this->parseExceptionForErrorMessages($e);
Expand All @@ -267,6 +270,7 @@ public function get($url, array $params = [], array $headers = [])
*/
public function post($url, $body)
{
$this->log("post ".$url);
$url = $this->formatUrl($url);

try {
Expand All @@ -289,6 +293,7 @@ public function post($url, $body)
*/
public function put($url, $body)
{
$this->log("put ".$url);
$url = $this->formatUrl($url);

try {
Expand All @@ -310,6 +315,7 @@ public function put($url, $body)
*/
public function delete($url)
{
$this->log("delete ".$url);
$url = $this->formatUrl($url);

try {
Expand Down Expand Up @@ -764,5 +770,31 @@ private function extractRateLimits(Response $response)

$this->minutelyLimit = (int) $response->getHeaderLine('X-RateLimit-Minutely-Limit');
$this->minutelyLimitRemaining = (int) $response->getHeaderLine('X-RateLimit-Minutely-Remaining');
$this->log("RL: dailyLimitRemaining: ".$this->dailyLimitRemaining."/ minutelyLimitRemaining: ".$this->minutelyLimitRemaining);
}

function log($arMsg)
{
//define empty string
$stEntry = "";
//get the event occur date time,when it will happened
$arLogData['event_datetime'] = '[' . date('D Y-m-d h:i:s A') . '] ';
//if message is array type
if (is_array($arMsg)) {
//concatenate msg with datetime
foreach ($arMsg as $msg)
$stEntry .= $arLogData['event_datetime'] . " " . $msg . PHP_EOL;
} else { //concatenate msg with datetime

$stEntry .= $arLogData['event_datetime'] . " " . $arMsg . PHP_EOL;
}
//create file with current date name
$stCurLogFileName = 'exactperformance.log';
//open the file append mode,dats the log file will create day wise
$fHandler = fopen("./var/log/".$stCurLogFileName, 'a+');
//write the info into the file
fwrite($fHandler, $stEntry);
//close handler
fclose($fHandler);
}
}

0 comments on commit 7b42280

Please sign in to comment.