@@ -34,54 +34,31 @@ class GuzzleClient implements ClientInterface
3434 protected $ guzzle ;
3535
3636 /**
37- * Base endpoint
37+ * Set the the guzzle client
3838 *
39- * @var string
39+ * @param array $options The options to set the defaul
40+ * @param Object|null $client Client to make the requests
4041 */
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 )
5943 {
60- if (!isset ( $ options [ ' base ' ]) ) {
61- throw new InvalidArgumentException ( " Missign base path " );
44+ if (!$ client ) {
45+ $ client = new HttpClient ( $ options );
6246 }
63-
64- if (!isset ($ options ['token ' ])) {
65- throw new InvalidArgumentException ("Missign token " );
66- }
67-
68- $ this ->base = $ options ['base ' ];
69- $ this ->token = $ options ['token ' ];
7047
71- $ this ->guzzle = new HttpClient ( $ options ) ;
48+ $ this ->guzzle = $ client ;
7249 }
7350
7451 /**
7552 * Create a new GET reuest
7653 *
7754 * @param string $endpoint The sub endpoint
78- * @param array $query Query parameters
55+ * @param array $headers Request headers
7956 *
8057 * @return array
8158 */
82- public function get ($ endpoint , $ query = [])
59+ public function get ($ endpoint , $ headers = [])
8360 {
84- $ request = new Request ('GET ' ,$ this -> buildUri ( $ endpoint , $ query ), $ this -> buildHeaders () );
61+ $ request = new Request ('GET ' ,$ endpoint , $ headers );
8562
8663 $ response = $ this ->guzzle ->send ($ request );
8764
@@ -93,15 +70,13 @@ public function get($endpoint, $query = [])
9370 *
9471 * @param string $endpoint The sub endpoint
9572 * @param string|array $data The data to be submited
96- * @param array $query Query parameters
73+ * @param array $headers Request headers
9774 *
9875 * @return array
9976 */
100- public function post ($ endpoint , $ data , $ query = [])
77+ public function post ($ endpoint , $ data , $ headers = [])
10178 {
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 );
10580
10681 $ response = $ this ->guzzle ->send ($ request );
10782
@@ -113,15 +88,13 @@ public function post($endpoint, $data, $query = [])
11388 *
11489 * @param string $endpoint The sub endpoint
11590 * @param string|array $data The data to be submited
116- * @param array $query Query parameters
91+ * @param array $headers Request headers
11792 *
11893 * @return array
11994 */
120- public function put ($ endpoint , $ data , $ query = [])
95+ public function put ($ endpoint , $ data , $ headers = [])
12196 {
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 );
12598
12699 $ response = $ this ->guzzle ->send ($ request );
127100
@@ -133,15 +106,13 @@ public function put($endpoint, $data, $query = [])
133106 *
134107 * @param string $endpoint The sub endpoint
135108 * @param string|array $data The data to be submited
136- * @param array $query Query parameters
109+ * @param array $headers Request headers
137110 *
138111 * @return array
139112 */
140- public function patch ($ endpoint , $ data , $ query = [])
113+ public function patch ($ endpoint , $ data , $ headers = [])
141114 {
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 );
145116
146117 $ response = $ this ->guzzle ->send ($ request );
147118
@@ -152,65 +123,20 @@ public function patch($endpoint, $data, $query = [])
152123 * Create a new DELETE reuest
153124 *
154125 * @param string $endpoint The sub endpoint
155- * @param array $query Query parameters
126+ * @param array $headers Request headers
156127 *
157128 * @return array
158129 */
159- public function delete ($ endpoint , $ query = [])
130+ public function delete ($ endpoint , $ headers = [])
160131 {
161- $ request = new Request ('DELETE ' ,$ this -> buildUri ( $ endpoint , $ query ), $ this -> buildHeaders () );
132+ $ request = new Request ('DELETE ' ,$ endpoint , $ headers );
162133
163134 $ response = $ this ->guzzle ->send ($ request );
164135
165136 return $ this ->handle ($ response );
166137 }
167138
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+
214140
215141 /**
216142 * Handle the response
0 commit comments