Skip to content

Commit 753cf95

Browse files
Merge pull request #11 from victorighalo/develop
Remove default log file being created on every request
2 parents 5dfcb82 + bead650 commit 753cf95

7 files changed

Lines changed: 98 additions & 76 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ src/Seerbit/Applications
1111
tests/seerbit.log
1212
.env
1313
*.bak
14+
*.cache
15+
.phpunit.cache

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
## 2.3.1 - 2023-02-28
2222

2323
- This version updates support for version 8.0 and above
24-
-
25-
## 2.3.2 - 2023-03-02
2624

25+
26+
## 2.3.2 - 2023-03-02
2727
- Updates and bug fixes
28+
29+
## 2.3.4 - 2023-03-03
30+
31+
- Remove default log file being created on every request

src/Seerbit/Client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function getLogger()
153153

154154
protected function createDefaultLogger()
155155
{
156-
$logger = new Logger('seerbit-php-api-library');
156+
$logger = new Logger('seerbit-php-api-library');
157157
try {
158158
$logger->pushHandler(new StreamHandler(($this->logger_path ? $this->logger_path : dirname(__FILE__) ) . '/seerbit.log', Logger::DEBUG));
159159
} catch (\Exception $e) {

src/Seerbit/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Config implements IConfig
77
/**
88
* @var array
99
*/
10-
protected $data = array('environment' => 'live');
10+
protected $data = array('environment' => 'live', 'endpoint' => 'https://seerbitapi.com/api/v2/');
1111

1212
/**
1313
* @var array

src/Seerbit/HttpClient/CurlClient.php

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
class CurlClient implements IClient
1010
{
1111

12-
13-
public function POST(IService $service, $requestUrl, $params = null, $token = null,$authType = \Seerbit\AuthType::BEARER)
12+
protected bool $shouldLog = false;
13+
/**
14+
* @throws SeerbitException
15+
*/
16+
public function POST(IService $service, $requestUrl, $params = null, $token = null, $authType = \Seerbit\AuthType::BEARER)
1417
{
1518

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

2124
// log the request
22-
$this->logRequest($logger, $requestUrl, $params);
25+
$this->shouldLog && $this->logRequest($logger, $requestUrl, $params);
2326

2427
//Initiate cURL.
2528
$ch = curl_init($requestUrl);
@@ -49,9 +52,9 @@ public function POST(IService $service, $requestUrl, $params = null, $token = nu
4952
}else{
5053
if($authType === \Seerbit\AuthType::BASIC){
5154
$key = base64_encode($client->getPublicKey().":".$client->getSecretKey());
52-
array_push($headers,'Authorization: Basic '.$key);
55+
$headers[] = 'Authorization: Basic ' . $key;
5356
}else{
54-
array_push($headers,'Authorization: Bearer '.$token);
57+
$headers[] = 'Authorization: Bearer ' . $token;
5558
}
5659

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

7073
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
7174
}
7275

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

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

8184

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

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

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

100103
$result = json_decode($result, true);
101104

102105
if (is_array($result) || is_object($result)){
103106
$msg = "";
104-
$data = isset($result["data"]) ? $result["data"] : null;
107+
$data = $result["data"] ?? null;
105108
if (is_null($data)){
106-
$data = isset($result["networks"]) ? $result["networks"] : null;
109+
$data = $result["networks"] ?? null;
107110
}
108111
if(isset($result["message"]) ){
109112
$msg = $result["message"];
@@ -125,15 +128,18 @@ public function POST(IService $service, $requestUrl, $params = null, $token = nu
125128

126129
}
127130

128-
public function GET(IService $service, $requestUrl, $token = null,$authType = \Seerbit\AuthType::BEARER)
131+
/**
132+
* @throws SeerbitException
133+
*/
134+
public function GET(IService $service, $requestUrl, $token = null, $authType = \Seerbit\AuthType::BEARER)
129135
{
130136

131137
$client = $service->getClient();
132138
$config = $client->getConfig();
133139
$logger = $client->getLogger();
134140

135141
// log the request
136-
$this->logRequest($logger, $requestUrl, null);
142+
$this->shouldLog && $this->logRequest($logger, $requestUrl, null);
137143

138144
//Initiate cURL.
139145
$ch = curl_init($requestUrl);
@@ -157,9 +163,9 @@ public function GET(IService $service, $requestUrl, $token = null,$authType = \S
157163
}else{
158164
if($authType === \Seerbit\AuthType::BASIC){
159165
$key = base64_encode($client->getPublicKey().":".$client->getSecretKey());
160-
array_push($headers,'Authorization: Basic '.$key);
166+
$headers[] = 'Authorization: Basic ' . $key;
161167
}else{
162-
array_push($headers,'Authorization: Bearer '.$token);
168+
$headers[] = 'Authorization: Bearer ' . $token;
163169
}
164170

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

178184
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
179185
}
180186

181187

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

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

190196

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

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

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

208214
$result = json_decode($result, true);
209215

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

234240
}
235241

236-
public function PUT(IService $service, $requestUrl, $params = null, $token = null,$authType = \Seerbit\AuthType::BEARER)
242+
/**
243+
* @throws SeerbitException
244+
*/
245+
public function PUT(IService $service, $requestUrl, $params = null, $token = null, $authType = \Seerbit\AuthType::BEARER)
237246
{
238247

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

244253
// log the request
245-
$this->logRequest($logger, $requestUrl, $params);
254+
$this->shouldLog && $this->logRequest($logger, $requestUrl, $params);
246255

247256
//Initiate cURL.
248257
$ch = curl_init($requestUrl);
@@ -273,9 +282,9 @@ public function PUT(IService $service, $requestUrl, $params = null, $token = nul
273282
}else{
274283
if($authType === \Seerbit\AuthType::BASIC){
275284
$key = base64_encode($client->getPublicKey().":".$client->getSecretKey());
276-
array_push($headers,'Authorization: Basic '.$key);
285+
$headers[] = 'Authorization: Basic ' . $key;
277286
}else{
278-
array_push($headers,'Authorization: Bearer '.$token);
287+
$headers[] = 'Authorization: Bearer ' . $token;
279288
}
280289

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

294303
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
295304
}
296305

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

299308

300309

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

307316

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

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

347356
}
348357

349-
protected function handleCurlError($url,$result, $errno, $message, $logger)
358+
/**
359+
* @throws SeerbitException
360+
*/
361+
protected function handleCurlError($url, $result, $errno, $message, $logger)
350362
{
351-
switch ($errno) {
352-
case CURLE_OK:
353-
$msg = "Probably your Web Service username and/or password is incorrect";
354-
break;
355-
case CURLE_COULDNT_RESOLVE_HOST:
356-
case CURLE_OPERATION_TIMEOUTED:
357-
$msg = "Could not connect to Seerbit ($url). Please check your "
358-
. "internet connection and try again.";
359-
break;
360-
case CURLE_SSL_CACERT:
361-
case CURLE_SSL_PEER_CERTIFICATE:
362-
$msg = "Could not verify Seerbit's SSL certificate. Please make sure "
363-
. "that your network is not intercepting certificates. "
364-
. "(Try going to $url in your browser.) "
365-
. "If this problem persists,";
366-
break;
367-
default:
368-
$msg = "Unexpected error communicating with Seerbit Server.";
369-
}
363+
$msg = match ($errno) {
364+
CURLE_OK => "Probably your Web Service username and/or password is incorrect",
365+
CURLE_COULDNT_RESOLVE_HOST, CURLE_OPERATION_TIMEOUTED => "Could not connect to Seerbit ($url). Please check your "
366+
. "internet connection and try again.",
367+
CURLE_SSL_CACERT, CURLE_SSL_PEER_CERTIFICATE => "Could not verify Seerbit's SSL certificate. Please make sure "
368+
. "that your network is not intercepting certificates. "
369+
. "(Try going to $url in your browser.) "
370+
. "If this problem persists,",
371+
default => "Unexpected error communicating with Seerbit Server.",
372+
};
370373
$msg .= "\n(Network error [errno $errno]: $message)";
371374
$msg .= "\n(Network error [result $errno]: $result)";
372-
$logger->error($msg);
373-
throw new \Seerbit\ConnectionException($msg, $errno);
375+
$this->shouldLog && $logger->error($msg);
376+
throw new \Seerbit\SeerbitException($msg, $errno);
374377
}
375378

379+
/**
380+
* @throws SeerbitException
381+
*/
376382
protected function handleResultError($result, $logger)
377383
{
378384

379385
$decodeResult = json_decode($result, true);
380386

381387
if ($result) {
382388
if (isset($decodeResult['message'])) {
383-
$logger->error($decodeResult['message']);
389+
$this->shouldLog && $logger->error($decodeResult['message']);
384390
throw new SeerbitException(
385391
$decodeResult['message'],
386392
"-00",
@@ -389,19 +395,19 @@ protected function handleResultError($result, $logger)
389395
time()
390396
);
391397
}
392-
$logger->error($result);
398+
$this->shouldLog && $logger->error($result);
393399
throw new SeerbitException("Error making HTTP request to SeerBit server", 500, null, "Server Error", time());
394400
}else{
395-
$logger->error($result);
401+
$this->shouldLog && $logger->error($result);
396402
throw new SeerbitException("Error making HTTP request to SeerBit server", 500, null, "Server Error", time());
397403
}
398404
}
399405

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

407413
protected function curlRequest($ch)

src/Seerbit/Service/Authenticate.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ public function __construct(\Seerbit\Client $client)
1313

1414
}
1515

16-
// public function Auth(){
17-
// $client = $this->getClient();
18-
// $config = $client->getConfig();
19-
// $params = ['clientId' => $config->getPublicKey(),"clientSecret" => $config->getClientSecret()];
20-
// $this->result = $this->postRequest("sbt/api/v1/auth",$params);
21-
// return $this;
22-
// }
16+
public function Auth(){
17+
$client = $this->getClient();
18+
$config = $client->getConfig();
19+
$params = ['clientId' => $config->getPublicKey(),"clientSecret" => $config->getClientSecret()];
20+
$this->result = $this->postRequest("sbt/api/v1/auth",$params);
21+
return $this;
22+
}
2323

2424
public function toArray(){
2525
return $this->result;

0 commit comments

Comments
 (0)