@@ -34,54 +34,31 @@ class GuzzleClient implements ClientInterface
34
34
protected $ guzzle ;
35
35
36
36
/**
37
- * Base endpoint
37
+ * Set the the guzzle client
38
38
*
39
- * @var string
39
+ * @param array $options The options to set the defaul
40
+ * @param Object|null $client Client to make the requests
40
41
*/
41
- protected $ base ;
42
-
43
- /**
44
- * Token
45
- *
46
- * @var string
47
- */
48
- protected $ token ;
49
-
50
- /**
51
- * Set the base path for Firebase endpont
52
- * the token to authenticate and the guzzle client
53
- *
54
- * @param string $base The base endpoint
55
- * @param string $token The token
56
- * @param \PhpFirebase\Interfaces\ClientInterface|null $client Client to make the request
57
- */
58
- public function __construct (array $ options = [])
42
+ public function __construct (array $ options = [], $ client = null )
59
43
{
60
- if (!isset ( $ options [ ' base ' ]) ) {
61
- throw new InvalidArgumentException ( " Missign base path " );
44
+ if (!$ client ) {
45
+ $ client = new HttpClient ( $ options );
62
46
}
63
-
64
- if (!isset ($ options ['token ' ])) {
65
- throw new InvalidArgumentException ("Missign token " );
66
- }
67
-
68
- $ this ->base = $ options ['base ' ];
69
- $ this ->token = $ options ['token ' ];
70
47
71
- $ this ->guzzle = new HttpClient ( $ options ) ;
48
+ $ this ->guzzle = $ client ;
72
49
}
73
50
74
51
/**
75
52
* Create a new GET reuest
76
53
*
77
54
* @param string $endpoint The sub endpoint
78
- * @param array $query Query parameters
55
+ * @param array $headers Request headers
79
56
*
80
57
* @return array
81
58
*/
82
- public function get ($ endpoint , $ query = [])
59
+ public function get ($ endpoint , $ headers = [])
83
60
{
84
- $ request = new Request ('GET ' ,$ this -> buildUri ( $ endpoint , $ query ), $ this -> buildHeaders () );
61
+ $ request = new Request ('GET ' ,$ endpoint , $ headers );
85
62
86
63
$ response = $ this ->guzzle ->send ($ request );
87
64
@@ -93,15 +70,13 @@ public function get($endpoint, $query = [])
93
70
*
94
71
* @param string $endpoint The sub endpoint
95
72
* @param string|array $data The data to be submited
96
- * @param array $query Query parameters
73
+ * @param array $headers Request headers
97
74
*
98
75
* @return array
99
76
*/
100
- public function post ($ endpoint , $ data , $ query = [])
77
+ public function post ($ endpoint , $ data , $ headers = [])
101
78
{
102
- $ data = $ this ->prepareData ($ data );
103
-
104
- $ request = new Request ('POST ' ,$ this ->buildUri ($ endpoint , $ query ),$ this ->buildHeaders (),$ data );
79
+ $ request = new Request ('POST ' ,$ endpoint , $ headers , $ data );
105
80
106
81
$ response = $ this ->guzzle ->send ($ request );
107
82
@@ -113,15 +88,13 @@ public function post($endpoint, $data, $query = [])
113
88
*
114
89
* @param string $endpoint The sub endpoint
115
90
* @param string|array $data The data to be submited
116
- * @param array $query Query parameters
91
+ * @param array $headers Request headers
117
92
*
118
93
* @return array
119
94
*/
120
- public function put ($ endpoint , $ data , $ query = [])
95
+ public function put ($ endpoint , $ data , $ headers = [])
121
96
{
122
- $ data = $ this ->prepareData ($ data );
123
-
124
- $ request = new Request ('PUT ' ,$ this ->buildUri ($ endpoint , $ query ),$ this ->buildHeaders (),$ data );
97
+ $ request = new Request ('PUT ' ,$ endpoint , $ headers , $ data );
125
98
126
99
$ response = $ this ->guzzle ->send ($ request );
127
100
@@ -133,15 +106,13 @@ public function put($endpoint, $data, $query = [])
133
106
*
134
107
* @param string $endpoint The sub endpoint
135
108
* @param string|array $data The data to be submited
136
- * @param array $query Query parameters
109
+ * @param array $headers Request headers
137
110
*
138
111
* @return array
139
112
*/
140
- public function patch ($ endpoint , $ data , $ query = [])
113
+ public function patch ($ endpoint , $ data , $ headers = [])
141
114
{
142
- $ data = $ this ->prepareData ($ data );
143
-
144
- $ request = new Request ('PATCH ' ,$ this ->buildUri ($ endpoint , $ query ),$ this ->buildHeaders (),$ data );
115
+ $ request = new Request ('PATCH ' ,$ endpoint , $ headers , $ data );
145
116
146
117
$ response = $ this ->guzzle ->send ($ request );
147
118
@@ -152,65 +123,20 @@ public function patch($endpoint, $data, $query = [])
152
123
* Create a new DELETE reuest
153
124
*
154
125
* @param string $endpoint The sub endpoint
155
- * @param array $query Query parameters
126
+ * @param array $headers Request headers
156
127
*
157
128
* @return array
158
129
*/
159
- public function delete ($ endpoint , $ query = [])
130
+ public function delete ($ endpoint , $ headers = [])
160
131
{
161
- $ request = new Request ('DELETE ' ,$ this -> buildUri ( $ endpoint , $ query ), $ this -> buildHeaders () );
132
+ $ request = new Request ('DELETE ' ,$ endpoint , $ headers );
162
133
163
134
$ response = $ this ->guzzle ->send ($ request );
164
135
165
136
return $ this ->handle ($ response );
166
137
}
167
138
168
- /**
169
- * Convert array|string to json
170
- *
171
- * @param array $data Data to be converted
172
- *
173
- * @return array
174
- */
175
- protected function prepareData ($ data = [])
176
- {
177
- return json_encode ($ data );
178
- }
179
-
180
- /**
181
- * Create a standard uri based on the end point
182
- * and add the auth token
183
- *
184
- * @param string $endpoint The sub endpoint
185
- * @param array $options Extra options to be added
186
- *
187
- * @return string
188
- */
189
- protected function buildUri ($ endpoint , $ options = [])
190
- {
191
- if ($ this ->token !== '' ) {
192
- $ options ['auth ' ] = $ this ->token ;
193
- }
194
-
195
- return $ this ->base . '/ ' . ltrim ($ endpoint , '/ ' ) . '.json? ' . http_build_query ($ options , '' , '& ' );
196
- }
197
-
198
- /**
199
- * Build all headers
200
- *
201
- * @param array $extraHeaders Extra headers to be added
202
- *
203
- * @return array
204
- */
205
- protected function buildHeaders ($ extraHeaders = [])
206
- {
207
- $ headers = [
208
- 'Accept ' => 'application/json ' ,
209
- 'Content-Type: application/json ' ,
210
- ];
211
-
212
- return array_merge ($ headers , $ extraHeaders );
213
- }
139
+
214
140
215
141
/**
216
142
* Handle the response
0 commit comments