Skip to content

Commit

Permalink
lots of fixes and beginning to update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Dec 15, 2020
1 parent 9f701ba commit 1dcfdc2
Show file tree
Hide file tree
Showing 21 changed files with 321 additions and 153 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"php-consul-api"
],
"require": {
"php": "^7.2",
"php": ">=7.2",
"ext-json": "*",
"dcarbone/gotime": "0.4.*",
"dcarbone/gohttp": "v0.3.*",
Expand All @@ -36,7 +36,8 @@
},
"autoload-dev": {
"files": [
"src/Coordinate/funcs.php"
"src/Coordinate/funcs.php",
"tests/funcs.php"
],
"psr-4": {
"DCarbone\\PHPConsulAPI\\": "src/",
Expand Down
16 changes: 8 additions & 8 deletions src/ACL/ACLClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function Bootstrap(): ValuedWriteStringResponse
$r = new Request('PUT', 'v1/acl/bootstrap', $this->config);

/** @var \Psr\Http\Message\ResponseInterface $response */
[$duration, $response, $err] = $this->requireOK($this->doRequest($r));
[$duration, $response, $err] = $this->requireOK($this->do($r));
if (null !== $err) {
return new ValuedWriteStringResponse('', null, $err);
}
Expand All @@ -67,7 +67,7 @@ public function Create(ACLEntry $acl, WriteOptions $opts = null): ValuedWriteStr
$r->setWriteOptions($opts);

/** @var \Psr\Http\Message\ResponseInterface $response */
[$duration, $response, $err] = $this->requireOK($this->doRequest($r));
[$duration, $response, $err] = $this->requireOK($this->do($r));
if (null !== $err) {
return new ValuedWriteStringResponse('', null, $err);
}
Expand All @@ -93,7 +93,7 @@ public function Update(ACLEntry $acl, WriteOptions $opts = null): WriteResponse
$r = new Request('PUT', 'v1/acl/update', $this->config, $acl);
$r->setWriteOptions($opts);

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

return new WriteResponse($this->buildWriteMeta($duration), $err);
}
Expand All @@ -109,7 +109,7 @@ public function Destroy(string $id, WriteOptions $opts = null): WriteResponse
$r = new Request('PUT', sprintf('v1/acl/destroy/%s', $id), $this->config);
$r->setWriteOptions($opts);

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

return new WriteResponse($this->buildWriteMeta($duration), $err);
}
Expand All @@ -126,7 +126,7 @@ public function Clone(string $id, WriteOptions $opts = null): ValuedWriteStringR
$r->setWriteOptions($opts);

/** @var \Psr\Http\Message\ResponseInterface $response */
[$duration, $response, $err] = $this->requireOK($this->doRequest($r));
[$duration, $response, $err] = $this->requireOK($this->do($r));
if (null !== $err) {
return new ValuedWriteStringResponse('', null, $err);
}
Expand All @@ -153,7 +153,7 @@ public function Info(string $id, QueryOptions $opts = null): ACLEntriesResponse
$r->setQueryOptions($opts);

/** @var \Psr\Http\Message\ResponseInterface $response */
[$duration, $response, $err] = $this->requireOK($this->doRequest($r));
[$duration, $response, $err] = $this->requireOK($this->do($r));
if (null !== $err) {
return new ACLEntriesResponse(null, null, $err);
}
Expand All @@ -179,7 +179,7 @@ public function List(QueryOptions $opts = null): ACLEntriesResponse
$r->setQueryOptions($opts);

/** @var \Psr\Http\Message\ResponseInterface $response */
[$duration, $response, $err] = $this->requireOK($this->doRequest($r));
[$duration, $response, $err] = $this->requireOK($this->do($r));
if (null !== $err) {
return new ACLEntriesResponse(null, null, $err);
}
Expand All @@ -205,7 +205,7 @@ public function Replication(QueryOptions $opts = null): ACLReplicationStatusResp
$r->setQueryOptions($opts);

/** @var \Psr\Http\Message\ResponseInterface $response */
[$duration, $response, $err] = $this->requireOK($this->doRequest($r));
[$duration, $response, $err] = $this->requireOK($this->do($r));
if (null !== $err) {
return new ACLReplicationStatusResponse(null, null, $err);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ACL/ACLEntriesResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct(?array $entries, ?QueryMeta $qm, ?Error $err)
/**
* @return \DCarbone\PHPConsulAPI\ACL\ACLEntry[]|null
*/
public function getValue()
public function getValue(): ?array
{
return $this->ACLEntries;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ACL/ACLReplicationStatusResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(?array $status, ?QueryMeta $qm, ?Error $err)
/**
* @return \DCarbone\PHPConsulAPI\ACL\ACLReplicationStatus|null
*/
public function getValue()
public function getValue(): ?ACLReplicationStatus
{
return $this->ACLReplicationStatus;
}
Expand Down
56 changes: 37 additions & 19 deletions src/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
limitations under the License.
*/

use DCarbone\Go\HTTP;
use DCarbone\Go\Time;
use GuzzleHttp\ClientInterface;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -30,6 +31,14 @@
*/
abstract class AbstractClient
{
private const headerConsulPrefix = 'X-Consul-';
private const headerConsulIndex = self::headerConsulPrefix . 'Index';
private const headerConsulContentHash = self::headerConsulPrefix . 'ContentHash';
private const headerConsulKnownLeader = self::headerConsulPrefix . 'KnownLeader';
private const headerConsulLastContact = self::headerConsulPrefix . 'LastContact';
private const headerConsulTranslateAddresses = self::headerConsulPrefix . 'Translate-Addresses';
private const headerCache = 'X-Cache';

/** @var Config */
protected $config;

Expand All @@ -53,9 +62,10 @@ public function getConfig(): Config

/**
* @param \DCarbone\PHPConsulAPI\RequestResponse $r
* @param int $desiredCode
* @return \DCarbone\PHPConsulAPI\RequestResponse
*/
protected function requireOK(RequestResponse $r): RequestResponse
protected function requireCode(RequestResponse $r, int $desiredCode): RequestResponse
{
// If a previous error occurred, just return as-is.
if (null !== $r->Err) {
Expand All @@ -67,19 +77,20 @@ protected function requireOK(RequestResponse $r): RequestResponse
// If this is a response...
if ($r->Response instanceof ResponseInterface) {
// Get the response code...
$code = $r->Response->getStatusCode();
$actualCode = $r->Response->getStatusCode();

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

// Otherwise, return error
$r->Err = new Error(
sprintf(
'%s - Non-200 response seen. Response code: %d. Message: %s',
'%s - Non-%d response seen. Response code: %d. Message: %s',
get_class($this),
$code,
$desiredCode,
$actualCode,
$r->Response->getReasonPhrase()
)
);
Expand All @@ -97,12 +108,21 @@ protected function requireOK(RequestResponse $r): RequestResponse
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 doRequest(Request $r): RequestResponse
protected function do(Request $r): RequestResponse
{
$rt = microtime(true);
$response = null;
Expand Down Expand Up @@ -145,35 +165,33 @@ protected function buildQueryMeta(
Time\Duration $duration,
ResponseInterface $response,
UriInterface $uri
): QueryMeta {
): QueryMeta
{
$qm = new QueryMeta();

$qm->RequestTime = $duration;
$qm->RequestUrl = (string)$uri;

if ('' !== ($h = $response->getHeaderLine('X-Consul-Index'))) {
if ('' !== ($h = $response->getHeaderLine(self::headerConsulIndex))) {
$qm->LastIndex = (int)$h;
}

$qm->LastContentHash = $response->getHeaderLine('X-Consul-ContentHash');
$qm->LastContentHash = $response->getHeaderLine(self::headerConsulContentHash);

if ('' !== ($h = $response->getHeaderLine('X-Consul-KnownLeader'))) {
$qm->KnownLeader = (bool)$h;
} elseif ('' !== ($h = $response->getHeaderLine('X-Consul-Knownleader'))) {
// note: do not need to check both as guzzle response compares headers insensitively
if ('' !== ($h = $response->getHeaderLine(self::headerConsulKnownLeader))) {
$qm->KnownLeader = (bool)$h;
}

if ('' !== ($h = $response->getHeaderLine('X-Consul-LastContact'))) {
$qm->LastContact = (int)$h;
} elseif ('' !== ($h = $response->getHeaderLine('X-Consul-Lastcontact'))) {
// note: do not need to check both as guzzle response compares headers insensitively
if ('' !== ($h = $response->getHeaderLine(self::headerConsulLastContact))) {
$qm->LastContact = (int)$h;
}

if ('' !== ($h = $response->getHeaderLine('X-Consul-Translate-Addresses'))) {
if ('' !== ($h = $response->getHeaderLine(self::headerConsulTranslateAddresses))) {
$qm->AddressTranslationEnabled = (bool)$h;
}

if ('' !== ($h = $response->getHeaderLine('X-Cache'))) {
if ('' !== ($h = $response->getHeaderLine(self::headerCache))) {
$qm->CacheAge = Time::Duration(intval($h, 10) * Time::Second);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Agent/AgentChecksResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(?array $checks, ?Error $err)
/**
* @return \DCarbone\PHPConsulAPI\Agent\AgentCheck[]|null
*/
public function getValue()
public function getValue(): ?array
{
return $this->Checks;
}
Expand Down
Loading

0 comments on commit 1dcfdc2

Please sign in to comment.