Skip to content

Commit

Permalink
some internal api cleanup and adding DebugClient
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jun 1, 2022
1 parent 9ab48e7 commit 8ba6e23
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 38 deletions.
6 changes: 3 additions & 3 deletions src/ACL/ACLClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ACLClient extends AbstractClient
*/
public function Bootstrap(): ValuedWriteStringResponse
{
return $this->_doPutValuedStr('v1/acl/bootstrap', null, null);
return $this->_executePutValuedStr('v1/acl/bootstrap', null, null);
}

/**
Expand All @@ -47,7 +47,7 @@ public function Bootstrap(): ValuedWriteStringResponse
*/
public function Create(ACLEntry $acl, ?WriteOptions $opts = null): ValuedWriteStringResponse
{
return $this->_doPutValuedStr('v1/acl/create', $acl, $opts);
return $this->_executePutValuedStr('v1/acl/create', $acl, $opts);
}

/**
Expand Down Expand Up @@ -80,7 +80,7 @@ public function Destroy(string $id, ?WriteOptions $opts = null): WriteResponse
*/
public function Clone(string $id, ?WriteOptions $opts = null): ValuedWriteStringResponse
{
return $this->_doPutValuedStr(sprintf('v1/acl/clone/%s', $id), null, $opts);
return $this->_executePutValuedStr(sprintf('v1/acl/clone/%s', $id), null, $opts);
}

/**
Expand Down
76 changes: 41 additions & 35 deletions src/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,39 +198,44 @@ protected function _requireStatus(RequestResponse $r, int ...$allowed): RequestR
return $r;
}

// If we have any kind of response...
if (null !== $r->Response) {
// If this is a response...
if ($r->Response instanceof ResponseInterface) {
// Get the response code...
$actualCode = $r->Response->getStatusCode();

// If response code is in allowed list, move right along
if (\in_array($actualCode, $allowed, true)) {
return $r;
}

// Otherwise, return error
$r->Err = new Error(
sprintf(
'%s - Non-%d response seen. Response code: %d. Response: %s',
static::class,
$allowed,
$actualCode,
$r->Response->getBody()->getContents()
)
);
} else {
$r->Err = new Error(
sprintf(
'%s - Expected response to be instance of \\Psr\\Message\\ResponseInterface, %s seen.',
static::class,
\is_object($r->Response) ? \get_class($r->Response) : \gettype($r->Response)
)
);
}
// if no response, return immediately
if (null === $r->Response) {
return $r;
}

// if, for whatever reason, we see an unexpected response structure...
if (!($r->Response instanceof ResponseInterface)) {
$r->Err = new Error(
sprintf(
'%s - Expected response to be instance of \\Psr\\Message\\ResponseInterface, %s seen.',
static::class,
\is_object($r->Response) ? \get_class($r->Response) : \gettype($r->Response)
)
);
return $r;
}

// once here, assume operable response instance

// Get the response code...
$actualCode = $r->Response->getStatusCode();

// If response code is in allowed list, move right along
if (\in_array($actualCode, $allowed, true)) {
return $r;
}

// Otherwise, return error
$r->Err = new Error(
sprintf(
'%s - Non-%d response seen. Response code: %d. Response: %s',
static::class,
$allowed,
$actualCode,
$r->Response->getBody()->getContents()
)
);

return $r;
}

Expand Down Expand Up @@ -380,7 +385,7 @@ protected function _executeDelete(string $path, ?WriteOptions $opts): WriteRespo
* @throws \Exception
* @return \DCarbone\PHPConsulAPI\ValuedWriteStringResponse
*/
protected function _doPutValuedStr(string $path, $body, ?WriteOptions $opts): ValuedWriteStringResponse
protected function _executePutValuedStr(string $path, $body, ?WriteOptions $opts): ValuedWriteStringResponse
{
$r = $this->_newPutRequest($path, $body, $opts);
$resp = $this->_requireOK($this->_do($r));
Expand All @@ -396,7 +401,7 @@ protected function _doPutValuedStr(string $path, $body, ?WriteOptions $opts): Va
* @throws \Exception
* @return \DCarbone\PHPConsulAPI\ValuedQueryStringResponse
*/
protected function _doGetValuedStr(string $path, ?QueryOptions $opts): ValuedQueryStringResponse
protected function _executeGetValuedStr(string $path, ?QueryOptions $opts): ValuedQueryStringResponse
{
$r = $this->_newGetRequest($path, $opts);
$resp = $this->_requireOK($this->_do($r));
Expand All @@ -412,7 +417,7 @@ protected function _doGetValuedStr(string $path, ?QueryOptions $opts): ValuedQue
* @throws \Exception
* @return \DCarbone\PHPConsulAPI\ValuedQueryStringsResponse
*/
protected function _doGetValuedStrs(string $path, ?QueryOptions $opts): ValuedQueryStringsResponse
protected function _executeGetValuedStrs(string $path, ?QueryOptions $opts): ValuedQueryStringsResponse
{
$r = $this->_newGetRequest($path, $opts);
$resp = $this->_requireOK($this->_do($r));
Expand All @@ -431,6 +436,7 @@ protected function _doGetValuedStrs(string $path, ?QueryOptions $opts): ValuedQu
protected function _unmarshalResponse(RequestResponse $resp, AbstractResponse $ret): void
{
// determine if this response contains a *Meta field
// TODO: change to use interfaces + instanceof?
if (property_exists($ret, Transcoding::FIELD_QUERY_META)) {
$ret->QueryMeta = $resp->buildQueryMeta();
} elseif (property_exists($ret, Transcoding::FIELD_WRITE_META)) {
Expand All @@ -453,7 +459,7 @@ protected function _unmarshalResponse(RequestResponse $resp, AbstractResponse $r
return;
}

// attempt response decoded
// attempt response decode
$dec = $this->_decodeBody($resp->Response->getBody());
if (null !== $dec->Err) {
if ($hasErrField) {
Expand Down
118 changes: 118 additions & 0 deletions src/Debug/DebugClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php declare(strict_types=1);

namespace DCarbone\PHPConsulAPI\Debug;

/*
Copyright 2016-2021 Daniel Carbone ([email protected])
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

use DCarbone\Go\HTTP;
use DCarbone\PHPConsulAPI\AbstractClient;
use DCarbone\PHPConsulAPI\Error;
use DCarbone\PHPConsulAPI\ValuedStringResponse;

/**
* Class DebugClient
*/
class DebugClient extends AbstractClient
{
/**
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Exception
* @return \DCarbone\PHPConsulAPI\ValuedStringResponse
*/
public function Heap(): ValuedStringResponse
{
$ret = new ValuedStringResponse();
$resp = $this->_doGet('/debug/pprof/heap', null);
if (null !== $resp->Err) {
$ret->Err = $resp->Err;
return $ret;
}
if (HTTP\StatusOK !== $resp->Response->getStatusCode()) {
$ret->Err = Error::unexpectedResponseCodeError($resp);
}
$this->_unmarshalResponse($resp, $ret);
return $ret;
}

/**
* @param int $seconds
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Exception
* @return \DCarbone\PHPConsulAPI\ValuedStringResponse
*/
public function Profile(int $seconds): ValuedStringResponse
{
$ret = new ValuedStringResponse();
$req = $this->_newGetRequest('/debug/pprof/profile', null);
$req->params->set('seconds', (string)$seconds);
$resp = $this->_do($req);
if (null !== $resp->Err) {
$ret->Err = $resp->Err;
return $ret;
}
if (HTTP\StatusOK !== $resp->Response->getStatusCode()) {
$ret->Err = Error::unexpectedResponseCodeError($resp);
return $ret;
}
$this->_unmarshalResponse($resp, $ret);
return $ret;
}

/**
* @param int $seconds
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Exception
* @return \DCarbone\PHPConsulAPI\ValuedStringResponse
*/
public function Trace(int $seconds): ValuedStringResponse
{
$ret = new ValuedStringResponse();
$req = $this->_newGetRequest('/debug/pprof/trace', null);
$req->params->set('seconds', (string)$seconds);
$resp = $this->_do($req);
if (null !== $resp->Err) {
$ret->Err = $resp->Err;
return $ret;
}
if (HTTP\StatusOK !== $resp->Response->getStatusCode()) {
$ret->Err = Error::unexpectedResponseCodeError($resp);
return $ret;
}
$this->_unmarshalResponse($resp, $ret);
return $ret;
}

/**
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Exception
* @return \DCarbone\PHPConsulAPI\ValuedStringResponse
*/
public function Goroutine(): ValuedStringResponse
{
$ret = new ValuedStringResponse();
$resp = $this->_doGet('/debug/pprof/goroutine', null);
if (null !== $resp->Err) {
$ret->Err = $resp->Err;
return $ret;
}
if (HTTP\StatusOK !== $resp->Response->getStatusCode()) {
$ret->Err = Error::unexpectedResponseCodeError($resp);
}
$this->_unmarshalResponse($resp, $ret);
return $ret;
}
}
17 changes: 17 additions & 0 deletions src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,23 @@ public function __construct(string $message)
$this->message = $message;
}

/**
* @param \DCarbone\PHPConsulAPI\RequestResponse $resp
* @throws \Exception
* @return \DCarbone\PHPConsulAPI\Error
*/
public static function unexpectedResponseCodeError(RequestResponse $resp): self
{
return new static(
sprintf(
'unexpected response code: %d (%s)',
$resp->Response->getStatusCode(),
$resp->Response->getBody()->getContents(),
)

);
}

/**
* @return \DCarbone\Go\Time\Time
*/
Expand Down

0 comments on commit 8ba6e23

Please sign in to comment.