1
1
<?php namespace Scriptotek \Sru ;
2
2
3
- use GuzzleHttp \Client as HttpClient ;
3
+ use Http \Client \Common \Plugin \AuthenticationPlugin ;
4
+ use Http \Client \Common \PluginClient ;
5
+ use Http \Client \HttpClient ;
6
+ use Http \Discovery \HttpClientDiscovery ;
7
+ use Http \Client \Common \Plugin \ErrorPlugin ;
8
+ use Http \Discovery \MessageFactoryDiscovery ;
9
+ use Http \Message \Authentication \BasicAuth ;
10
+ use Http \Message \MessageFactory ;
4
11
5
12
/**
6
13
* SRU client
@@ -10,6 +17,9 @@ class Client
10
17
/** @var HttpClient */
11
18
protected $ httpClient ;
12
19
20
+ /** @var MessageFactory */
21
+ protected $ messageFactory ;
22
+
13
23
/** @var string SRU service base URL */
14
24
protected $ url ;
15
25
@@ -38,15 +48,22 @@ class Client
38
48
/**
39
49
* Create a new client
40
50
*
41
- * @param string $url Base URL to the SRU service
42
- * @param array $options Associative array of options
43
- * @param HttpClient $httpClient
51
+ * @param string $url Base URL to the SRU service
52
+ * @param array $options Associative array of options
53
+ * @param HttpClient $httpClient
54
+ * @param MessageFactory|null $messageFactory
55
+ * @throws \ErrorException
44
56
*/
45
- public function __construct ($ url , $ options = null , $ httpClient = null )
46
- {
57
+ public function __construct (
58
+ $ url ,
59
+ $ options = null ,
60
+ HttpClient $ httpClient = null ,
61
+ MessageFactory $ messageFactory = null
62
+ ) {
47
63
$ this ->url = $ url ;
48
64
$ options = $ options ?: array ();
49
- $ this ->httpClient = $ httpClient ?: new HttpClient ;
65
+
66
+ $ plugins = [new ErrorPlugin ()];
50
67
51
68
$ this ->schema = isset ($ options ['schema ' ])
52
69
? $ options ['schema ' ]
@@ -60,13 +77,17 @@ public function __construct($url, $options = null, $httpClient = null)
60
77
? $ options ['user-agent ' ]
61
78
: null ;
62
79
63
- $ this ->credentials = isset ($ options ['credentials ' ])
64
- ? $ options ['credentials ' ]
65
- : null ;
80
+ if (isset ($ options ['credentials ' ])) {
81
+ $ authentication = new BasicAuth ($ options ['credentials ' ][0 ], $ options ['credentials ' ][1 ]);
82
+ $ plugins [] = new AuthenticationPlugin ($ authentication );
83
+ }
66
84
67
- $ this ->proxy = isset ($ options ['proxy ' ])
68
- ? $ options ['proxy ' ]
69
- : null ;
85
+ if (isset ($ options ['proxy ' ])) {
86
+ throw new \ErrorException ('Not supported ' );
87
+ }
88
+
89
+ $ this ->httpClient = new PluginClient ($ httpClient ?: HttpClientDiscovery::find (), $ plugins );
90
+ $ this ->messageFactory = $ messageFactory ?: MessageFactoryDiscovery::find ();
70
91
}
71
92
72
93
/**
@@ -106,24 +127,16 @@ public function urlTo($cql, $start = 1, $count = 10, $extraParams = array())
106
127
*
107
128
* @return array
108
129
*/
109
- public function getHttpOptions ()
130
+ public function getHttpHeaders ()
110
131
{
111
132
$ headers = array (
112
133
'Accept ' => 'application/xml '
113
134
);
114
135
if ($ this ->userAgent ) {
115
136
$ headers ['User-Agent ' ] = $ this ->userAgent ;
116
137
}
117
- $ options = array (
118
- 'headers ' => $ headers
119
- );
120
- if ($ this ->credentials ) {
121
- $ options ['auth ' ] = $ this ->credentials ;
122
- }
123
- if ($ this ->proxy ) {
124
- $ options ['proxy ' ] = $ this ->proxy ;
125
- }
126
- return $ options ;
138
+
139
+ return $ headers ;
127
140
}
128
141
129
142
/**
@@ -139,10 +152,7 @@ public function getHttpOptions()
139
152
public function search ($ cql , $ start = 1 , $ count = 10 , $ extraParams = array ())
140
153
{
141
154
$ url = $ this ->urlTo ($ cql , $ start , $ count , $ extraParams );
142
- $ options = $ this ->getHttpOptions ();
143
-
144
- $ response = $ this ->httpClient ->get ($ url , $ options );
145
- $ body = (string ) $ response ->getBody ();
155
+ $ body = $ this ->request ('GET ' , $ url );
146
156
147
157
return new SearchRetrieveResponse ($ body , $ this );
148
158
}
@@ -200,11 +210,22 @@ public function explain()
200
210
'operation ' => 'explain ' ,
201
211
'version ' => $ this ->version ,
202
212
));
203
- $ options = $ this ->getHttpOptions ();
204
213
205
- $ response = $ this ->httpClient ->get ($ url , $ options );
206
- $ body = (string ) $ response ->getBody ();
214
+ $ body = $ this ->request ('GET ' , $ url );
207
215
208
216
return new ExplainResponse ($ body , $ this );
209
217
}
218
+
219
+ /**
220
+ * @param string $method
221
+ * @param string $url
222
+ * @return string
223
+ */
224
+ public function request ($ method , $ url )
225
+ {
226
+ $ request = $ this ->messageFactory ->createRequest ($ method , $ url , $ this ->getHttpHeaders ());
227
+ $ response = $ this ->httpClient ->sendRequest ($ request );
228
+
229
+ return (string ) $ response ->getBody ();
230
+ }
210
231
}
0 commit comments