DirectRequest.php
2.72 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
<?php
namespace App\Service\Requests;
/**
* Class DirectRequest
* @package App\Service\Requests
*
*
*/
class DirectRequest extends APIRequest {
CONST MAX_COUNT = 10000;
private $url;
protected function __construct($api)
{
parent::__construct($api);
$this->url = config('api.yandex.url');
}
function getCountObjects(): int
{
return 1;
}
function slice($count): ?\App\Service\Contract\APIRequest
{
return null;
if ($count<0)
return null;
//для запросов get ничего не разбиваем, т.к. заранее не знаем сколько чего будет
//для них только после выполнения запроса будет известно есть ли кроме $count еще данные
//а текущий запрос просто устанавливается в $count
//а вот для запросов мутаторов будем разбивать. Но тут уже будет зависеть от запроса
//будем под каждый реализовывать делитель
if (!isset($this->params['Page'])){
$this->params['Page'] = [];
}
$this->params['Page']['Limit'] = $count;
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 . $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;
}
return $this;
}
}