Skip to content

Commit

Permalink
Merge pull request #5 from julienloizelet/feat/handle-404-for-get-dec…
Browse files Browse the repository at this point in the history
…isions

Feat/handle 404 for get decisions
  • Loading branch information
julienloizelet committed Jan 5, 2023
2 parents 20f47f3 + d9fc45c commit bc1de4f
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 8 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.3.0](https://github.com/crowdsecurity/php-lapi-client/releases/tag/v0.3.0) - 2023-01-05
[_Compare with previous release_](https://github.com/crowdsecurity/php-lapi-client/compare/v0.2.0...v0.3.0)

### Changed

- Do not throw error on LAPI 404 response
- Use compressed requests for `Curl`
- Use message log instead of a context message field

---


## [0.2.0](https://github.com/crowdsecurity/php-lapi-client/releases/tag/v0.2.0) - 2022-12-29
[_Compare with previous release_](https://github.com/crowdsecurity/php-lapi-client/compare/v0.1.0...v0.2.0)
Expand Down
12 changes: 9 additions & 3 deletions src/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ protected function request(
): array {
$method = strtoupper($method);
if (!in_array($method, $this->allowedMethods)) {
throw new ClientException("Method ($method) is not allowed.");
$message = "Method ($method) is not allowed.";
$this->logger->error($message, ['type' => 'LAPI_CLIENT_REQUEST']);
throw new ClientException($message);
}

$response = $this->sendRequest(
Expand Down Expand Up @@ -130,13 +132,17 @@ private function formatResponseBody(Response $response): array
$decoded = json_decode($response->getJsonBody(), true);

if (null === $decoded) {
throw new ClientException('Body response is not a valid json');
$message = 'Body response is not a valid json';
$this->logger->error($message, ['type' => 'LAPI_CLIENT_FORMAT_RESPONSE']);
throw new ClientException($message);
}
}

if ($statusCode < 200 || $statusCode >= 300) {
$message = "Unexpected response status code: $statusCode. Body was: " . str_replace("\n", '', $body);
throw new ClientException($message, $statusCode);
if ($statusCode !== 404) {
throw new ClientException($message, $statusCode);
}
}

return $decoded;
Expand Down
4 changes: 3 additions & 1 deletion src/Bouncer.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public function getFilteredDecisions(array $filter = []): array

/**
* Process a decisions stream call to LAPI.
* When the $startup flag is used, all the decisions are returned.
* Else only the decisions updates (add or remove) from the last stream call are returned.
*
* @see https://crowdsecurity.github.io/api_doc/index.html?urls.primaryName=LAPI#/bouncers/getDecisionsStream
* @throws ClientException
Expand Down Expand Up @@ -114,7 +116,7 @@ private function manageRequest(
string $endpoint,
array $parameters = []
): array {
$this->logger->debug('', [
$this->logger->debug('Now processing a bouncer request', [
'type' => 'BOUNCER_CLIENT_REQUEST',
'method' => $method,
'endpoint' => $endpoint,
Expand Down
2 changes: 1 addition & 1 deletion src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ class Constants
/**
* @var string The current version of this library
*/
public const VERSION = 'v0.2.0';
public const VERSION = 'v0.3.0';
}
4 changes: 2 additions & 2 deletions src/Logger/FileLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public function __construct(array $configs = [])
if (empty($configs['disable_prod_log'])) {
$logPath = $logDir . '/' . self::PROD_FILE;
$fileHandler = new RotatingFileHandler($logPath, 0, Logger::INFO);
$fileHandler->setFormatter(new LineFormatter("%datetime%|%level%|%context%\n"));
$fileHandler->setFormatter(new LineFormatter("%datetime%|%level%|%message%|%context%\n"));
$this->pushHandler($fileHandler);
}

if (!empty($configs['debug_mode'])) {
$debugLogPath = $logDir . '/' . self::DEBUG_FILE;
$debugFileHandler = new RotatingFileHandler($debugLogPath, 0, Logger::DEBUG);
$debugFileHandler->setFormatter(new LineFormatter("%datetime%|%level%|%context%\n"));
$debugFileHandler->setFormatter(new LineFormatter("%datetime%|%level%|%message%|%context%\n"));
$this->pushHandler($debugFileHandler);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/RequestHandler/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ private function createOptions(Request $request): array
\CURLOPT_HEADER => false,
\CURLOPT_RETURNTRANSFER => true,
\CURLOPT_USERAGENT => $headers['User-Agent'],
\CURLOPT_ENCODING => ''
];

$options[\CURLOPT_HTTPHEADER] = [];
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/AbstractClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function testClientInit()
$this->assertEquals(
Constants::DEFAULT_LAPI_URL . '/',
$url,
'Url should be dev by default'
'Url should be default'
);
$this->assertEquals(
'/',
Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/CurlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public function testOptions()
\CURLOPT_CUSTOMREQUEST => $method,
\CURLOPT_TIMEOUT => TestConstants::API_TIMEOUT,
\CURLOPT_SSL_VERIFYPEER => false,
\CURLOPT_ENCODING => ''
];

$this->assertEquals(
Expand Down Expand Up @@ -217,6 +218,7 @@ public function testOptions()
\CURLOPT_CUSTOMREQUEST => $method,
\CURLOPT_TIMEOUT => TestConstants::API_TIMEOUT,
\CURLOPT_SSL_VERIFYPEER => false,
\CURLOPT_ENCODING => ''
];

$this->assertEquals(
Expand Down Expand Up @@ -253,6 +255,7 @@ public function testOptions()
\CURLOPT_CUSTOMREQUEST => $method,
\CURLOPT_TIMEOUT => TestConstants::API_TIMEOUT,
\CURLOPT_SSL_VERIFYPEER => true,
\CURLOPT_ENCODING => '',
\CURLOPT_SSLCERT => 'tls_cert_path_test',
\CURLOPT_SSLKEY => 'tls_key_path_test',
\CURLOPT_CAINFO => 'tls_ca_cert_path_test',
Expand Down Expand Up @@ -282,6 +285,7 @@ public function testOptions()
'User-Agent:' . TestConstants::USER_AGENT_SUFFIX,
],
\CURLOPT_POST => false,
\CURLOPT_ENCODING => '',
\CURLOPT_URL => $url,
\CURLOPT_CUSTOMREQUEST => $method,
\CURLOPT_TIMEOUT => TestConstants::API_TIMEOUT,
Expand Down

0 comments on commit bc1de4f

Please sign in to comment.