1
|
|
|
<?php |
2
|
|
|
namespace Carpenstar\ByBitAPI\Core\Endpoints; |
3
|
|
|
|
4
|
|
|
use Carpenstar\ByBitAPI\Core\Builders\ResponseHandlerBuilder; |
5
|
|
|
use Carpenstar\ByBitAPI\Core\Enums\EnumHttpMethods; |
6
|
|
|
use Carpenstar\ByBitAPI\Core\Exceptions\ApiException; |
7
|
|
|
use Carpenstar\ByBitAPI\Core\Interfaces\ICurlResponseDtoInterface; |
8
|
|
|
use Carpenstar\ByBitAPI\Core\Interfaces\IEndpointInterface; |
9
|
|
|
use Carpenstar\ByBitAPI\Core\Interfaces\IParametersInterface; |
10
|
|
|
use Carpenstar\ByBitAPI\Core\Interfaces\IResponseHandlerInterface; |
11
|
|
|
use Carpenstar\ByBitAPI\Core\Objects\StubQueryBag; |
12
|
|
|
use Carpenstar\ByBitAPI\Core\Request\Curl; |
13
|
|
|
use Carpenstar\ByBitAPI\Core\Request\GetRequest; |
14
|
|
|
use Carpenstar\ByBitAPI\Core\Request\PostRequest; |
15
|
|
|
use Carpenstar\ByBitAPI\Core\Response\CurlResponseHandler; |
16
|
|
|
|
17
|
|
|
abstract class Endpoint implements IEndpointInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string HTTP-метод GET, POST и т.д |
21
|
|
|
*/ |
22
|
|
|
protected string $method; |
23
|
|
|
protected string $url; |
24
|
|
|
protected IParametersInterface $parameters; |
25
|
|
|
|
26
|
|
|
protected IResponseHandlerInterface $response; |
27
|
|
|
|
28
|
|
|
abstract protected function getResponseClassname(): string; |
29
|
|
|
abstract protected function getRequestClassname(): string; |
30
|
|
|
abstract protected function getEndpointUrl(): string; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
protected function getResponseHandlerClassname(): string |
36
|
|
|
{ |
37
|
|
|
return CurlResponseHandler::class; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param IParametersInterface|null $requestParameters |
42
|
|
|
* @return $this |
43
|
|
|
* @throws ApiException |
44
|
|
|
*/ |
45
|
|
|
public function bindRequestParameters(?IParametersInterface $requestParameters): self |
46
|
|
|
{ |
47
|
|
|
if (get_class($requestParameters ?? new StubQueryBag()) != $this->getRequestClassname()) { |
48
|
|
|
throw new ApiException(get_class($requestParameters) . " must be instance of " . $this->getRequestClassname()); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->parameters = $requestParameters ?? new StubQueryBag(); |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return Curl |
57
|
|
|
* @throws \Exception |
58
|
|
|
*/ |
59
|
|
|
protected function makeRequestObject(): Curl |
60
|
|
|
{ |
61
|
|
|
switch (static::HTTP_METHOD) { |
|
|
|
|
62
|
|
|
case EnumHttpMethods::GET: |
63
|
|
|
return GetRequest::getInstance(static::IS_NEED_AUTHORIZATION); |
|
|
|
|
64
|
|
|
case EnumHttpMethods::POST: |
65
|
|
|
return PostRequest::getInstance(static::IS_NEED_AUTHORIZATION); |
66
|
|
|
default: |
67
|
|
|
throw new \Exception("Http Method not detected"); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param int $mode |
73
|
|
|
* @return ICurlResponseDtoInterface |
74
|
|
|
* @throws \Exception |
75
|
|
|
*/ |
76
|
|
|
public function execute(int $mode): ICurlResponseDtoInterface |
77
|
|
|
{ |
78
|
|
|
return ResponseHandlerBuilder::make( |
79
|
|
|
$this->makeRequestObject()->exec( |
|
|
|
|
80
|
|
|
$this->getEndpointUrl(), $this->parameters->array() |
81
|
|
|
), |
82
|
|
|
$this->getResponseHandlerClassname(), |
83
|
|
|
$this->getResponseClassname(), |
84
|
|
|
$mode |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |