|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Cbws\Sdk\Private\Flows\V1alpha1; |
| 4 | + |
| 5 | +use Cbws\API\Client\GrpcTrait; |
| 6 | +use Cbws\API\Exception\StatusException; |
| 7 | +use Cbws\Private_\Flows\V1alpha1\GetTrafficUsageResponse as GrpcGetTrafficUsageResponse; |
| 8 | +use Cbws\Private_\Flows\V1alpha1\GetTrafficPercentileResponse as GrpcGetTrafficPercentileResponse; |
| 9 | +use Cbws\Private_\Flows\V1alpha1\TrafficServiceClient; |
| 10 | +use Grpc\ChannelCredentials; |
| 11 | + |
| 12 | +class Client |
| 13 | +{ |
| 14 | + use GrpcTrait; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var TrafficServiceClient |
| 18 | + */ |
| 19 | + protected $client; |
| 20 | + |
| 21 | + public function __destruct() |
| 22 | + { |
| 23 | + if (!is_null($this->client)) { |
| 24 | + $this->client->close(); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Get traffic usage in total bytes |
| 30 | + * |
| 31 | + * @param GetTrafficUsageRequest|null $request |
| 32 | + * @return GetTrafficUsageResponse |
| 33 | + * @throws StatusException |
| 34 | + */ |
| 35 | + public function getTrafficUsage(GetTrafficUsageRequest $request = null): GetTrafficUsageResponse |
| 36 | + { |
| 37 | + if (is_null($request)) { |
| 38 | + $request = new GetTrafficUsageRequest(); |
| 39 | + } |
| 40 | + |
| 41 | + $call = $this->getClient()->GetTrafficUsage($request->toGrpc()); |
| 42 | + /** @var GrpcGetTrafficUsageResponse $data */ |
| 43 | + list($data, $status) = $call->wait(); |
| 44 | + if ($status->code !== 0) { |
| 45 | + throw StatusException::fromStatus($status); |
| 46 | + } |
| 47 | + |
| 48 | + return new GetTrafficUsageResponse($data); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Get traffic usage in 95th percentile |
| 53 | + * |
| 54 | + * @param GetTrafficPercentileRequest|null $request |
| 55 | + * @return GetTrafficPercentileResponse |
| 56 | + * @throws StatusException |
| 57 | + */ |
| 58 | + public function getTrafficPercentile(GetTrafficPercentileRequest $request = null): GetTrafficPercentileResponse |
| 59 | + { |
| 60 | + if (is_null($request)) { |
| 61 | + $request = new GetTrafficPercentileRequest(); |
| 62 | + } |
| 63 | + |
| 64 | + $call = $this->getClient()->GetTrafficPercentile($request->toGrpc()); |
| 65 | + /** @var GrpcGetTrafficPercentileResponse $data */ |
| 66 | + list($data, $status) = $call->wait(); |
| 67 | + if ($status->code !== 0) { |
| 68 | + throw StatusException::fromStatus($status); |
| 69 | + } |
| 70 | + |
| 71 | + return new GetTrafficPercentileResponse($data); |
| 72 | + } |
| 73 | + |
| 74 | + protected function getClient(): TrafficServiceClient |
| 75 | + { |
| 76 | + if (!is_null($this->client)) { |
| 77 | + return $this->client; |
| 78 | + } |
| 79 | + |
| 80 | + $channelCredentials = ChannelCredentials::createInsecure(); |
| 81 | + $this->client = new TrafficServiceClient($this->getEndpoint(), [ |
| 82 | + 'credentials' => $channelCredentials, |
| 83 | + ]); |
| 84 | + |
| 85 | + return $this->client; |
| 86 | + } |
| 87 | + |
| 88 | + protected function getEndpoint(): string |
| 89 | + { |
| 90 | + return 'localhost:9000'; |
| 91 | + } |
| 92 | +} |
0 commit comments