-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClient.php
265 lines (210 loc) · 6.79 KB
/
Client.php
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
namespace DG\API\Photo;
class Client
{
const API_VERSION = '2.0';
const API_URL = 'http://photo.local/2.0/';
const HTTP_POST = 'POST';
const HTTP_GET = 'GET';
const FORMAT_JSON = 'json';
const FORMAT_JSONP = 'jsonp';
const LOCALE_RU_RU = 'ru_RU';
const LOCALE_EN_GB = 'en_GB';
const LOCALE_EN_US = 'en_US';
const OBJECT_TYPE_BRANCH = 'branch';
const OBJECT_TYPE_GEO = 'geo';
const ALBUM_CODE_COMMON = 'common';
const ALBUM_CODE_VIEW = 'view';
const ALBUM_CODE_FACILITIES = 'facilities';
protected $_apiKey;
protected $_format;
protected $_locale;
protected $_onResult;
public function __construct($apiKey, $format = self::FORMAT_JSON, $locale = self::LOCALE_RU_RU)
{
$this->_apiKey = $apiKey;
$this->_format = $format;
$this->_locale = $locale;
}
protected function getCurlExecString($methodName, array $params = [], $httpMethod = self::HTTP_POST)
{
$fx = function($fx, $prefix, $ar) {
$res = [];
foreach($ar as $k => $v)
{
if(is_array($v))
{
$r = $fx($fx, ($prefix ? $prefix.'['.$k.']' : $k), $v);
foreach($r as $vx)
{
$res[] = $vx;
}
}else
{
$res[] = '--form '.($prefix ? $prefix.'['.$k.']' : $k).'='."'".str_replace(["'", '\\'], ["\\'", '\\\\'], $v)."'";
}
}
return $res;
};
$args = $fx($fx, '', $params);
$cmd = '/usr/bin/curl -s -X '.$httpMethod.' \''.self::API_URL.$methodName.'\' '.implode(' ', $args);
return $cmd;
}
protected function onResult($jsonResult, $methodName, $params, $httpMethod)
{
if($this->_onResult && is_callable($this->_onResult))
{
return call_user_func_array($this->_onResult, func_get_args());
}
return false;
}
protected function callMethod($methodName, array $params = [], $httpMethod = self::HTTP_POST)
{
$httpCode = 200;
try
{
/**
* @DESC php curl cant work with multi-array arguments normally with file attache
*/
$cmd = $this->getCurlExecString($methodName, $params, $httpMethod);
exec($cmd, $res, $returnCode);
if($returnCode != 0)
{
$httpCode = 500;
}
$res = implode('', $res);
} catch (\Exception $e) {
throw new Exception($e->getMessage(), $e->getCode());
}
$this->onResult($res, $methodName, $params, $httpMethod, $cmd);
if($httpCode != 200)
{
/**
* @TODO error
*/
throw new Exception('Result code is not 200');
}
return $res;
}
protected function parseResponse($response, $format = self::FORMAT_JSON)
{
$resJson = json_decode($response, true);
if(!$resJson)
{
throw new Exception('Result is not JSON');
}
return $resJson;
}
public function makeRequest($methodName, array $params, $httpMethod)
{
$res = $this->callMethod($methodName, $params, $httpMethod);
$resJson = $this->parseResponse($res, $this->_format);
return $resJson;
}
protected function extendParams(array $params = [], array $fields = ['key', 'format', 'locale'])
{
$systemFields = [
'key' => $this->_apiKey,
'format' => $this->_format,
'locale' => $this->_locale,
];
foreach($fields as $fieldKey)
{
$params[$fieldKey] = $systemFields[$fieldKey];
}
return $params;
}
public function setOnResult($onResult)
{
$this->_onResult = $onResult;
return $this;
}
public function getOnResult()
{
return $this->_onResult;
}
public function add(PhotoCollection &$collection, $objectType, $objectId, $albumCode, $userId = null)
{
$params = $this->extendParams([
'object_type' => $objectType,
'object_id' => $objectId,
'album_code' => $albumCode,
'user_id' => $userId,
'items' => array_values( array_map(function($item){
return [
'uid' => $item->getUID(),
'options' => $item->getOptions(),
];
}, $collection->getItems()) ),
]);
$res = $this->makeRequest('add', $params, self::HTTP_POST);
if(!$res)
{
throw new Exception('No result');
}
if(!isset($res['meta']['code']) || $res['meta']['code'] != 200 )
{
/**
* @TODO error
*/
throw new Exception('Result code is not 200');
}
if(!isset($res['result']))
{
throw new Exception('No result');
}
$result = $res['result'];
$fields = ['total', 'album_code', 'album_name', 'items'];
$resData = [];
foreach($fields as $field)
{
if(!isset($result[$field]))
{
throw new Exception('No field "'.$field.'" in result');
}
$resData[$field] = $result[$field];
}
if(!is_array($resData['items']))
{
throw new Exception('Items are not array');
}
foreach($resData['items'] as $resItem)
{
if(isset($resItem['error']))
{
/**
* @TODO error
*/
throw new Exception('Item error');
}
$data = $resItem;
$uid = isset($resItem['uid']) ? $resItem['uid'] : '';
$id = isset($resItem['id']) ? $resItem['id'] : '';
$hash = isset($resItem['hash']) ? $resItem['hash'] : '';
$r = $collection->setItemDataByUID($uid, $id, $hash, $data);
if(!$r)
{
/**
* @TODO error
*/
throw new Exception('Item set data error');
}
}
return true;
}
public function upload(PhotoCollection &$collection)
{
$items = $collection->getItems();
$params = $this->extendParams([
'items' => array_values( array_map(function($item){
return [
'file' => '@'.$item->getFilePath(),
'id' => $item->getId(),
'hash' => $item->getHash(),
];
}, $collection->getItems()) ),
]);
$res = $this->makeRequest('upload', $params, self::HTTP_POST);
return true;
}
}