Skip to content
This repository has been archived by the owner on Nov 24, 2020. It is now read-only.

Commit

Permalink
Client setup with some paths (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
soullivaneuh authored Nov 10, 2016
1 parent 91e1591 commit 51aade2
Show file tree
Hide file tree
Showing 12 changed files with 695 additions and 2 deletions.
22 changes: 20 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,29 @@
}
],
"require": {
"php": "^7.0"
"php": "^7.0",
"php-http/client-common": "^1.4",
"php-http/client-implementation": "^1.0",
"php-http/discovery": "^1.0",
"php-http/httplug": "^1.1",
"symfony/options-resolver": "^2.8 || ^3.0"
},
"require-dev": {
"sllh/php-cs-fixer-styleci-bridge": "^2.1"
"guzzlehttp/psr7": "^1.3",
"php-http/guzzle6-adapter": "^1.1",
"php-http/message": "^1.3",
"sllh/php-cs-fixer-styleci-bridge": "^2.1",
"symfony/dependency-injection": "^2.8 || ^3.0",
"symfony/http-kernel": "^2.8 || ^3.0"
},
"suggest": {
"ext-solr": "For Lucene query escaping.",
"internations/solr-utils": "For Lucene query escaping.",
"symfony/http-kernel": "For Symfony integration as a bundle",
"symfony/dependency-injection": "For Symfony integration as a bundle"
},
"minimum-stability": "beta",
"prefer-stable": true,
"config": {
"sort-packages": true
},
Expand Down
78 changes: 78 additions & 0 deletions src/Api/AbstractApi.php
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);
}
}
45 changes: 45 additions & 0 deletions src/Api/Search.php
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';
}
}
67 changes: 67 additions & 0 deletions src/Api/Search/Keyword.php
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';
}
}
57 changes: 57 additions & 0 deletions src/Api/Search/Relative.php
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';
}
}
40 changes: 40 additions & 0 deletions src/Api/User.php
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';
}
}
53 changes: 53 additions & 0 deletions src/Api/User/Token.php
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';
}
}
Loading

0 comments on commit 51aade2

Please sign in to comment.