1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scriptotek\PrimoSearch; |
4
|
|
|
|
5
|
|
|
use Http\Client\HttpClient; |
6
|
|
|
use Http\Discovery\HttpClientDiscovery; |
7
|
|
|
use Http\Message\MessageFactory; |
8
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
9
|
|
|
use Psr\Http\Message\RequestInterface; |
10
|
|
|
|
11
|
|
|
class Primo |
12
|
|
|
{ |
13
|
|
|
// Services |
14
|
|
|
protected $http; |
15
|
|
|
protected $messageFactory; |
16
|
|
|
|
17
|
|
|
// For hosted setup |
18
|
|
|
protected $apiKey; |
19
|
|
|
protected $region; |
20
|
|
|
|
21
|
|
|
// For on-premises setup |
22
|
|
|
protected $baseUrl; |
23
|
|
|
protected $searchUrl; |
24
|
|
|
protected $inst; |
25
|
|
|
protected $jwtToken; |
26
|
|
|
|
27
|
|
|
// Common config |
28
|
|
|
protected $userAgent = 'scriptotek/primo-search'; |
29
|
|
|
protected $vid; |
30
|
|
|
protected $scope; |
31
|
|
|
protected $lang = 'en_US'; |
32
|
|
|
|
33
|
|
|
public function __construct(array $config, HttpClient $httpClient = null, MessageFactory $messageFactory = null) |
34
|
|
|
{ |
35
|
|
|
$this->vid = $config['vid']; |
36
|
|
|
$this->scope = $config['scope']; |
37
|
|
|
|
38
|
|
|
if (isset($config['apiKey'])) { |
39
|
|
|
// Hosted |
40
|
|
|
$this->apiKey = $config['apiKey']; |
41
|
|
|
$this->region = $config['region'] ?? 'eu'; |
42
|
|
|
$this->baseUrl = "https://api-{$this->region}.hosted.exlibrisgroup.com/primo/v1/"; |
43
|
|
|
$this->searchUrl = "{$this->baseUrl}/search"; |
44
|
|
|
} else { |
45
|
|
|
// On-premises |
46
|
|
|
$this->inst = $config['inst']; |
47
|
|
|
$baseUrl = rtrim($config['baseUrl'], '/') . '/'; |
48
|
|
|
$this->baseUrl = $baseUrl; |
49
|
|
|
$this->searchUrl = $config['searchUrl'] ?? "{$this->baseUrl}/pnxs"; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->http = $httpClient ?: HttpClientDiscovery::find(); |
53
|
|
|
$this->messageFactory = $messageFactory ?: MessageFactoryDiscovery::find(); |
54
|
|
|
} |
55
|
|
|
/** |
56
|
|
|
* Set the view ID. |
57
|
|
|
* @param $vid |
58
|
|
|
*/ |
59
|
|
|
public function setVid(string $vid) |
60
|
|
|
{ |
61
|
|
|
$this->vid = $vid; |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set the search scope. |
67
|
|
|
* @param $scope |
68
|
|
|
*/ |
69
|
|
|
public function setScope(string $scope) |
70
|
|
|
{ |
71
|
|
|
$this->scope = $scope; |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function getGuestJwtToken() |
76
|
|
|
{ |
77
|
|
|
$res = $this->request($this->baseUrl . "guestJwt/{$this->inst}", [ |
78
|
|
|
'isGuest' => 'true', |
79
|
|
|
'viewId' => $this->vid, |
80
|
|
|
'lang' => $this->lang, |
81
|
|
|
]); |
82
|
|
|
|
83
|
|
|
$this->jwtToken = trim($res, '"'); |
84
|
|
|
|
85
|
|
|
return $this->jwtToken; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Make an API request. |
90
|
|
|
* @param Query $query |
91
|
|
|
* @return string |
92
|
|
|
* @throws \Http\Client\Exception |
93
|
|
|
*/ |
94
|
|
|
public function search(Query $query) |
95
|
|
|
{ |
96
|
|
|
if (!isset($this->apiKey) && !isset($this->jwtToken)) { |
97
|
|
|
$this->getGuestJwtToken(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$params = array_merge( |
101
|
|
|
[ |
102
|
|
|
'vid' => $this->vid, |
103
|
|
|
'scope' => $this->scope, |
104
|
|
|
'inst' => $this->inst, |
105
|
|
|
'lang' => $this->lang, |
106
|
|
|
'pcAvailability' => 'false', |
107
|
|
|
'mode' => 'advanced', |
108
|
|
|
'newspapersActive' => 'false', |
109
|
|
|
'newspapersSearch' => 'false', |
110
|
|
|
'skipDelivery' => 'Y', |
111
|
|
|
'tab' => 'default_tab', |
112
|
|
|
'rtaLinks' => 'true', |
113
|
|
|
], |
114
|
|
|
$query->build() |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$result = $this->request($this->searchUrl, $params); |
118
|
|
|
|
119
|
|
|
return json_decode($result) ?? $result; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function request($url, $params) |
123
|
|
|
{ |
124
|
|
|
$url .= '?' . http_build_query($params); |
125
|
|
|
|
126
|
|
|
$headers = [ |
127
|
|
|
'Accept-Encoding' => 'gzip', |
128
|
|
|
'Accept' => 'application/json', |
129
|
|
|
'User-Agent' => $this->userAgent, |
130
|
|
|
'Authorization' => isset($this->apiKey) |
131
|
|
|
? "apikey {$this->apiKey}" |
132
|
|
|
: "Bearer {$this->jwtToken}", |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
$request = $this->messageFactory->createRequest('GET', $url, $headers); |
136
|
|
|
|
137
|
|
|
$response = $this->http->sendRequest($request); |
138
|
|
|
|
139
|
|
|
return strval($response->getBody()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function setJwtToken(string $token) |
143
|
|
|
{ |
144
|
|
|
$this->jwtToken = $token; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function getJwtToken() |
148
|
|
|
{ |
149
|
|
|
if (isset($this->jwtToken)) { |
150
|
|
|
return $this->jwtToken; |
151
|
|
|
} |
152
|
|
|
return $this->getGuestJwtToken(); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|