1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carpenstar\ByBitAPI\Core\Endpoints; |
4
|
|
|
|
5
|
|
|
use Carpenstar\ByBitAPI\Core\Auth\Credentials; |
6
|
|
|
use Carpenstar\ByBitAPI\Core\Builders\CurlBuilder; |
7
|
|
|
use Carpenstar\ByBitAPI\Core\Exceptions\ApiException; |
8
|
|
|
use Carpenstar\ByBitAPI\Core\Exceptions\SDKException; |
9
|
|
|
use Carpenstar\ByBitAPI\Core\Interfaces\IResponseInterface; |
10
|
|
|
use Carpenstar\ByBitAPI\Core\Interfaces\IEndpointInterface; |
11
|
|
|
use Carpenstar\ByBitAPI\Core\Interfaces\IParametersInterface; |
12
|
|
|
use Carpenstar\ByBitAPI\Core\Objects\StubQueryBag; |
13
|
|
|
use Carpenstar\ByBitAPI\Core\Response\CurlResponseHandler; |
14
|
|
|
|
15
|
|
|
abstract class Endpoint implements IEndpointInterface |
16
|
|
|
{ |
17
|
|
|
protected Credentials $credentials; |
18
|
|
|
protected string $method; |
19
|
|
|
protected string $url; |
20
|
|
|
protected IParametersInterface $parameters; |
21
|
|
|
abstract protected function getResponseClassnameByCondition(array &$apiData = null): string; |
22
|
|
|
abstract protected function getRequestClassname(): string; |
23
|
|
|
abstract protected function getEndpointUrl(): string; |
24
|
|
|
|
25
|
|
|
public function setCredentials(Credentials $credentials): self |
26
|
|
|
{ |
27
|
|
|
$this->credentials = $credentials; |
28
|
|
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param IParametersInterface|null $requestParameters |
33
|
|
|
* @return $this |
34
|
|
|
* @throws ApiException|SDKException |
35
|
|
|
*/ |
36
|
|
|
public function bindRequestParameters(?IParametersInterface $requestParameters): self |
37
|
|
|
{ |
38
|
|
|
if (get_class($requestParameters ?? new StubQueryBag()) != $this->getRequestClassname()) { |
39
|
|
|
throw new SDKException(get_class($requestParameters) . " must be instance of " . $this->getRequestClassname()); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->parameters = $requestParameters ?? new StubQueryBag(); |
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return IResponseInterface |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
|
|
public function execute(): IResponseInterface |
51
|
|
|
{ |
52
|
|
|
$reguestParameters = $this->parameters->array(); |
53
|
|
|
|
54
|
|
|
$curlInstance = CurlBuilder::make($this->getEndpointRequestMethod()) |
55
|
|
|
->init($this->isNeedAuthorization(), $this->credentials); |
56
|
|
|
|
57
|
|
|
$data = $curlInstance->execute($this->getEndpointUrl(), $reguestParameters); |
58
|
|
|
|
59
|
|
|
return (new CurlResponseHandler())->build($data, $this->getResponseClassnameByCondition($data)); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|