mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-01 03:08:06 +08:00
Add more apis
This commit is contained in:
parent
438c15a935
commit
d1e6cada43
85
src/nsq/src/Api/Api.php
Normal file
85
src/nsq/src/Api/Api.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Nsq\Api;
|
||||
|
||||
use GuzzleHttp\RequestOptions;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class Api extends Client
|
||||
{
|
||||
public function stats(string $format = 'text', ?string $topic = null, ?string $channel = null): ResponseInterface
|
||||
{
|
||||
$query = [
|
||||
'format' => $format,
|
||||
];
|
||||
! is_null($topic) && $query['topic'] = $topic;
|
||||
! is_null($channel) && $query['channel'] = $channel;
|
||||
return $this->client->request('GET', '/stats', [
|
||||
RequestOptions::QUERY => $query,
|
||||
]);
|
||||
}
|
||||
|
||||
public function ping(): bool
|
||||
{
|
||||
$response = $this->client->request('GET', '/ping');
|
||||
return $response->getStatusCode() === 200;
|
||||
}
|
||||
|
||||
public function info(): ResponseInterface
|
||||
{
|
||||
return $this->client->request('GET', '/info');
|
||||
}
|
||||
|
||||
public function debugPprof(): ResponseInterface
|
||||
{
|
||||
return $this->client->request('GET', '/debug/pprof');
|
||||
}
|
||||
|
||||
public function debugPprofProfile(): ResponseInterface
|
||||
{
|
||||
return $this->client->request('GET', '/debug/pprof/profile');
|
||||
}
|
||||
|
||||
public function debugPprofGoroutine(): ResponseInterface
|
||||
{
|
||||
return $this->client->request('GET', '/debug/pprof/goroutine');
|
||||
}
|
||||
|
||||
public function debugPprofHeap(): ResponseInterface
|
||||
{
|
||||
return $this->client->request('GET', '/debug/pprof/heap');
|
||||
}
|
||||
|
||||
public function debugPprofBlock(): ResponseInterface
|
||||
{
|
||||
return $this->client->request('GET', '/debug/pprof/block');
|
||||
}
|
||||
|
||||
public function debugPprofThreadCreate(): ResponseInterface
|
||||
{
|
||||
return $this->client->request('GET', '/debug/pprof/threadcreate');
|
||||
}
|
||||
|
||||
public function getConfigNsqlookupdTcpAddresses(): ResponseInterface
|
||||
{
|
||||
return $this->client->request('GET', '/config/nsqlookupd_tcp_addresses');
|
||||
}
|
||||
|
||||
public function setConfigNsqlookupdTcpAddresses(array $addresses): bool
|
||||
{
|
||||
$response = $this->client->request('PUT', '/config/nsqlookupd_tcp_addresses', [
|
||||
RequestOptions::JSON => $addresses,
|
||||
]);
|
||||
$statusCode = $response->getStatusCode();
|
||||
return $statusCode >= 200 && $statusCode < 300;
|
||||
}
|
||||
}
|
@ -12,11 +12,13 @@ declare(strict_types=1);
|
||||
namespace HyperfTest\Nsq;
|
||||
|
||||
use Hyperf\Config\Config;
|
||||
use Hyperf\Nsq\Api\Api;
|
||||
use Hyperf\Nsq\Api\Channel;
|
||||
use Hyperf\Nsq\Api\HttpClient;
|
||||
use Hyperf\Nsq\Api\Topic;
|
||||
use Mockery;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
@ -52,9 +54,56 @@ class ApiTest extends TestCase
|
||||
$this->assertTrue($client->delete('hyperf.test', 'test'));
|
||||
}
|
||||
|
||||
public function testBasicApi()
|
||||
{
|
||||
$client = new Api($this->getClient());
|
||||
|
||||
// /info
|
||||
$info = $client->info();
|
||||
$this->assertInstanceOf(ResponseInterface::class, $info);
|
||||
$this->assertSame(200, $info->getStatusCode());
|
||||
$this->assertJson($info->getBody()->getContents());
|
||||
|
||||
// /ping
|
||||
$ping = $client->ping();
|
||||
$this->assertTrue($ping);
|
||||
|
||||
// /stats - text
|
||||
$stats = $client->stats();
|
||||
$this->assertInstanceOf(ResponseInterface::class, $stats);
|
||||
$this->assertSame(200, $stats->getStatusCode());
|
||||
$this->assertTrue(is_string($stats->getBody()->getContents()));
|
||||
|
||||
// /stats - json
|
||||
$stats = $client->stats('json');
|
||||
$this->assertInstanceOf(ResponseInterface::class, $stats);
|
||||
$this->assertSame(200, $stats->getStatusCode());
|
||||
$this->assertJson($stats->getBody()->getContents());
|
||||
}
|
||||
|
||||
public function testConfigNsalookupdTcpAddresses()
|
||||
{
|
||||
$client = new Api($this->getClient());
|
||||
|
||||
// PUT /config/nsqlookupd_tcp_addresses
|
||||
$addresses = $client->setConfigNsqlookupdTcpAddresses(['nsqlookupd:4160', 'nsqlookupd:4161']);
|
||||
$this->assertTrue($addresses);
|
||||
$this->assertSame('["nsqlookupd:4160","nsqlookupd:4161"]', $client->getConfigNsqlookupdTcpAddresses()->getBody()->getContents());
|
||||
|
||||
// Reset addresses
|
||||
$client->setConfigNsqlookupdTcpAddresses(['nsqlookupd:4160']);
|
||||
|
||||
// GET /config/nsqlookupd_tcp_addresses
|
||||
$addresses = $client->getConfigNsqlookupdTcpAddresses();
|
||||
$this->assertInstanceOf(ResponseInterface::class, $addresses);
|
||||
$this->assertSame(200, $addresses->getStatusCode());
|
||||
$this->assertSame('["nsqlookupd:4160"]', $addresses->getBody()->getContents());
|
||||
|
||||
}
|
||||
|
||||
protected function getClient()
|
||||
{
|
||||
$config = new Config([]);
|
||||
$config = new Config(['nsq' => ['api' => ['port' => 32771]]]);
|
||||
return new HttpClient($config);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user