This repository has been archived by the owner on Nov 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91e1591
commit 51aade2
Showing
12 changed files
with
695 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Nexylan packages. | ||
* | ||
* (c) Nexylan SAS <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nexy\Graylog\Api; | ||
|
||
use Nexy\Graylog\Graylog; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* @author Sullivan Senechal <[email protected]> | ||
*/ | ||
abstract class AbstractApi | ||
{ | ||
/** | ||
* @var Graylog | ||
*/ | ||
protected $graylog; | ||
|
||
/** | ||
* @param Graylog $graylog | ||
*/ | ||
public function __construct(Graylog $graylog) | ||
{ | ||
$this->graylog = $graylog; | ||
} | ||
|
||
/** | ||
* Call GET http client request. | ||
* | ||
* @param string $path Request path | ||
* @param array $parameters GET Parameters | ||
* @param array $headers Reconfigure the request headers for this call only | ||
* | ||
* @return array | ||
*/ | ||
final protected function get($path, array $parameters = [], array $headers = []) | ||
{ | ||
$uri = $this->getApiBasePath().$path; | ||
if (count($parameters)) { | ||
$uri .= '?'.http_build_query($parameters); | ||
} | ||
|
||
return $this->parseResponseContent($this->graylog->getHttpClient()->get($uri, $headers)); | ||
} | ||
|
||
/** | ||
* Call POST http client request. | ||
* | ||
* @param string $path Request path | ||
* @param array $body Request body | ||
* @param array $headers Reconfigure the request headers for this call only | ||
* | ||
* @return array | ||
*/ | ||
final protected function post($path, array $body = null, array $headers = []) | ||
{ | ||
return $this->parseResponseContent( | ||
$this->graylog->getHttpClient()->post($path, $headers, $body ? json_encode($body) : null) | ||
); | ||
} | ||
|
||
abstract protected function getApiBasePath(); | ||
|
||
private function parseResponseContent(ResponseInterface $response) | ||
{ | ||
return json_decode($response->getBody()->getContents(), true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Nexylan packages. | ||
* | ||
* (c) Nexylan SAS <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nexy\Graylog\Api; | ||
|
||
use Nexy\Graylog\Api\Search\Keyword; | ||
use Nexy\Graylog\Api\Search\Relative; | ||
|
||
/** | ||
* @author Sullivan Senechal <[email protected]> | ||
*/ | ||
final class Search extends AbstractApi | ||
{ | ||
/** | ||
* @return Keyword | ||
*/ | ||
public function keyword() | ||
{ | ||
return new Keyword($this->graylog); | ||
} | ||
|
||
/** | ||
* @return Relative | ||
*/ | ||
public function relative() | ||
{ | ||
return new Relative($this->graylog); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getApiBasePath() | ||
{ | ||
return '/search'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Nexylan packages. | ||
* | ||
* (c) Nexylan SAS <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nexy\Graylog\Api\Search; | ||
|
||
use Nexy\Graylog\Api\AbstractApi; | ||
|
||
/** | ||
* @author Sullivan Senechal <[email protected]> | ||
*/ | ||
final class Keyword extends AbstractApi | ||
{ | ||
/** | ||
* @param string $query | ||
* @param string $rangeKeyword | ||
* @param array|null $fields | ||
* @param int|null $limit | ||
* @param int|null $offset | ||
* @param string $filter | ||
* @param string|null $sort | ||
* | ||
* @return array | ||
*/ | ||
public function fetch(string $query, string $rangeKeyword, array $fields = null, int $limit = null, int $offset = null, string $filter = null, string $sort = null) | ||
{ | ||
$parameters = [ | ||
'query' => $query, | ||
'keyword' => $rangeKeyword, | ||
]; | ||
|
||
if (null !== $fields) { | ||
$parameters['fields'] = implode(',', $fields); | ||
} | ||
if (null !== $limit) { | ||
$parameters['limit'] = $limit; | ||
} | ||
if (null !== $offset) { | ||
$parameters['offset'] = $offset; | ||
} | ||
if (null !== $filter) { | ||
$parameters['filter'] = $filter; | ||
} | ||
if (null !== $sort) { | ||
$parameters['sort'] = $sort; | ||
} | ||
|
||
return $this->get('/', $parameters); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getApiBasePath() | ||
{ | ||
return '/search/universal/keyword'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Nexylan packages. | ||
* | ||
* (c) Nexylan SAS <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nexy\Graylog\Api\Search; | ||
|
||
use Nexy\Graylog\Api\AbstractApi; | ||
|
||
/** | ||
* @author Sullivan Senechal <[email protected]> | ||
*/ | ||
final class Relative extends AbstractApi | ||
{ | ||
/** | ||
* @param string $field | ||
* @param string $query | ||
* @param int $range | ||
* @param int|null $size | ||
* @param string|null $filter | ||
* | ||
* @return array | ||
*/ | ||
public function terms(string $field, string $query, int $range, int $size = null, string $filter = null) | ||
{ | ||
$parameters = [ | ||
'field' => $field, | ||
'query' => $query, | ||
'range' => $range, | ||
]; | ||
|
||
if (null !== $size) { | ||
$parameters['size'] = $size; | ||
} | ||
if (null !== $filter) { | ||
$parameters['filter'] = $filter; | ||
} | ||
|
||
return $this->get('/terms', $parameters); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getApiBasePath() | ||
{ | ||
return '/search/universal/relative'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Nexylan packages. | ||
* | ||
* (c) Nexylan SAS <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nexy\Graylog\Api; | ||
|
||
use Nexy\Graylog\Api\User\Token; | ||
|
||
/** | ||
* @author Sullivan Senechal <[email protected]> | ||
*/ | ||
final class User extends AbstractApi | ||
{ | ||
public function all() | ||
{ | ||
return $this->get('/'); | ||
} | ||
|
||
public function tokens($username) | ||
{ | ||
$token = new Token($this->graylog); | ||
$token->setUsername($username); | ||
|
||
return $token; | ||
} | ||
|
||
protected function getApiBasePath() | ||
{ | ||
return '/users'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Nexylan packages. | ||
* | ||
* (c) Nexylan SAS <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Nexy\Graylog\Api\User; | ||
|
||
use Nexy\Graylog\Api\AbstractApi; | ||
|
||
/** | ||
* @author Sullivan Senechal <[email protected]> | ||
*/ | ||
final class Token extends AbstractApi | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $username; | ||
|
||
/** | ||
* @param string $username | ||
*/ | ||
public function setUsername(string $username) | ||
{ | ||
$this->username = $username; | ||
} | ||
|
||
public function all() | ||
{ | ||
return $this->get('/'); | ||
} | ||
|
||
public function create(string $name) | ||
{ | ||
return $this->post('/'.rawurldecode($name)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getApiBasePath() | ||
{ | ||
return '/users/'.rawurlencode($this->username).'/tokens'; | ||
} | ||
} |
Oops, something went wrong.