Builder.php
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace Lackoxygen\TiktokShop\Transmit;
use Lackoxygen\TiktokShop\Attribute\Config;
use Lackoxygen\TiktokShop\Attribute\Request;
use Lackoxygen\TiktokShop\Exception\ClientException;
use Lackoxygen\TiktokShop\Mock\Mock;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
class Builder
{
protected Request $request;
protected Config $config;
protected string $passage, $method;
public function __construct(Config $config, string $passage, string $method)
{
$this->request = new Request;
$this->config = $config;
$this->request->setConfig($config);
$this->passage = $passage;
$this->method = $method;
}
public static function create(Config $config, string $passage, string $method): Builder
{
return new static(...func_get_args());
}
public function method(string $method): Builder
{
$this->request->setMethod($method);
return $this;
}
public function params(array $params): Builder
{
$this->request->setParams($params);
return $this;
}
public function v(string $v): Builder
{
$this->request->setV($v);
return $this;
}
public function timestamp($timestamp): Builder
{
$this->request->setTimestamp($timestamp);
return $this;
}
public function service(?string $service): Builder
{
$this->request->setService($service);
return $this;
}
public function signature(bool $signature): Builder
{
$this->request->setSignature($signature);
return $this;
}
public function path(string $path): Builder
{
$this->request->setPath($path);
return $this;
}
/**
* @return Request
*/
public function getRequest(): Request
{
return $this->request;
}
/**
* @return ResponseInterface
* @throws ClientException
*/
public function request(): ResponseInterface
{
$client = Client::create();
if ($this->config->isEnableMock()) {
$mock = new Mock($this->passage, $this->method);
return $mock->response();
}
try {
return $client->request($this->request);
} catch (GuzzleException $e) {
throw new ClientException($e);
}
}
}