DirectRequest.php
2.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
<?php
namespace App\Service\Requests;
/**
* Class DirectRequest
* @package App\Service\Requests
*
*
*/
class DirectRequest extends APIRequest
{
CONST MAX_COUNT = 10000;
protected $max_count = -1;
private $url;
protected function __construct($api)
{
parent::__construct($api);
$this->url = config('api.yandex.url');
}
function slice($maxObjects): ?\App\Service\Contract\APIRequest
{
return null;
}
function next($offset)
{
if (!isset($this->params['Page'])) {
$this->params['Page'] = [];
}
$this->params['Page']['Limit'] = self::MAX_COUNT;
$this->params['Page']['Offset'] = $offset;
}
function getUrl()
{
return $this->url . strtolower($this->getService());
}
function getBody()
{
$data = [
'method' => $this->getMethod(),
'params' => $this->getParams() ?? (object)[]
];
return json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
function getHeaders()
{
return [
// "Authorization: Bearer " . $this->getToken()->token, // OAuth-токен. Использование слова Bearer обязательно
"Accept-Language: ru", // Язык ответных сообщений
"Content-Type: application/json; charset=utf-8" // Тип данных и кодировка запроса
];
}
function getRequest($service, $method)
{
$ns = str_replace("DirectRequest", "Direct", self::class);
$class = $ns . "\\" . ucfirst($method) . ucfirst($service);
if (class_exists($class)) {
$request = new $class($this->getApi());
$request->setToken($this->getToken());
return $request;
}
throw new \Exception("Unknown Service or Method \"{$class}\"");
return $this;
}
function getObjectsCount()
{
return $this->getMaxCount();
}
function getMaxCount()
{
return $this->max_count;
}
}