Skip to content

Commit a509e02

Browse files
MarlincMarlin Cremers
authored andcommitted
feat: add private flows sdk
1 parent 5c137aa commit a509e02

6 files changed

Lines changed: 334 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Cbws\Sdk\Private\Flows\V1alpha1;
4+
5+
use DateTime;
6+
use Google\Protobuf\Timestamp;
7+
8+
class GetTrafficPercentileRequest
9+
{
10+
/**
11+
* @var \Cbws\Private_\Flows\V1alpha1\GetTrafficPercentileRequest
12+
*/
13+
protected $object;
14+
15+
public function __construct(\Cbws\Private_\Flows\V1alpha1\GetTrafficPercentileRequest $object = null)
16+
{
17+
if (is_null($object)) {
18+
$object = new \Cbws\Private_\Flows\V1alpha1\GetTrafficPercentileRequest();
19+
}
20+
21+
$this->object = $object;
22+
}
23+
24+
public function withStartTime(DateTime $time): self
25+
{
26+
$timestamp = new Timestamp();
27+
$timestamp->fromDateTime($time);
28+
29+
$cloned = clone $this;
30+
31+
$cloned->object->setStartTime($timestamp);
32+
33+
return $cloned;
34+
}
35+
36+
public function withEndTime(DateTime $time): self
37+
{
38+
$timestamp = new Timestamp();
39+
$timestamp->fromDateTime($time);
40+
41+
$cloned = clone $this;
42+
43+
$cloned->object->setEndTime($timestamp);
44+
45+
return $cloned;
46+
}
47+
48+
public function withTenantName(string $tenant): self
49+
{
50+
$cloned = clone $this;
51+
52+
$cloned->object->setTenantName($tenant);
53+
54+
return $cloned;
55+
}
56+
57+
public function withTrafficDirection(TrafficDirection $direction): self
58+
{
59+
$cloned = clone $this;
60+
61+
$cloned->object->setTrafficDirection($direction->asGrpc());
62+
63+
return $cloned;
64+
}
65+
66+
public function toGrpc(): \Cbws\Private_\Flows\V1alpha1\GetTrafficPercentileRequest
67+
{
68+
return $this->object;
69+
}
70+
71+
public function __debugInfo()
72+
{
73+
return [
74+
'startTime' => $this->object->getStartTime(),
75+
'endTime' => $this->object->getEndTime(),
76+
'direction' => $this->object->getTrafficDirection(),
77+
'tenantName' => $this->object->getTenantName(),
78+
];
79+
}
80+
81+
public static function create()
82+
{
83+
return new self();
84+
}
85+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Cbws\Sdk\Private\Flows\V1alpha1;
4+
5+
class GetTrafficPercentileResponse
6+
{
7+
/**
8+
* @var \Cbws\Private_\Flows\V1alpha1\GetTrafficPercentileResponse
9+
*/
10+
protected $object;
11+
12+
public function __construct(\Cbws\Private_\Flows\V1alpha1\GetTrafficPercentileResponse $object)
13+
{
14+
$this->object = $object;
15+
}
16+
17+
public function getPercentile(): int
18+
{
19+
return $this->object->getPercentile();
20+
}
21+
22+
public function __debugInfo()
23+
{
24+
return [
25+
'percentile' => $this->getPercentile(),
26+
];
27+
}
28+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Cbws\Sdk\Private\Flows\V1alpha1;
4+
5+
use DateTime;
6+
use Google\Protobuf\Timestamp;
7+
8+
class GetTrafficUsageRequest
9+
{
10+
/**
11+
* @var \Cbws\Private_\Flows\V1alpha1\GetTrafficUsageRequest
12+
*/
13+
protected $object;
14+
15+
public function __construct(\Cbws\Private_\Flows\V1alpha1\GetTrafficUsageRequest $object = null)
16+
{
17+
if (is_null($object)) {
18+
$object = new \Cbws\Private_\Flows\V1alpha1\GetTrafficUsageRequest();
19+
}
20+
21+
$this->object = $object;
22+
23+
}
24+
25+
public function withStartTime(DateTime $time): self
26+
{
27+
$timestamp = new Timestamp();
28+
$timestamp->fromDateTime($time);
29+
30+
$this->object->setStartTime($timestamp);
31+
32+
return $this;
33+
}
34+
35+
public function withEndTime(DateTime $time): self
36+
{
37+
$timestamp = new Timestamp();
38+
$timestamp->fromDateTime($time);
39+
40+
$this->object->setEndTime($timestamp);
41+
42+
return $this;
43+
}
44+
45+
public function withTenantName(string $tenant): self
46+
{
47+
$this->object->setTenantName($tenant);
48+
49+
return $this;
50+
}
51+
52+
public function withTrafficDirection(TrafficDirection $direction): self
53+
{
54+
$cloned = clone $this;
55+
56+
$cloned->object->setTrafficDirection($direction->asGrpc());
57+
58+
return $cloned;
59+
}
60+
61+
public function toGrpc(): \Cbws\Private_\Flows\V1alpha1\GetTrafficUsageRequest
62+
{
63+
return $this->object;
64+
}
65+
66+
public function __debugInfo()
67+
{
68+
return [
69+
70+
];
71+
}
72+
73+
public static function create()
74+
{
75+
return new self();
76+
}
77+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Cbws\Sdk\Private\Flows\V1alpha1;
4+
5+
class GetTrafficUsageResponse
6+
{
7+
/**
8+
* @var \Cbws\Private_\Flows\V1alpha1\GetTrafficUsageResponse
9+
*/
10+
protected $object;
11+
12+
public function __construct(\Cbws\Private_\Flows\V1alpha1\GetTrafficUsageResponse $object)
13+
{
14+
$this->object = $object;
15+
}
16+
17+
public function getBytes(): int
18+
{
19+
return $this->object->getBytes();
20+
}
21+
22+
public function __debugInfo()
23+
{
24+
return [
25+
'bytes' => $this->getBytes(),
26+
];
27+
}
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Cbws\Sdk\Private\Flows\V1alpha1;
4+
5+
use Cbws\Private_\Flows\V1alpha1\TrafficDirection as GrpcTrafficDirection;
6+
7+
enum TrafficDirection
8+
{
9+
case Unspecified;
10+
case Ingress;
11+
case Egress;
12+
13+
public function asGrpc(): int
14+
{
15+
switch ($this) {
16+
case TrafficDirection::Ingress:
17+
return GrpcTrafficDirection::TRAFFIC_DIRECTION_INGRESS;
18+
case TrafficDirection::Egress:
19+
return GrpcTrafficDirection::TRAFFIC_DIRECTION_EGRESS;
20+
}
21+
22+
return GrpcTrafficDirection::TRAFFIC_DIRECTION_UNSPECIFIED;
23+
}
24+
}

0 commit comments

Comments
 (0)