Skip to content

Commit

Permalink
Merge pull request #9 from Manlinup/master
Browse files Browse the repository at this point in the history
Readme
  • Loading branch information
kiss291323003 authored Sep 10, 2019
2 parents 8351f4a + fb6a0af commit dc0e343
Show file tree
Hide file tree
Showing 16 changed files with 1,687 additions and 598 deletions.
1,150 changes: 1,135 additions & 15 deletions README.md

Large diffs are not rendered by default.

31 changes: 8 additions & 23 deletions src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
namespace EasySwoole\Consul;

use EasySwoole\Consul\Exception\MissingRequiredParamsException;
use EasySwoole\Consul\Exception\WrongRequiredParamsException;
use EasySwoole\Consul\Exception\Exception;
use EasySwoole\Consul\Exception\InvalidRequiredParamsException;
use EasySwoole\Consul\Request\Agent\Check\Deregister;
use EasySwoole\Consul\Request\Agent\Check\Fail;
use EasySwoole\Consul\Request\Agent\Check\Pass;
Expand All @@ -14,7 +13,6 @@
use EasySwoole\Consul\Request\Agent\Connect\Authorize;
use EasySwoole\Consul\Request\Agent\Connect\Ca\Leaf;
use EasySwoole\Consul\Request\Agent\Connect\Ca\Roots;
use EasySwoole\Consul\Request\Agent\Connect\Proxy;
use EasySwoole\Consul\Request\Agent\ForceLeave;
use EasySwoole\Consul\Request\Agent\Health\Service\ID;
use EasySwoole\Consul\Request\Agent\Health\Service\Name;
Expand Down Expand Up @@ -73,7 +71,7 @@ public function maintenance(Maintenance $maintenance)
if (empty($maintenance->getEnable())) {
throw new MissingRequiredParamsException('Missing the required param: enable.');
}
$this->putJSON($maintenance);
$this->putJSON($maintenance , false);
}

/**
Expand Down Expand Up @@ -145,7 +143,7 @@ public function forceLeave(ForceLeave $forceLeave)
* Update ACL Tokens
* @param Token $token
* @throws MissingRequiredParamsException
* @throws WrongRequiredParamsException
* @throws InvalidRequiredParamsException
* @throws \EasySwoole\HttpClient\Exception\InvalidUrl
*/
public function token(Token $token)
Expand All @@ -164,7 +162,7 @@ public function token(Token $token)
throw new MissingRequiredParamsException('Missing the required param: action');
}
if (! in_array(strtolower(trim($token->getAction())), $actionArr)) {
throw new WrongRequiredParamsException('Wrong required param: action');
throw new InvalidRequiredParamsException('Wrong required param: action');
}
$token->setUrl(sprintf($token->getUrl(), $token->getAction()));
$token->setAction('');
Expand Down Expand Up @@ -192,6 +190,9 @@ public function register(Register $register)
if (empty($register->getName())) {
throw new MissingRequiredParamsException('Missing the required param: Name.');
}
if (empty($register->getTTL())) {
throw new MissingRequiredParamsException('Missing the required param: TTL.');
}
$this->putJSON($register);
}

Expand Down Expand Up @@ -374,7 +375,7 @@ public function serviceMaintenance(Service\Maintenance $maintenance)
if (empty($maintenance->getServiceID())) {
throw new MissingRequiredParamsException('Missing the required param: service_id.');
}
if (empty($maintenance->getEnable())) {
if ($maintenance->getEnable() === '' || $maintenance->getEnable() === null) {
throw new MissingRequiredParamsException('Missing the required param: enable.');
}
$maintenance->setUrl(sprintf($maintenance->getUrl(), $maintenance->getServiceID()));
Expand Down Expand Up @@ -427,20 +428,4 @@ public function leaf(Leaf $leaf)
$leaf->setService('');
$this->getJson($leaf);
}

/**
* Managed Proxy Configuration (Deprecated)
* @param Proxy $proxy
* @throws MissingRequiredParamsException
* @throws \EasySwoole\HttpClient\Exception\InvalidUrl
*/
public function proxy(Proxy $proxy)
{
if (empty($proxy->getId())) {
throw new MissingRequiredParamsException('Missing the required param: ID.');
}
$proxy->setUrl(sprintf($proxy->getUrl(), $proxy->getId()));
$proxy->setId('');
$this->getJson($proxy);
}
}
71 changes: 51 additions & 20 deletions src/BaseFunc.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php
namespace EasySwoole\Consul;

use EasySwoole\Consul\Exception\Exception;
use EasySwoole\Consul\Request\BaseCommand;
use EasySwoole\HttpClient\Bean\Response;
use EasySwoole\HttpClient\Exception\InvalidUrl;
use EasySwoole\HttpClient\HttpClient;
use EasySwoole\Spl\SplBean;

class BaseFunc
{
Expand All @@ -25,20 +26,24 @@ public function __construct(Config $config)

/**
* @param BaseCommand $bean
* @return mixed|null
* @param bool $isJsonParams
* @return mixed
* @throws InvalidUrl
* @throws \Exception
*/
protected function putJSON(BaseCommand $bean)
protected function putJSON(BaseCommand $bean, $isJsonParams = true)
{
$url = $this->getUrl($bean);
$data = $this->toRequestJson($bean);
$http = new HttpClient($url);
if (! $isJsonParams) {
$data = $this->toRequestParam($bean);
$url = $data ? $url . '?' . $data : $url;
}

$http = new HttpClient($url);
if ($http) {
try{
$ret = $http->put($data)->getBody();
return !empty($ret) ? json_decode($ret, true): null;
return $this->checkResponse($http->put($data));
} catch (\Exception $exception) {
throw new \Exception($exception->getMessage());
}
Expand All @@ -50,7 +55,7 @@ protected function putJSON(BaseCommand $bean)
/**
* @param BaseCommand $bean
* @param array $headers
* @return mixed|null
* @return mixed
* @throws InvalidUrl
* @throws \Exception
*/
Expand All @@ -68,8 +73,7 @@ protected function getJson(BaseCommand $bean, array $headers = [])
$http->setHeader($headerKey, $headerVal);
}
}
$ret = $http->get()->getBody();
return !empty($ret) ? json_decode($ret, true): null;
return $this->checkResponse($http->get());
} catch (\Exception $exception) {
throw new \Exception($exception->getMessage());
}
Expand All @@ -80,8 +84,8 @@ protected function getJson(BaseCommand $bean, array $headers = [])

/**
* @param BaseCommand $bean
* @param array $headers
* @return mixed|null
* @param array $headers
* @return mixed
* @throws InvalidUrl
* @throws \Exception
*/
Expand All @@ -98,8 +102,7 @@ protected function postJson(BaseCommand $bean, array $headers = [])
$http->setHeader($headerKey, $headerVal);
}
}
$ret = $http->postJson($data)->getBody();
return !empty($ret) ? json_decode($ret, true): null;
return $this->checkResponse($http->postJson($data));
} catch (\Exception $exception) {
throw new \Exception($exception->getMessage());
}
Expand All @@ -110,26 +113,25 @@ protected function postJson(BaseCommand $bean, array $headers = [])

/**
* @param BaseCommand $bean
* @param array $headers
* @return mixed|null
* @param array $headers
* @return mixed
* @throws InvalidUrl
* @throws \Exception
*/
protected function deleteJson(BaseCommand $bean, array $headers=[])
{
$url = $this->getUrl($bean);
$data = $this->toRequestJson($bean);
$data = $this->toRequestParam($bean);
$url = $data ? $url . '?' . $data : $url;
$http = new HttpClient($url);

if ($http) {
try{
if (isset($headers) && !empty($headers)) {
foreach ($headers as $headerKey => $headerVal) {
$http->setHeader($headerKey, $headerVal);
}
}
$ret = $http->delete()->getBody();
return !empty($ret) ? json_decode($ret, true): null;
return $this->checkResponse($http->delete());
} catch (\Exception $exception) {
throw new \Exception($exception->getMessage());
}
Expand All @@ -138,14 +140,43 @@ protected function deleteJson(BaseCommand $bean, array $headers=[])
}
}

/**
* @param Response $consulResponse
* @return mixed
* @throws Exception
*/
private function checkResponse(Response $consulResponse) {
// 网络响应失败
if ($consulResponse->getErrCode()) {
throw new Exception('Consul Network Error: ' . $consulResponse->getErrMsg());
}

// 错误请求状态码
if ($consulResponse->getStatusCode() >= 400) {
$reason = isset($consulResponse->getHeaders()['x-consul-reason']) ?
$consulResponse->getHeaders()['x-consul-reason'] : '';
$reason = $reason ?: $consulResponse->getBody();
throw new Exception(sprintf(
'Consul Request Error (Status Code: %d, Reason: %s)',
$consulResponse->getStatusCode(),
$reason
));
};
// 解码请求内容
$contentJson = $consulResponse->getBody();
$contentArr = json_decode($contentJson, true);

return $contentArr;
}

private function getUrl(BaseCommand $bean): string
{
return $this->route . $bean->getUrl();
}

/**
* 处理请求Request,过滤null值
* @param SplBean $request
* @param BaseCommand $request
* @return string
*/
private function toRequestParam(BaseCommand $request): string
Expand Down
4 changes: 1 addition & 3 deletions src/Coordinates.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,16 @@ public function node(Node $node)
* Update LAN Coordinates for a node
* @param Update $update
* @throws \EasySwoole\HttpClient\Exception\InvalidUrl
* @throws \ReflectionException
*/
public function update(Update $update)
{
if (empty($update->getDc())) {
$update->setUrl(substr($update->getUrl(), 0, strlen($update->getUrl()) -3));
$this->putJSON($update);
} else {
$update->setUrl(sprintf($update->getUrl(), $update->getDc()));
$update->setUrl(sprintf($update->getUrl(), '?dc=' . $update->getDc()));
$update->setDc('');
$this->putJSON($update);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
namespace EasySwoole\Consul\Exception;

class WrongRequiredParamsException extends Exception
class InvalidRequiredParamsException extends Exception
{

}
37 changes: 0 additions & 37 deletions src/Request/Agent/Connect/Proxy.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Request/Coordinate/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
class Update extends BaseCommand
{
protected $url = 'coordinate/update/%s';
protected $url = 'coordinate/update%s';

/**
* @var string
Expand Down
1 change: 1 addition & 0 deletions tests/AclTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use EasySwoole\Consul\Request\Acl\Update;
use EasySwoole\Consul\Request\Agent\Join;
use PHPUnit\Framework\TestCase;

class AclTest extends TestCase
{
protected $config;
Expand Down
Loading

0 comments on commit dc0e343

Please sign in to comment.