Skip to content

Commit

Permalink
Merge pull request #11 from victorighalo/develop
Browse files Browse the repository at this point in the history
Remove default log file being created on every request
  • Loading branch information
seerbit-developers authored Mar 3, 2023
2 parents 5dfcb82 + bead650 commit 753cf95
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 76 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ src/Seerbit/Applications
tests/seerbit.log
.env
*.bak
*.cache
.phpunit.cache
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
## 2.3.1 - 2023-02-28

- This version updates support for version 8.0 and above
-
## 2.3.2 - 2023-03-02


## 2.3.2 - 2023-03-02
- Updates and bug fixes

## 2.3.4 - 2023-03-03

- Remove default log file being created on every request
2 changes: 1 addition & 1 deletion src/Seerbit/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function getLogger()

protected function createDefaultLogger()
{
$logger = new Logger('seerbit-php-api-library');
$logger = new Logger('seerbit-php-api-library');
try {
$logger->pushHandler(new StreamHandler(($this->logger_path ? $this->logger_path : dirname(__FILE__) ) . '/seerbit.log', Logger::DEBUG));
} catch (\Exception $e) {
Expand Down
2 changes: 1 addition & 1 deletion src/Seerbit/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Config implements IConfig
/**
* @var array
*/
protected $data = array('environment' => 'live');
protected $data = array('environment' => 'live', 'endpoint' => 'https://seerbitapi.com/api/v2/');

/**
* @var array
Expand Down
118 changes: 62 additions & 56 deletions src/Seerbit/HttpClient/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
class CurlClient implements IClient
{


public function POST(IService $service, $requestUrl, $params = null, $token = null,$authType = \Seerbit\AuthType::BEARER)
protected bool $shouldLog = false;
/**
* @throws SeerbitException
*/
public function POST(IService $service, $requestUrl, $params = null, $token = null, $authType = \Seerbit\AuthType::BEARER)
{

$client = $service->getClient();
Expand All @@ -19,7 +22,7 @@ public function POST(IService $service, $requestUrl, $params = null, $token = nu
$jsonRequest = json_encode($params);

// log the request
$this->logRequest($logger, $requestUrl, $params);
$this->shouldLog && $this->logRequest($logger, $requestUrl, $params);

//Initiate cURL.
$ch = curl_init($requestUrl);
Expand Down Expand Up @@ -49,9 +52,9 @@ public function POST(IService $service, $requestUrl, $params = null, $token = nu
}else{
if($authType === \Seerbit\AuthType::BASIC){
$key = base64_encode($client->getPublicKey().":".$client->getSecretKey());
array_push($headers,'Authorization: Basic '.$key);
$headers[] = 'Authorization: Basic ' . $key;
}else{
array_push($headers,'Authorization: Bearer '.$token);
$headers[] = 'Authorization: Bearer ' . $token;
}

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
Expand All @@ -62,15 +65,15 @@ public function POST(IService $service, $requestUrl, $params = null, $token = nu
//Set the headers
if($authType === \Seerbit\AuthType::BASIC){
$key = base64_encode($client->getPublicKey().":".$client->getSecretKey());
array_push($headers,'Authorization: Basic '.$key);
$headers[] = 'Authorization: Basic ' . $key;
}else{
array_push($headers,'Authorization: Bearer '.$token);
$headers[] = 'Authorization: Bearer ' . $token;
}

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}

$logger->info("Request headers to SeerBit" . print_r($headers, 1));
$this->shouldLog && $logger->info("Request headers to SeerBit" . print_r($headers, 1));

// return the result
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Expand All @@ -80,7 +83,7 @@ public function POST(IService $service, $requestUrl, $params = null, $token = nu


// log the raw response
$logger->info("JSON Response is: " . $result);
$this->shouldLog && $logger->info("JSON Response is: " . $result);

// Get errors
list($errno, $message) = $this->curlError($ch);
Expand All @@ -95,15 +98,15 @@ public function POST(IService $service, $requestUrl, $params = null, $token = nu
}

// log the array result
$logger->info('Params in response from SeerBit: ' . print_r(json_decode($result, true), 1));
$this->shouldLog && $logger->info('Params in response from SeerBit: ' . print_r(json_decode($result, true), 1));

$result = json_decode($result, true);

if (is_array($result) || is_object($result)){
$msg = "";
$data = isset($result["data"]) ? $result["data"] : null;
$data = $result["data"] ?? null;
if (is_null($data)){
$data = isset($result["networks"]) ? $result["networks"] : null;
$data = $result["networks"] ?? null;
}
if(isset($result["message"]) ){
$msg = $result["message"];
Expand All @@ -125,15 +128,18 @@ public function POST(IService $service, $requestUrl, $params = null, $token = nu

}

public function GET(IService $service, $requestUrl, $token = null,$authType = \Seerbit\AuthType::BEARER)
/**
* @throws SeerbitException
*/
public function GET(IService $service, $requestUrl, $token = null, $authType = \Seerbit\AuthType::BEARER)
{

$client = $service->getClient();
$config = $client->getConfig();
$logger = $client->getLogger();

// log the request
$this->logRequest($logger, $requestUrl, null);
$this->shouldLog && $this->logRequest($logger, $requestUrl, null);

//Initiate cURL.
$ch = curl_init($requestUrl);
Expand All @@ -157,9 +163,9 @@ public function GET(IService $service, $requestUrl, $token = null,$authType = \S
}else{
if($authType === \Seerbit\AuthType::BASIC){
$key = base64_encode($client->getPublicKey().":".$client->getSecretKey());
array_push($headers,'Authorization: Basic '.$key);
$headers[] = 'Authorization: Basic ' . $key;
}else{
array_push($headers,'Authorization: Bearer '.$token);
$headers[] = 'Authorization: Bearer ' . $token;
}

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
Expand All @@ -170,16 +176,16 @@ public function GET(IService $service, $requestUrl, $token = null,$authType = \S
//Set the headers
if($authType === \Seerbit\AuthType::BASIC){
$key = base64_encode($client->getPublicKey().":".$client->getSecretKey());
array_push($headers,'Authorization: Basic '.$key);
$headers[] = 'Authorization: Basic ' . $key;
}else{
array_push($headers,'Authorization: Bearer '.$token);
$headers[] = 'Authorization: Bearer ' . $token;
}

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}


$logger->info("Request headers to Seerbit" . print_r($headers, 1));
$this->shouldLog && $logger->info("Request headers to Seerbit" . print_r($headers, 1));

// return the result
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Expand All @@ -189,7 +195,7 @@ public function GET(IService $service, $requestUrl, $token = null,$authType = \S


// log the raw response
$logger->info("JSON Response is: " . $result);
$this->shouldLog && $logger->info("JSON Response is: " . $result);

// Get errors
list($errno, $message) = $this->curlError($ch);
Expand All @@ -203,7 +209,7 @@ public function GET(IService $service, $requestUrl, $token = null,$authType = \S
}

// log the array result
$logger->info('Params in response from Seerbit:' . print_r($result, 1));
$this->shouldLog && $logger->info('Params in response from Seerbit:' . print_r($result, 1));

$result = json_decode($result, true);

Expand Down Expand Up @@ -233,7 +239,10 @@ public function GET(IService $service, $requestUrl, $token = null,$authType = \S

}

public function PUT(IService $service, $requestUrl, $params = null, $token = null,$authType = \Seerbit\AuthType::BEARER)
/**
* @throws SeerbitException
*/
public function PUT(IService $service, $requestUrl, $params = null, $token = null, $authType = \Seerbit\AuthType::BEARER)
{

$client = $service->getClient();
Expand All @@ -242,7 +251,7 @@ public function PUT(IService $service, $requestUrl, $params = null, $token = nul
$jsonRequest = json_encode($params);

// log the request
$this->logRequest($logger, $requestUrl, $params);
$this->shouldLog && $this->logRequest($logger, $requestUrl, $params);

//Initiate cURL.
$ch = curl_init($requestUrl);
Expand Down Expand Up @@ -273,9 +282,9 @@ public function PUT(IService $service, $requestUrl, $params = null, $token = nul
}else{
if($authType === \Seerbit\AuthType::BASIC){
$key = base64_encode($client->getPublicKey().":".$client->getSecretKey());
array_push($headers,'Authorization: Basic '.$key);
$headers[] = 'Authorization: Basic ' . $key;
}else{
array_push($headers,'Authorization: Bearer '.$token);
$headers[] = 'Authorization: Bearer ' . $token;
}

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
Expand All @@ -286,15 +295,15 @@ public function PUT(IService $service, $requestUrl, $params = null, $token = nul
//Set the headers
if($authType === \Seerbit\AuthType::BASIC){
$key = base64_encode($client->getPublicKey().":".$client->getSecretKey());
array_push($headers,'Authorization: Basic '.$key);
$headers[] = 'Authorization: Basic ' . $key;
}else{
array_push($headers,'Authorization: Bearer '.$token);
$headers[] = 'Authorization: Bearer ' . $token;
}

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}

$logger->info("Request headers to Seerbit" . print_r($headers, 1));
$this->shouldLog && $logger->info("Request headers to Seerbit" . print_r($headers, 1));



Expand All @@ -306,7 +315,7 @@ public function PUT(IService $service, $requestUrl, $params = null, $token = nul


// log the raw response
$logger->info("JSON Response is: " . $result);
$this->shouldLog && $logger->info("JSON Response is: " . $result);

// Get errors
list($errno, $message) = $this->curlError($ch);
Expand Down Expand Up @@ -346,41 +355,38 @@ public function PUT(IService $service, $requestUrl, $params = null, $token = nul

}

protected function handleCurlError($url,$result, $errno, $message, $logger)
/**
* @throws SeerbitException
*/
protected function handleCurlError($url, $result, $errno, $message, $logger)
{
switch ($errno) {
case CURLE_OK:
$msg = "Probably your Web Service username and/or password is incorrect";
break;
case CURLE_COULDNT_RESOLVE_HOST:
case CURLE_OPERATION_TIMEOUTED:
$msg = "Could not connect to Seerbit ($url). Please check your "
. "internet connection and try again.";
break;
case CURLE_SSL_CACERT:
case CURLE_SSL_PEER_CERTIFICATE:
$msg = "Could not verify Seerbit's SSL certificate. Please make sure "
. "that your network is not intercepting certificates. "
. "(Try going to $url in your browser.) "
. "If this problem persists,";
break;
default:
$msg = "Unexpected error communicating with Seerbit Server.";
}
$msg = match ($errno) {
CURLE_OK => "Probably your Web Service username and/or password is incorrect",
CURLE_COULDNT_RESOLVE_HOST, CURLE_OPERATION_TIMEOUTED => "Could not connect to Seerbit ($url). Please check your "
. "internet connection and try again.",
CURLE_SSL_CACERT, CURLE_SSL_PEER_CERTIFICATE => "Could not verify Seerbit's SSL certificate. Please make sure "
. "that your network is not intercepting certificates. "
. "(Try going to $url in your browser.) "
. "If this problem persists,",
default => "Unexpected error communicating with Seerbit Server.",
};
$msg .= "\n(Network error [errno $errno]: $message)";
$msg .= "\n(Network error [result $errno]: $result)";
$logger->error($msg);
throw new \Seerbit\ConnectionException($msg, $errno);
$this->shouldLog && $logger->error($msg);
throw new \Seerbit\SeerbitException($msg, $errno);
}

/**
* @throws SeerbitException
*/
protected function handleResultError($result, $logger)
{

$decodeResult = json_decode($result, true);

if ($result) {
if (isset($decodeResult['message'])) {
$logger->error($decodeResult['message']);
$this->shouldLog && $logger->error($decodeResult['message']);
throw new SeerbitException(
$decodeResult['message'],
"-00",
Expand All @@ -389,19 +395,19 @@ protected function handleResultError($result, $logger)
time()
);
}
$logger->error($result);
$this->shouldLog && $logger->error($result);
throw new SeerbitException("Error making HTTP request to SeerBit server", 500, null, "Server Error", time());
}else{
$logger->error($result);
$this->shouldLog && $logger->error($result);
throw new SeerbitException("Error making HTTP request to SeerBit server", 500, null, "Server Error", time());
}
}

private function logRequest(\Psr\Log\LoggerInterface $logger, $requestUrl, $params)
{
// log the requestUr, params and json request
$logger->info("Request url to SeerBit: " . $requestUrl);
$logger->info('JSON Request payload to SeerBit:' . json_encode($params));
$this->shouldLog && $logger->info("Request url to SeerBit: " . $requestUrl);
$this->shouldLog && $logger->info('JSON Request payload to SeerBit:' . json_encode($params));
}

protected function curlRequest($ch)
Expand Down
14 changes: 7 additions & 7 deletions src/Seerbit/Service/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public function __construct(\Seerbit\Client $client)

}

// public function Auth(){
// $client = $this->getClient();
// $config = $client->getConfig();
// $params = ['clientId' => $config->getPublicKey(),"clientSecret" => $config->getClientSecret()];
// $this->result = $this->postRequest("sbt/api/v1/auth",$params);
// return $this;
// }
public function Auth(){
$client = $this->getClient();
$config = $client->getConfig();
$params = ['clientId' => $config->getPublicKey(),"clientSecret" => $config->getClientSecret()];
$this->result = $this->postRequest("sbt/api/v1/auth",$params);
return $this;
}

public function toArray(){
return $this->result;
Expand Down
Loading

0 comments on commit 753cf95

Please sign in to comment.