1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\Remi\OpenLibrary; |
4
|
|
|
|
5
|
|
|
use Onoi\Remi\FilteredHttpResponseParser; |
6
|
|
|
use Onoi\Remi\FilteredRecord; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @license GNU GPL v2+ |
10
|
|
|
* @since 0.1 |
11
|
|
|
* |
12
|
|
|
* @author mwjames |
13
|
|
|
*/ |
14
|
|
|
class OLFilteredHttpResponseParser extends FilteredHttpResponseParser { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @see https://openlibrary.org/dev/docs/api/books |
18
|
|
|
*/ |
19
|
|
|
const OL_REST = "https://openlibrary.org/api/"; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @since 0.1 |
23
|
|
|
* |
24
|
|
|
* {@inheritDoc} |
25
|
|
|
*/ |
26
|
3 |
|
public function getRawResponseById( $olID ) { |
27
|
3 |
|
return $this->requestResponseFor( $olID ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @since 0.1 |
32
|
|
|
* |
33
|
|
|
* {@inheritDoc} |
34
|
|
|
*/ |
35
|
5 |
|
public function doFilterResponseById( $olID ) { |
36
|
|
|
|
37
|
5 |
|
$text = $this->requestResponseFor( $olID ); |
38
|
|
|
|
39
|
5 |
|
if ( $this->httpRequest->getLastError() !== '' ) { |
40
|
1 |
|
return $this->addMessage( array( 'onoi-remi-request-error', $this->httpRequest->getLastError(), $olID ) ); |
41
|
|
|
} |
42
|
|
|
|
43
|
4 |
|
$json = json_decode( |
44
|
4 |
|
$text, |
45
|
|
|
true |
46
|
4 |
|
); |
47
|
|
|
|
48
|
4 |
|
if ( $json === null || $json === '' || $json === array() ) { |
49
|
1 |
|
return $this->addMessage( array( 'onoi-remi-response-empty', $olID ) ); |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
$this->doProcessJson( $json ); |
53
|
|
|
|
54
|
3 |
|
$this->filteredRecord->set( 'retrieved-from', 'https://openlibrary.org/' ); |
55
|
3 |
|
} |
56
|
|
|
|
57
|
3 |
|
private function doProcessJson( $json ) { |
58
|
|
|
|
59
|
3 |
|
$olBooksAPIJsonProcessor = new OLBooksAPIJsonProcessor( |
60
|
3 |
|
$this->filteredRecord |
61
|
3 |
|
); |
62
|
|
|
|
63
|
3 |
|
$olBooksAPIJsonProcessor->doProcess( $json ); |
64
|
3 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @see https://openlibrary.org/dev/docs/api/books#data |
68
|
|
|
* |
69
|
|
|
* @param string $id |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
5 |
|
private function requestResponseFor( $id ) { |
74
|
|
|
|
75
|
5 |
|
$this->httpRequest->setOption( CURLOPT_FOLLOWLOCATION, true ); |
76
|
|
|
|
77
|
5 |
|
$this->httpRequest->setOption( CURLOPT_RETURNTRANSFER, true ); |
78
|
5 |
|
$this->httpRequest->setOption( CURLOPT_FAILONERROR, true ); |
79
|
5 |
|
$this->httpRequest->setOption( CURLOPT_SSL_VERIFYPEER, false ); |
80
|
|
|
|
81
|
5 |
|
$this->httpRequest->setOption( CURLOPT_HTTPHEADER, array( |
82
|
5 |
|
'Accept: application/json', |
83
|
|
|
'Content-type: application/json; charset=utf-8' |
84
|
5 |
|
) ); |
85
|
|
|
|
86
|
5 |
|
$this->httpRequest->setOption( |
87
|
5 |
|
CURLOPT_URL, |
88
|
5 |
|
self::OL_REST . "books?bibkeys=" . 'OLID:' . $id . ',ISBN:' . $id . '&format=json&jscmd=data' ); |
89
|
|
|
|
90
|
5 |
|
return $this->httpRequest->execute(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|