1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\CKANRegistry\Service; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Request; |
6
|
|
|
use RuntimeException; |
7
|
|
|
use SilverStripe\CKANRegistry\Model\Resource; |
8
|
|
|
use SilverStripe\Core\Extensible; |
9
|
|
|
use SilverStripe\Core\Injector\Injectable; |
10
|
|
|
|
11
|
|
|
class Client implements ClientInterface |
12
|
|
|
{ |
13
|
|
|
use Extensible; |
14
|
|
|
use Injectable; |
15
|
|
|
|
16
|
|
|
private static $dependencies = [ |
17
|
|
|
'GuzzleClient' => '%$' . \GuzzleHttp\Client::class, |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \GuzzleHttp\Client |
22
|
|
|
*/ |
23
|
|
|
protected $guzzleClient; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Instance cache for repeated calls within the same request |
27
|
|
|
* |
28
|
|
|
* @var array[] |
29
|
|
|
*/ |
30
|
|
|
protected $cache; |
31
|
|
|
|
32
|
|
|
public function getPackage(Resource $resource) |
33
|
|
|
{ |
34
|
|
|
return $this->getData($resource, 'package_show', 'DataSet'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getSearchData(Resource $resource) |
38
|
|
|
{ |
39
|
|
|
return $this->getData($resource, 'datastore_search'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getData(Resource $resource, $action = 'datastore_search', $id = 'Identifier') |
43
|
|
|
{ |
44
|
|
|
$endpoint = sprintf( |
45
|
|
|
'%s/api/%s/action/%s?id=%s', |
46
|
|
|
trim($resource->Endpoint, '/'), |
47
|
|
|
ClientInterface::API_VERSION, |
48
|
|
|
$action, |
49
|
|
|
$resource->{$id} |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
// Check for a cached result |
53
|
|
|
if (isset($this->cache[$endpoint])) { |
54
|
|
|
return $this->cache[$endpoint]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$request = new Request('GET', $endpoint); |
58
|
|
|
$response = $this->getGuzzleClient()->send($request, $this->getClientOptions()); |
59
|
|
|
|
60
|
|
|
$statusCode = $response->getStatusCode(); |
61
|
|
|
if ($statusCode < 200 || $statusCode >= 300) { |
62
|
|
|
throw new RuntimeException('CKAN API is not available. Error code ' . $statusCode); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (!count(preg_grep('#application/json#', $response->getHeader('Content-Type')))) { |
66
|
|
|
throw new RuntimeException('CKAN API returns an invalid response: Content-Type is not JSON'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$responseBody = json_decode($response->getBody()->getContents(), true); |
70
|
|
|
|
71
|
|
|
if (!$responseBody || !isset($responseBody['success']) || !$responseBody['success']) { |
72
|
|
|
throw new RuntimeException('CKAN API returns an invalid response: Responded as invalid'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Store cached result for subsequent calls within the same request |
76
|
|
|
$this->cache[$endpoint] = $responseBody; |
77
|
|
|
|
78
|
|
|
return $responseBody; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return \GuzzleHttp\Client |
83
|
|
|
*/ |
84
|
|
|
protected function getGuzzleClient() |
85
|
|
|
{ |
86
|
|
|
return $this->guzzleClient; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param \GuzzleHttp\Client $guzzleClient |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
|
|
public function setGuzzleClient(\GuzzleHttp\Client $guzzleClient) |
94
|
|
|
{ |
95
|
|
|
$this->guzzleClient = $guzzleClient; |
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get Guzzle client options |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
protected function getClientOptions() |
105
|
|
|
{ |
106
|
|
|
$options = [ |
107
|
|
|
'http_errors' => false, |
108
|
|
|
]; |
109
|
|
|
$this->extend('updateClientOptions', $options); |
110
|
|
|
return $options; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|