Skip to content

Commit

Permalink
streamlined query and response handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Feb 8, 2021
1 parent 30ea3ad commit 3f2834b
Show file tree
Hide file tree
Showing 63 changed files with 1,026 additions and 1,658 deletions.
65 changes: 12 additions & 53 deletions src/ACL/ACLClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
limitations under the License.
*/

use DCarbone\Go\HTTP;
use DCarbone\PHPConsulAPI\AbstractClient;
use DCarbone\PHPConsulAPI\QueryOptions;
use DCarbone\PHPConsulAPI\Request;
use DCarbone\PHPConsulAPI\ValuedWriteStringResponse;
use DCarbone\PHPConsulAPI\WriteOptions;
use DCarbone\PHPConsulAPI\WriteResponse;
Expand Down Expand Up @@ -92,23 +90,10 @@ public function Clone(string $id, ?WriteOptions $opts = null): ValuedWriteString
*/
public function Info(string $id, ?QueryOptions $opts = null): ACLEntriesResponse
{
$r = new Request(HTTP\MethodGet, \sprintf('v1/acl/info/%s', $id), $this->config, null);
$r->applyOptions($opts);

/** @var \Psr\Http\Message\ResponseInterface $response */
[$duration, $response, $err] = $this->_requireOK($this->_do($r));
if (null !== $err) {
return new ACLEntriesResponse(null, null, $err);
}

$qm = $this->buildQueryMeta($duration, $response, $r->getUri());

[$data, $err] = $this->decodeBody($response->getBody());
if (null !== $err) {
return new ACLEntriesResponse(null, $qm, $err);
}

return new ACLEntriesResponse($data, $qm, null);
$resp = $this->_doGet(\sprintf('v1/acl/info/%s', $id), $opts);
$ret = new ACLEntriesResponse();
$this->_hydrateResponse($resp, $ret);
return $ret;
}

/**
Expand All @@ -118,23 +103,10 @@ public function Info(string $id, ?QueryOptions $opts = null): ACLEntriesResponse
*/
public function List(?QueryOptions $opts = null): ACLEntriesResponse
{
$r = new Request(HTTP\MethodGet, 'v1/acl/list', $this->config, null);
$r->applyOptions($opts);

/** @var \Psr\Http\Message\ResponseInterface $response */
[$duration, $response, $err] = $this->_requireOK($this->_do($r));
if (null !== $err) {
return new ACLEntriesResponse(null, null, $err);
}

$qm = $this->buildQueryMeta($duration, $response, $r->getUri());

[$data, $err] = $this->decodeBody($response->getBody());
if (null !== $err) {
return new ACLEntriesResponse(null, $qm, $err);
}

return new ACLEntriesResponse($data, $qm, null);
$resp = $this->_doGet('v1/acl/list', $opts);
$ret = new ACLEntriesResponse();
$this->_hydrateResponse($resp, $ret);
return $ret;
}

/**
Expand All @@ -144,22 +116,9 @@ public function List(?QueryOptions $opts = null): ACLEntriesResponse
*/
public function Replication(?QueryOptions $opts = null): ACLReplicationStatusResponse
{
$r = new Request(HTTP\MethodGet, '/v1/acl/replication', $this->config, null);
$r->applyOptions($opts);

/** @var \Psr\Http\Message\ResponseInterface $response */
[$duration, $response, $err] = $this->_requireOK($this->_do($r));
if (null !== $err) {
return new ACLReplicationStatusResponse(null, null, $err);
}

$qm = $this->buildQueryMeta($duration, $response, $r->getUri());

[$data, $err] = $this->decodeBody($response->getBody());
if (null !== $err) {
return new ACLReplicationStatusResponse(null, $qm, $err);
}

return new ACLReplicationStatusResponse($data, $qm, null);
$resp = $this->_doGet('/v1/acl/replication', $opts);
$ret = new ACLReplicationStatusResponse();
$this->_hydrateResponse($resp, $ret);
return $ret;
}
}
35 changes: 17 additions & 18 deletions src/ACL/ACLEntriesResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,38 @@
limitations under the License.
*/

use DCarbone\PHPConsulAPI\AbstractValuedQueryResponse;
use DCarbone\PHPConsulAPI\Error;
use DCarbone\PHPConsulAPI\QueryMeta;
use DCarbone\PHPConsulAPI\AbstractResponse;
use DCarbone\PHPConsulAPI\ErrorContainer;
use DCarbone\PHPConsulAPI\QueryMetaContainer;
use DCarbone\PHPConsulAPI\HydratedResponseInterface;

/**
* Class ACLEntriesResponse
*/
class ACLEntriesResponse extends AbstractValuedQueryResponse
class ACLEntriesResponse extends AbstractResponse implements HydratedResponseInterface
{
use QueryMetaContainer;
use ErrorContainer;

/** @var \DCarbone\PHPConsulAPI\ACL\ACLEntry[]|null */
public array $ACLEntries = [];

/**
* ACLEntriesResponse constructor.
* @param array|null $entries
* @param \DCarbone\PHPConsulAPI\QueryMeta|null $qm
* @param \DCarbone\PHPConsulAPI\Error|null $err
* @return \DCarbone\PHPConsulAPI\ACL\ACLEntry[]|null
*/
public function __construct(?array $entries, ?QueryMeta $qm, ?Error $err)
public function getValue(): ?array
{
if (null !== $entries) {
foreach ($entries as $entry) {
$this->ACLEntries[] = new ACLEntry($entry);
}
}
parent::__construct($qm, $err);
return $this->ACLEntries;
}

/**
* @return \DCarbone\PHPConsulAPI\ACL\ACLEntry[]|null
* @param mixed $decodedData
* @return void
*/
public function getValue(): ?array
public function hydrateValue($decodedData): void
{
return $this->ACLEntries;
foreach ($decodedData as $entry) {
$this->ACLEntries[] = new ACLEntry($entry);
}
}
}
31 changes: 15 additions & 16 deletions src/ACL/ACLReplicationStatusResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,36 @@
limitations under the License.
*/

use DCarbone\PHPConsulAPI\AbstractValuedQueryResponse;
use DCarbone\PHPConsulAPI\Error;
use DCarbone\PHPConsulAPI\QueryMeta;
use DCarbone\PHPConsulAPI\AbstractResponse;
use DCarbone\PHPConsulAPI\ErrorContainer;
use DCarbone\PHPConsulAPI\QueryMetaContainer;
use DCarbone\PHPConsulAPI\HydratedResponseInterface;

/**
* Class ACLReplicationStatusResponse
*/
class ACLReplicationStatusResponse extends AbstractValuedQueryResponse
class ACLReplicationStatusResponse extends AbstractResponse implements HydratedResponseInterface
{
use QueryMetaContainer;
use ErrorContainer;

/** @var \DCarbone\PHPConsulAPI\ACL\ACLReplicationStatus|null */
public ?ACLReplicationStatus $ACLReplicationStatus = null;

/**
* ACLReplicationStatusResponse constructor.
* @param array|null $status
* @param \DCarbone\PHPConsulAPI\QueryMeta|null $qm
* @param \DCarbone\PHPConsulAPI\Error|null $err
* @return \DCarbone\PHPConsulAPI\ACL\ACLReplicationStatus|null
*/
public function __construct(?array $status, ?QueryMeta $qm, ?Error $err)
public function getValue(): ?ACLReplicationStatus
{
if (null !== $status) {
$this->ACLReplicationStatus = new ACLReplicationStatus($status);
}
parent::__construct($qm, $err);
return $this->ACLReplicationStatus;
}

/**
* @return \DCarbone\PHPConsulAPI\ACL\ACLReplicationStatus|null
* @param mixed $decodedData
* @return void
*/
public function getValue(): ?ACLReplicationStatus
public function hydrateValue($decodedData): void
{
return $this->ACLReplicationStatus;
$this->ACLReplicationStatus = new ACLReplicationStatus((array)$decodedData);
}
}
Loading

0 comments on commit 3f2834b

Please sign in to comment.