Skip to content
This repository was archived by the owner on May 24, 2019. It is now read-only.

Commit 33b18d3

Browse files
Upgrade HTTP Client to Guzzle 6
* Initial work to upgrade to Guzzle 6 * Made logging customizable * Updated documentation * Adding documentation
1 parent 791526e commit 33b18d3

54 files changed

Lines changed: 516 additions & 276 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Sample Composer file
2323
}
2424
],
2525
"require" : {
26-
"worldcat/discovery" : ">=0.7"
26+
"worldcat/discovery" : "^1.0"
2727
}
2828
}
2929
```

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
{
22
"name" : "worldcat/discovery",
3-
"version" : "0.17.1",
3+
"version" : "1.0.0",
44
"license" : "Apache-2.0",
55
"repositories" : [{
66
"type" : "git",
77
"url" : "https://github.com/OCLC-Developer-Network/oclc-auth-php.git"
88
}
99
],
1010
"require" : {
11-
"php" : ">=5.4.0",
11+
"php" : ">=5.6",
1212
"lib-curl" : "*",
1313
"lib-openssl" : "*",
1414
"ext-curl" : "*",
1515
"ext-json" : "*",
1616
"ext-openssl" : "*",
17-
"guzzle/guzzle" : ">=3.7.0,<3.9.0",
17+
"guzzlehttp/guzzle" : "^6",
1818
"easyrdf/easyrdf" : "*",
19-
"OCLC/Auth" : "2.0"
19+
"OCLC/Auth" : "^4.0"
2020
},
2121
"require-dev" : {
22-
"phpunit/phpunit" : "~3",
23-
"php-vcr/php-vcr" : ">=1.2.8",
22+
"phpunit/phpunit" : "^5",
23+
"php-vcr/php-vcr" : "1.3.1",
2424
"php-vcr/phpunit-testlistener-vcr" : "*",
2525
"monolog/monolog" : "*",
2626
"phpdocumentor/phpdocumentor" : "2.*"

docs/enablingLogging.rst

Lines changed: 98 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
Logging
22
============
33

4-
The library allows logging to be added via Guzzle's Log Plugin [http://guzzle3.readthedocs.org/plugins/log-plugin.html] which supports logs via a variety of adapters.
4+
The library allows logging to be added via Guzzle which supports any logging tool which implements a PSR-3 interface.
5+
One can specify the log format using substitutions see [https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php].
56

67
Log Message Format
78
==================
89
We use the follow message logging for our testing purposes
910

10-
{host} {method} {resource} {req_header_Authorization} \n {code} {reason} {res_header_X-OCLC-RequestId} {res_header_X-OCLC-SelfId}
11+
{host} {method} {target} {req_header_authorization} \n {code} {phrase} {res_header_x-OCLC-RequestId} {res_header_x-OCLC-SelfId} {error}
1112

1213
Example:
1314
==================================================
1415

15-
This example adds basic logging using the Zend Framework 2 Logging [http://framework.zend.com/manual/2.3/en/modules/zend.log.overview.html] and sending output to the buffer
16+
This example adds basic logging using Monolog Logging [https://github.com/Seldaek/monolog] and sending output to the buffer
1617

1718
.. code:: php
1819
@@ -22,32 +23,29 @@ This example adds basic logging using the Zend Framework 2 Logging [http://frame
2223
use OCLC\Auth\AccessToken;
2324
use WorldCat\Discovery\Bib;
2425
25-
use Guzzle\Plugin\Log\LogPlugin;
26-
use Guzzle\Log\MessageFormatter;
27-
use Guzzle\Log\Zf2LogAdapter;
28-
use Zend\Log\Logger;
29-
use Zend\Log\Writer\Stream
26+
use Monolog\Logger;
27+
use Monolog\Handler\StreamHandler;
3028
3129
$key = 'api-key';
3230
$secret = 'api-key-secret';
3331
$options = array('services' => array('WorldCatDiscoveryAPI', 'refresh_token'));
3432
$wskey = new WSKey($key, $secret, $options);
3533
$accessToken = $wskey->getAccessTokenWithClientCredentials('128807', '128807'));
3634
37-
$logwriter = new Stream('php://output');
38-
$logger = new Logger();
39-
$logger->addWriter($logwriter);
40-
$adapter = new Zf2LogAdapter($logger);
41-
$logPlugin = new LogPlugin($adapter, MessageFormatter::DEBUG_FORMAT);
42-
$options = array(
43-
'logger' => $logPlugin
44-
);
35+
$logger = new Logger('discoveryAPILog');
36+
$handler = new StreamHandler(php://output, Logger::DEBUG);
37+
$logger->pushHandler($handler);
38+
$options = array(
39+
'logger' => $logger,
40+
'log_format' => 'Request - {host} {method} {target} {req_header_authorization} \n Response - {code} {phrase} {res_header_x-OCLC-RequestId} {res_header_x-OCLC-SelfId} {error}'
41+
);
42+
4543
$bib = Bib::find(7977212, $accessToken, $options);
4644
4745
Example:
4846
==================================================
4947

50-
This example adds basic logging using the Zend Framework 2 Logging [http://framework.zend.com/manual/2.3/en/modules/zend.log.overview.html] and sending output to the filesystem
48+
This example adds basic logging using the Monolog Logging [https://github.com/Seldaek/monolog] and sending output to the filesystem
5149

5250
.. code:: php
5351
@@ -57,24 +55,95 @@ This example adds basic logging using the Zend Framework 2 Logging [http://frame
5755
use OCLC\Auth\AccessToken;
5856
use WorldCat\Discovery\Bib;
5957
60-
use Guzzle\Plugin\Log\LogPlugin;
61-
use Guzzle\Log\MessageFormatter;
62-
use Guzzle\Log\Zf2LogAdapter;
63-
use Zend\Log\Logger;
64-
use Zend\Log\Writer\Stream
58+
use Monolog\Logger;
59+
use Monolog\Handler\StreamHandler;
6560
6661
$key = 'api-key';
6762
$secret = 'api-key-secret';
6863
$options = array('services' => array('WorldCatDiscoveryAPI', 'refresh_token'));
6964
$wskey = new WSKey($key, $secret, $options);
7065
$accessToken = $wskey->getAccessTokenWithClientCredentials('128807', '128807'));
7166
72-
$logwriter = new Stream('/path/to/logfile');
73-
$logger = new Logger();
74-
$logger->addWriter($logwriter);
75-
$adapter = new Zf2LogAdapter($logger);
76-
$logPlugin = new LogPlugin($adapter, MessageFormatter::DEBUG_FORMAT);
67+
$logger = new Logger('discoveryAPILog');
68+
$handler = new StreamHandler(__DIR__.'/my_app.log', Logger::DEBUG);
69+
$logger->pushHandler($handler);
7770
$options = array(
78-
'logger' => $logPlugin
71+
'logger' => $logger,
72+
'log_format' => Request - {host} {method} {target} {req_header_authorization} \n Response - {code} {phrase} {res_header_x-OCLC-RequestId} {res_header_x-OCLC-SelfId} {error}'
7973
);
80-
$bib = Bib::find(7977212, $accessToken, $options);
74+
75+
$bib = Bib::find(7977212, $accessToken, $options);
76+
77+
Example:
78+
==================================================
79+
80+
This example adds basic logging using Zend Framework 2 Logging [http://framework.zend.com/manual/2.3/en/modules/zend.log.overview.html] and sending output to the buffer
81+
82+
.. code:: php
83+
84+
use OCLC\Auth\WSKey;
85+
use OCLC\Auth\AccessToken;
86+
use WorldCat\Discovery\Bib;
87+
use Zend\Log\Logger;
88+
use Zend\Log\PsrLoggerAdapter;
89+
use Zend\Log\Writer\Stream;
90+
91+
$key = 'api-key';
92+
$secret = 'api-key-secret';
93+
$options = array('services' => array('WorldCatDiscoveryAPI', 'refresh_token'));
94+
$wskey = new WSKey($key, $secret, $options);
95+
$accessToken = $wskey->getAccessTokenWithClientCredentials('128807', '128807'));
96+
97+
$logMock = new Mock();
98+
$logger = new Logger();
99+
$logger->addWriter($logMock);
100+
101+
$writer = new Stream('php://output');
102+
$logger = new Logger();
103+
$logger->addWriter($writer);
104+
105+
$psrLogger = new PsrLoggerAdapter($logger);
106+
107+
$options = array(
108+
'logger' => $psrLogger,
109+
'log_format' => Request - {host} {method} {target} {req_header_authorization} \n Response - {code} {phrase} {res_header_x-OCLC-RequestId} {res_header_x-OCLC-SelfId} {error}'
110+
);
111+
112+
$bib = Bib::find(7977212, $accessToken, $options);
113+
114+
Example:
115+
==================================================
116+
117+
This example adds basic logging using Zend Framework 2 Logging [http://framework.zend.com/manual/2.3/en/modules/zend.log.overview.html] and sending output to the filesystem
118+
119+
.. code:: php
120+
121+
use OCLC\Auth\WSKey;
122+
use OCLC\Auth\AccessToken;
123+
use WorldCat\Discovery\Bib;
124+
use Zend\Log\Logger;
125+
use Zend\Log\PsrLoggerAdapter;
126+
use Zend\Log\Writer\Stream;
127+
128+
$key = 'api-key';
129+
$secret = 'api-key-secret';
130+
$options = array('services' => array('WorldCatDiscoveryAPI', 'refresh_token'));
131+
$wskey = new WSKey($key, $secret, $options);
132+
$accessToken = $wskey->getAccessTokenWithClientCredentials('128807', '128807'));
133+
134+
$logMock = new Mock();
135+
$logger = new Logger();
136+
$logger->addWriter($logMock);
137+
138+
$writer = new Stream(__DIR__.'/my_app.log');
139+
$logger = new Logger();
140+
$logger->addWriter($writer);
141+
142+
$psrLogger = new PsrLoggerAdapter($logger);
143+
144+
$options = array(
145+
'logger' => $psrLogger,
146+
'log_format' => Request - {host} {method} {target} {req_header_authorization} \n Response - {code} {phrase} {res_header_x-OCLC-RequestId} {res_header_x-OCLC-SelfId} {error}'
147+
);
148+
149+
$bib = Bib::find(7977212, $accessToken, $options);

src/Authority.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
namespace WorldCat\Discovery;
1717

1818
use \EasyRdf_Format;
19+
use GuzzleHttp\HandlerStack,
20+
GuzzleHttp\Middleware,
21+
GuzzleHttp\MessageFormatter,
22+
GuzzleHttp\Client,
23+
GuzzleHttp\Exception\RequestException,
24+
GuzzleHttp\Psr7\Response,
25+
GuzzleHttp\Psr7;
1926

2027
/**
2128
* A class that represents a MADS Authority
@@ -31,8 +38,8 @@ class Authority extends Thing
3138
*/
3239
public function load($format = null)
3340
{
34-
$guzzleOptions = static::getGuzzleOptions(array('accept' => 'application/rdf+xml'));
35-
$responseBody = \Guzzle::get($this->getURI(), $guzzleOptions)->getBody(true);
41+
$client = new Client(static::getGuzzleOptions(array('accept' => 'application/rdf+xml')));
42+
$responseBody = $client->get($this->getURI())->getBody();
3643
$this->graph->parse($responseBody);
3744
}
3845

src/Bib.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
use \EasyRdf_Graph;
1919
use \EasyRdf_Resource;
2020
use \EasyRdf_TypeMapper;
21+
use GuzzleHttp\HandlerStack,
22+
GuzzleHttp\Middleware,
23+
GuzzleHttp\MessageFormatter,
24+
GuzzleHttp\Client,
25+
GuzzleHttp\Exception\RequestException,
26+
GuzzleHttp\Psr7\Response,
27+
GuzzleHttp\Psr7;
2128

2229
/**
2330
* A class that represents a Bibliographic Resource in WorldCat
@@ -83,9 +90,11 @@ public static function find($id, $accessToken, $options = null)
8390
$parsedOptions = static::parseOptions($options, $validRequestOptions);
8491
$requestOptions = $parsedOptions['requestOptions'];
8592
$logger = $parsedOptions['logger'];
93+
$log_format = $parsedOptions['log_format'];
8694
} else {
8795
$requestOptions = array();
8896
$logger = null;
97+
$log_format = null;
8998
}
9099

91100
if (!is_numeric($id)){
@@ -96,22 +105,20 @@ public static function find($id, $accessToken, $options = null)
96105

97106
static::requestSetup();
98107

99-
100-
101-
$guzzleOptions = static::getGuzzleOptions(array('accessToken' => $accessToken, 'logger' => $logger));
108+
$client = new Client(static::getGuzzleOptions(array('accessToken' => $accessToken, 'logger' => $logger, 'log_format' => $log_format)));
102109

103110
$bibURI = Bib::$serviceUrl . '/bib/data/' . $id;
104111

105112
if (!empty($requestOptions)){
106113
$bibURI .= '?' . static::buildParameters(null, $requestOptions);
107114
}
108115
try {
109-
$response = \Guzzle::get($bibURI, $guzzleOptions);
116+
$response = $client->get($bibURI);
110117
$graph = new EasyRdf_Graph();
111-
$graph->parse($response->getBody(true));
118+
$graph->parse($response->getBody());
112119
$bib = $graph->resource('http://www.worldcat.org/title/-/oclc/' . $id);
113120
return $bib->getCreativeWork();
114-
} catch (\Guzzle\Http\Exception\BadResponseException $error) {
121+
} catch (RequestException $error) {
115122
return Error::parseError($error);
116123
}
117124
}
@@ -179,9 +186,11 @@ public static function search($query, $accessToken, $options = null)
179186
$parsedOptions = static::parseOptions($options, $validRequestOptions);
180187
$requestOptions = $parsedOptions['requestOptions'];
181188
$logger = $parsedOptions['logger'];
189+
$log_format = $parsedOptions['log_format'];
182190
} else {
183191
$requestOptions = array();
184192
$logger = null;
193+
$log_format = null;
185194
}
186195

187196
if (!is_string($query)){
@@ -194,7 +203,7 @@ public static function search($query, $accessToken, $options = null)
194203

195204
static::requestSetup();
196205

197-
$guzzleOptions = static::getGuzzleOptions(array('accessToken' => $accessToken, 'logger' => $logger));
206+
$client = new Client(static::getGuzzleOptions(array('accessToken' => $accessToken, 'logger' => $logger, 'log_format' => $log_format)));
198207

199208
if (empty($requestOptions['dbIds'])){
200209
$requestOptions['dbIds'] = 638;
@@ -203,13 +212,13 @@ public static function search($query, $accessToken, $options = null)
203212
$bibSearchURI = Bib::$serviceUrl . '/bib/search?' . static::buildParameters($query, $requestOptions);
204213

205214
try {
206-
$searchResponse = \Guzzle::get($bibSearchURI, $guzzleOptions);
215+
$searchResponse = $client->get($bibSearchURI);
207216
$graph = new EasyRdf_Graph();
208-
$graph->parse($searchResponse->getBody(true));
217+
$graph->parse($searchResponse->getBody());
209218
$search = $graph->allOfType('discovery:SearchResults');
210219
$search = $search[0];
211220
return $search;
212-
} catch (\Guzzle\Http\Exception\BadResponseException $error) {
221+
} catch (RequestException $error) {
213222
return Error::parseError($error);
214223
}
215224
}

0 commit comments

Comments
 (0)