Skip to content

Commit

Permalink
Merge pull request #9 from julienloizelet/feat/use-php-common
Browse files Browse the repository at this point in the history
Feat/use php common
  • Loading branch information
julienloizelet committed Feb 2, 2023
2 parents 659095b + 774992c commit ecf9afe
Show file tree
Hide file tree
Showing 33 changed files with 120 additions and 1,566 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
run: |
ddev composer validate --strict --working-dir ./${{env.EXTENSION_PATH}}
- name: Install CrowdSec lib dependencies and Coding standards tools
- name: Install dependencies and Coding standards tools
run: |
ddev composer update --working-dir ./${{env.EXTENSION_PATH}}
ddev composer update --working-dir ./${{env.EXTENSION_PATH}}/tools/coding-standards
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:
# Check top [_Compare with previous release_](GITHUB_URL/compare/vLAST_TAG...vVERSION_NUMBER) in CHANGELOG.md
if [[ ${{ github.event.inputs.first-release }} != "true" ]]
then
COMPARISON=$(grep -oP "\/compare\/\K(.*)$" CHANGELOG.md | head -1)
COMPARISON=$(grep -oP "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/compare/\K(.*)$" CHANGELOG.md | head -1)
LAST_TAG=$(curl -Ls -o /dev/null -w %{url_effective} $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/releases/latest | grep -oP "\/tag\/\K(.*)$")
if [[ $COMPARISON == "$LAST_TAG...v${{ env.VERSION_NUMBER }})" ]]
then
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unit-and-integration-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ jobs:
run: |
ddev composer validate --strict --working-dir ./${{env.EXTENSION_PATH}}
- name: Install CrowdSec lib dependencies
- name: Install dependencies
run: |
ddev composer update --working-dir ./${{env.EXTENSION_PATH}}
Expand Down
27 changes: 21 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

The purpose of this section is to declare the public API of this library as required by [item 1 of semantic versioning specification](https://semver.org/spec/v2.0.0.html#spec-item-1).

The public API of this library consists of all public or protected methods, properties and constants belonging to
the following files:
- `src/Bouncer.php`
- `src/ClientException.php`
- `src/Configuration.php`
- `src/Constants.php`
The public API of this library consists of all public or protected methods, properties and constants belonging to the `src` folder.

---


## [2.0.0](https://github.com/crowdsecurity/php-lapi-client/releases/tag/v2.0.0) - 2023-02-02
[_Compare with previous release_](https://github.com/crowdsecurity/php-lapi-client/compare/v1.0.1...v2.0.0)

### Changed

- Use `crowdsec/common` package as a dependency for code factoring

- *Breaking change*: Use `CrowdSec\Common` classes for the following files and folder:
- `HttpMessage`
- `Logger`
- `RequestHanlder`
- `AbstractClient.php`

- *Breaking change*: If not null, the second param of `Bouncer` constructor must be of type
`CrowdSec\Common\Client\RequestHandler\AbstractRequestHandler`
- *Breaking change*: Move `Bouncer` constants in `Constants`

---

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
},
"require": {
"php": "^7.2.5 || ^8.0",
"crowdsec/common": "^1.2.0",
"ext-json": "*",
"symfony/config": "^4.4.44 || ^5.4.11 || ^6.0.11",
"monolog/monolog": "^1.17 || ^2.1"
Expand Down
24 changes: 12 additions & 12 deletions docs/USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ To instantiate a bouncer client, you have to:
settings](#bouncer-client-configurations).


- Optionally, you can pass an implementation of the `RequestHandlerInterface` as a second
parameter. By default, a `Curl` request handler will be used.
- Optionally, you can pass an implementation of the `AbstractRequestHandler` (from the `crowdsec/common` dependency
package) as a second parameter. By default, a `Curl` request handler will be used.


- Optionally, to log some information, you can pass an implementation of the `Psr\Log\LoggerInterface` as a fourth
parameter. You will find an example of such implementation with the provided `Logger\FileLog` class.
- Optionally, to log some information, you can pass an implementation of the `Psr\Log\LoggerInterface` as a third
parameter. You will find an example of such implementation with the provided `Logger\FileLog` class of the
`crowdsec/common` dependency package.

```php
use CrowdSec\LapiClient\Bouncer;
Expand Down Expand Up @@ -310,18 +311,17 @@ By default, the `Bouncer` object will do curl requests to call the LAPI. If for
use curl then you can create your own request handler class and pass it as a second parameter of the `Bouncer`
constructor.

Your custom request handler class must implement the `RequestHandlerInterface` interface, and you will have to
explicitly
write an `handle` method:
Your custom request handler class must extend the `AbstractRequestHandler` class of the `crowdsec/common` dependency,
and you will have to explicitly write an `handle` method:

```php
<?php

use CrowdSec\LapiClient\HttpMessage\Request;
use CrowdSec\LapiClient\HttpMessage\Response;
use CrowdSec\LapiClient\RequestHandler\RequestHandlerInterface;
use CrowdSec\Common\Client\HttpMessage\Request;
use CrowdSec\Common\Client\HttpMessage\Response;
use CrowdSec\Common\Client\RequestHandler\AbstractRequestHandler;

class CustomRequestHandler implements RequestHandlerInterface
class CustomRequestHandler extends AbstractRequestHandler
{
/**
* Performs an HTTP request and returns a response.
Expand Down Expand Up @@ -361,7 +361,7 @@ handler. To use it, you should instantiate it and pass the created object as a p

```php
use CrowdSec\LapiClient\Bouncer;
use CrowdSec\LapiClient\RequestHandler\FileGetContents;
use CrowdSec\Common\Client\RequestHandler\FileGetContents;

$requestHandler = new FileGetContents($configs);

Expand Down
159 changes: 0 additions & 159 deletions src/AbstractClient.php

This file was deleted.

38 changes: 19 additions & 19 deletions src/Bouncer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace CrowdSec\LapiClient;

use CrowdSec\LapiClient\RequestHandler\RequestHandlerInterface;
use CrowdSec\Common\Client\AbstractClient;
use CrowdSec\Common\Client\ClientException as CommonClientException;
use CrowdSec\Common\Client\RequestHandler\AbstractRequestHandler;
use Psr\Log\LoggerInterface;
use Symfony\Component\Config\Definition\Processor;

Expand All @@ -20,14 +22,6 @@
*/
class Bouncer extends AbstractClient
{
/**
* @var string The decisions endpoint
*/
public const DECISIONS_FILTER_ENDPOINT = '/v1/decisions';
/**
* @var string The decisions stream endpoint
*/
public const DECISIONS_STREAM_ENDPOINT = '/v1/decisions/stream';
/**
* @var array
*/
Expand All @@ -39,7 +33,7 @@ class Bouncer extends AbstractClient

public function __construct(
array $configs,
RequestHandlerInterface $requestHandler = null,
AbstractRequestHandler $requestHandler = null,
LoggerInterface $logger = null
) {
$this->configure($configs);
Expand All @@ -54,13 +48,14 @@ public function __construct(
* Process a decisions call to LAPI with some filter(s).
*
* @see https://crowdsecurity.github.io/api_doc/index.html?urls.primaryName=LAPI#/bouncers/getDecisions
*
* @throws ClientException
*/
public function getFilteredDecisions(array $filter = []): array
{
return $this->manageRequest(
'GET',
self::DECISIONS_FILTER_ENDPOINT,
Constants::DECISIONS_FILTER_ENDPOINT,
$filter
);
}
Expand All @@ -71,6 +66,7 @@ public function getFilteredDecisions(array $filter = []): array
* 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
*/
public function getStreamDecisions(
Expand All @@ -79,7 +75,7 @@ public function getStreamDecisions(
): array {
return $this->manageRequest(
'GET',
self::DECISIONS_STREAM_ENDPOINT,
Constants::DECISIONS_STREAM_ENDPOINT,
array_merge(['startup' => $startup ? 'true' : 'false'], $filter)
);
}
Expand Down Expand Up @@ -116,13 +112,17 @@ private function manageRequest(
string $endpoint,
array $parameters = []
): array {
$this->logger->debug('Now processing a bouncer request', [
'type' => 'BOUNCER_CLIENT_REQUEST',
'method' => $method,
'endpoint' => $endpoint,
'parameters' => $parameters,
]);
try {
$this->logger->debug('Now processing a bouncer request', [
'type' => 'BOUNCER_CLIENT_REQUEST',
'method' => $method,
'endpoint' => $endpoint,
'parameters' => $parameters,
]);

return $this->request($method, $endpoint, $parameters, $this->headers);
return $this->request($method, $endpoint, $parameters, $this->headers);
} catch (CommonClientException $e) {
throw new ClientException($e->getMessage(), $e->getCode(), $e);
}
}
}
4 changes: 3 additions & 1 deletion src/ClientException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace CrowdSec\LapiClient;

use CrowdSec\Common\Client\ClientException as CommonClientException;

/**
* Exception interface for all exceptions thrown by CrowdSec LAPI Client.
*
Expand All @@ -14,6 +16,6 @@
* @copyright Copyright (c) 2022+ CrowdSec
* @license MIT License
*/
class ClientException extends \RuntimeException
class ClientException extends CommonClientException
{
}

0 comments on commit ecf9afe

Please sign in to comment.