Skip to content

Commit

Permalink
some thoughts
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Dec 17, 2020
1 parent d76ac8c commit 957a369
Show file tree
Hide file tree
Showing 19 changed files with 423 additions and 322 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
},
"autoload": {
"files": [
"src/Coordinate/funcs.php"
"src/Coordinate/funcs.php",
"src/funcs.php"
],
"psr-4": {
"DCarbone\\PHPConsulAPI\\": "src/"
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 10 additions & 38 deletions src/ACL/ACLClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ACLClient extends AbstractClient
*/
public function Bootstrap(): ValuedWriteStringResponse
{
return $this->_putStrResp('v1/acl/bootstrap',null, null);
return $this->_putStrResp('v1/acl/bootstrap', null, null);
}

/**
Expand All @@ -60,12 +60,7 @@ public function Create(ACLEntry $acl, ?WriteOptions $opts = null): ValuedWriteSt
*/
public function Update(ACLEntry $acl, ?WriteOptions $opts = null): WriteResponse
{
$r = new Request(HTTP\MethodPut, 'v1/acl/update', $this->config, $acl);
$r->setWriteOptions($opts);

[$duration, $_, $err] = $this->requireOK($this->do($r));

return new WriteResponse($this->buildWriteMeta($duration), $err);
return $this->_put('v1/acl/update', $acl, $opts);
}

/**
Expand All @@ -76,12 +71,7 @@ public function Update(ACLEntry $acl, ?WriteOptions $opts = null): WriteResponse
*/
public function Destroy(string $id, ?WriteOptions $opts = null): WriteResponse
{
$r = new Request(HTTP\MethodPut, sprintf('v1/acl/destroy/%s', $id), $this->config);
$r->setWriteOptions($opts);

[$duration, $_, $err] = $this->requireOK($this->do($r));

return new WriteResponse($this->buildWriteMeta($duration), $err);
return $this->_put(sprintf('v1/acl/destroy/%s', $id), null, $opts);
}

/**
Expand All @@ -92,23 +82,7 @@ public function Destroy(string $id, ?WriteOptions $opts = null): WriteResponse
*/
public function Clone(string $id, ?WriteOptions $opts = null): ValuedWriteStringResponse
{
$r = new Request(HTTP\MethodPut, sprintf('v1/acl/clone/%s', $id), $this->config);
$r->setWriteOptions($opts);

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

$wm = $this->buildWriteMeta($duration);

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

return new ValuedWriteStringResponse($data, $wm, null);
return $this->_putStrResp(sprintf('v1/acl/clone/%s', $id), null, $opts);
}

/**
Expand All @@ -119,11 +93,11 @@ 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);
$r = new Request(HTTP\MethodGet, sprintf('v1/acl/info/%s', $id), $this->config, null);
$r->setQueryOptions($opts);

/** @var \Psr\Http\Message\ResponseInterface $response */
[$duration, $response, $err] = $this->requireOK($this->do($r));
[$duration, $response, $err] = $this->_requireOK($this->_do($r));
if (null !== $err) {
return new ACLEntriesResponse(null, null, $err);
}
Expand All @@ -145,11 +119,11 @@ 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);
$r = new Request(HTTP\MethodGet, 'v1/acl/list', $this->config, null);
$r->setQueryOptions($opts);

/** @var \Psr\Http\Message\ResponseInterface $response */
[$duration, $response, $err] = $this->requireOK($this->do($r));
[$duration, $response, $err] = $this->_requireOK($this->_do($r));
if (null !== $err) {
return new ACLEntriesResponse(null, null, $err);
}
Expand All @@ -171,11 +145,11 @@ public function List(?QueryOptions $opts = null): ACLEntriesResponse
*/
public function Replication(?QueryOptions $opts = null): ACLReplicationStatusResponse
{
$r = new Request(HTTP\MethodGet, '/v1/acl/replication', $this->config);
$r = new Request(HTTP\MethodGet, '/v1/acl/replication', $this->config, null);
$r->setQueryOptions($opts);

/** @var \Psr\Http\Message\ResponseInterface $response */
[$duration, $response, $err] = $this->requireOK($this->do($r));
[$duration, $response, $err] = $this->_requireOK($this->_do($r));
if (null !== $err) {
return new ACLReplicationStatusResponse(null, null, $err);
}
Expand All @@ -189,6 +163,4 @@ public function Replication(?QueryOptions $opts = null): ACLReplicationStatusRes

return new ACLReplicationStatusResponse($data, $qm, null);
}


}
108 changes: 82 additions & 26 deletions src/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ abstract class AbstractClient
/** @var Config */
protected $config;

/** @var \GuzzleHttp\ClientInterface */
protected $httpClient;

/**
* AbstractConsulClient constructor.
* @param Config $config
*/
public function __construct(Config $config)
{
// TODO: Clone config?
$this->config = clone $config;
}

Expand All @@ -54,10 +56,10 @@ public function getConfig(): Config

/**
* @param \DCarbone\PHPConsulAPI\RequestResponse $r
* @param int $desiredCode
* @param int $statusCode
* @return \DCarbone\PHPConsulAPI\RequestResponse
*/
protected function requireCode(RequestResponse $r, int $desiredCode): RequestResponse
protected function _requireStatus(RequestResponse $r, int $statusCode): RequestResponse
{
// If a previous error occurred, just return as-is.
if (null !== $r->Err) {
Expand All @@ -72,7 +74,7 @@ protected function requireCode(RequestResponse $r, int $desiredCode): RequestRes
$actualCode = $r->Response->getStatusCode();

// If $desiredCode, move right along
if ($desiredCode === $actualCode) {
if ($statusCode === $actualCode) {
return $r;
}

Expand All @@ -81,7 +83,7 @@ protected function requireCode(RequestResponse $r, int $desiredCode): RequestRes
sprintf(
'%s - Non-%d response seen. Response code: %d. Message: %s',
get_class($this),
$desiredCode,
$statusCode,
$actualCode,
$r->Response->getReasonPhrase()
)
Expand All @@ -100,21 +102,12 @@ protected function requireCode(RequestResponse $r, int $desiredCode): RequestRes
return $r;
}

/**
* @param \DCarbone\PHPConsulAPI\RequestResponse $r
* @return \DCarbone\PHPConsulAPI\RequestResponse
*/
protected function requireOK(RequestResponse $r): RequestResponse
{
return $this->requireCode($r, HTTP\StatusOK);
}

/**
* @param \DCarbone\PHPConsulAPI\Request $r
* @return \DCarbone\PHPConsulAPI\RequestResponse
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function do(Request $r): RequestResponse
protected function _do(Request $r): RequestResponse
{
$start = microtime(true);
$response = null;
Expand Down Expand Up @@ -143,8 +136,19 @@ protected function do(Request $r): RequestResponse
);
}

// Calculate duration and move along whatever response and error we see (if any)
return new RequestResponse(Time::Duration(intval((microtime(true) - $start) * Time::Second)), $response, $err);
// calculate execution time
$dur = new Time\Duration(intval((microtime(true) - $start) * Time::Second));

return new RequestResponse($dur, $response, $err);
}

/**
* @param \DCarbone\PHPConsulAPI\RequestResponse $r
* @return \DCarbone\PHPConsulAPI\RequestResponse
*/
protected function _requireOK(RequestResponse $r): RequestResponse
{
return $this->_requireStatus($r, HTTP\StatusOK);
}

/**
Expand Down Expand Up @@ -222,6 +226,59 @@ protected function decodeBody(StreamInterface $body): DecodedBody
);
}

/**
* @param string $path
* @param mixed $body
* @param \DCarbone\PHPConsulAPI\WriteOptions|null $opts
* @return \DCarbone\PHPConsulAPI\Request
*/
protected function _writeRequest(string $path, $body, ?WriteOptions $opts): Request
{
$r = new Request(HTTP\MethodPut, $path, $this->config, $body);
$r->setWriteOptions($opts);
return $r;
}

/**
* @param string $path
* @param $body
* @param \DCarbone\PHPConsulAPI\QueryOptions|null $opts
* @return \DCarbone\PHPConsulAPI\Request
*/
protected function _queryRequest(string $path, $body, ?QueryOptions $opts): Request
{
$r = new Request(HTTP\MethodGet, $path, $this->config, $body);
$r->setQueryOptions($opts);
return $r;
}

/**
* @param string $path
* @param mixed $body
* @param \DCarbone\PHPConsulAPI\WriteOptions|null $opts
* @return \DCarbone\PHPConsulAPI\WriteResponse
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function _put(string $path, $body, ?WriteOptions $opts): WriteResponse
{
$resp = $this->_requireOK($this->_do($this->_writeRequest($path, $body, $opts)));
return new WriteResponse($this->buildWriteMeta($resp->Duration), $resp->Err);
}

/**
* @param string $path
* @param mixed $body
* @param \DCarbone\PHPConsulAPI\WriteOptions|null $opts
* @return \DCarbone\PHPConsulAPI\Error|null
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function _putNoResp(string $path, $body, ?WriteOptions $opts): ?Error
{
$r = new Request(HTTP\MethodPut, $path, $this->config, $body);
$r->setWriteOptions($opts);
return $this->_requireOK($this->_do($r))->Err;
}

/**
* @param string $path
* @param mixed $body
Expand All @@ -234,19 +291,18 @@ protected function _putStrResp(string $path, $body, ?WriteOptions $opts): Valued
$r = new Request(HTTP\MethodPut, $path, $this->config, $body);
$r->setWriteOptions($opts);

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

[$data, $err] = $this->decodeBody($response->getBody());
if (null !== $err) {
return new ValuedWriteStringResponse('', null, $err);
$decoded = $this->decodeBody($resp->Response->getBody());
if (null !== $decoded->Err) {
return new ValuedWriteStringResponse('', null, $decoded->Err);
}

$wm = $this->buildWriteMeta($duration);
$wm = $this->buildWriteMeta($resp->Duration);

return new ValuedWriteStringResponse($data, $wm, null);
return new ValuedWriteStringResponse($decoded->Decoded, $wm, null);
}
}
Loading

0 comments on commit 957a369

Please sign in to comment.