Builder.php
3.1 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace Lackoxygen\TiktokShop\Support\Client;
use GuzzleHttp\Exception\GuzzleException;
use Lackoxygen\TiktokShop\Attribute\Config\Config;
use Lackoxygen\TiktokShop\Attribute\Config\Option;
use Lackoxygen\TiktokShop\Attribute\Request;
use Lackoxygen\TiktokShop\Exception\ClientException;
use Lackoxygen\TiktokShop\Mock\Mock;
use Psr\Http\Message\ResponseInterface;
class Builder extends Config
{
/**
* @var Request
*/
protected Request $requestHandler;
/**
* @var string
*/
protected string $request;
/**
* @var string
*/
protected string $method;
public function __construct(Option $config, string $request, string $method)
{
parent::__construct($config);
$this->requestHandler = new Request($config);
$this->request = $request;
$this->method = $method;
}
/**
* @param Option $config
* @param string $passage
* @param string $method
* @return Builder
*/
public static function create(Option $config, string $passage, string $method): Builder
{
return new static(...func_get_args());
}
/**
* @param string $method
* @return $this
*/
public function method(string $method): Builder
{
$this->requestHandler->setMethod($method);
return $this;
}
/**
* @param array $params
* @return $this
*/
public function params(array $params): Builder
{
$this->requestHandler->setParams($params);
return $this;
}
/**
* @param string $v
* @return $this
*/
public function v(string $v): Builder
{
$this->requestHandler->setV($v);
return $this;
}
/**
* @param $timestamp
* @return $this
*/
public function timestamp($timestamp): Builder
{
$this->requestHandler->setTimestamp($timestamp);
return $this;
}
/**
* @param string|null $service
* @return $this
*/
public function service(?string $service): Builder
{
$this->requestHandler->setService($service);
return $this;
}
/**
* @param bool $signature
* @return $this
*/
public function signature(bool $signature): Builder
{
$this->requestHandler->setSignature($signature);
return $this;
}
/**
* @param string $path
* @return $this
*/
public function path(string $path): Builder
{
$this->requestHandler->setPath($path);
return $this;
}
/**
* @return Request
*/
public function getRequest(): Request
{
return $this->requestHandler;
}
/**
* @return ResponseInterface
* @throws ClientException
*/
public function request(): ResponseInterface
{
$client = Client::create();
if ($this->config->isEnableMock()) {
$mock = new Mock($this->request, $this->method);
return $mock->response();
}
try {
return $client->request($this->requestHandler);
} catch (GuzzleException $e) {
throw new ClientException($e);
}
}
}