1313use Nexmo \Client \Credentials \CredentialsInterface ;
1414use Nexmo \Client \Credentials \Keypair ;
1515use Nexmo \Client \Credentials \OAuth ;
16- use Nexmo \Client \Credentials \SharedSecret ;
16+ use Nexmo \Client \Credentials \SignatureSecret ;
1717use Nexmo \Client \Factory \FactoryInterface ;
1818use Nexmo \Client \Factory \MapFactory ;
1919use Nexmo \Client \Response \Response ;
2222use Nexmo \Verify \Verification ;
2323use Psr \Http \Message \RequestInterface ;
2424use Zend \Diactoros \Uri ;
25+ use Zend \Diactoros \Request ;
2526
2627/**
2728 * Nexmo API Client, allows access to the API from PHP.
@@ -76,7 +77,7 @@ public function __construct(CredentialsInterface $credentials, $options = array(
7677 $ this ->setHttpClient ($ client );
7778
7879 //make sure we know how to use the credentials
79- if (!($ credentials instanceof Container) && !($ credentials instanceof Basic) && !($ credentials instanceof SharedSecret ) && !($ credentials instanceof OAuth)){
80+ if (!($ credentials instanceof Container) && !($ credentials instanceof Basic) && !($ credentials instanceof SignatureSecret ) && !($ credentials instanceof OAuth) && !( $ credentials instanceof Keypair )){
8081 throw new \RuntimeException ('unknown credentials type: ' . get_class ($ credentials ));
8182 }
8283
@@ -135,7 +136,7 @@ public function setFactory(FactoryInterface $factory)
135136 * @param Signature $signature
136137 * @return RequestInterface
137138 */
138- public static function signRequest (RequestInterface $ request , SharedSecret $ credentials )
139+ public static function signRequest (RequestInterface $ request , SignatureSecret $ credentials )
139140 {
140141 switch ($ request ->getHeaderLine ('content-type ' )){
141142 case 'application/json ' :
@@ -144,7 +145,7 @@ public static function signRequest(RequestInterface $request, SharedSecret $cred
144145 $ content = $ body ->getContents ();
145146 $ params = json_decode ($ content , true );
146147 $ params ['api_key ' ] = $ credentials ['api_key ' ];
147- $ signature = new Signature ($ params , $ credentials ['shared_secret ' ]);
148+ $ signature = new Signature ($ params , $ credentials ['signature_secret ' ]);
148149 $ body ->rewind ();
149150 $ body ->write (json_encode ($ signature ->getSignedParams ()));
150151 break ;
@@ -155,7 +156,7 @@ public static function signRequest(RequestInterface $request, SharedSecret $cred
155156 $ params = [];
156157 parse_str ($ content , $ params );
157158 $ params ['api_key ' ] = $ credentials ['api_key ' ];
158- $ signature = new Signature ($ params , $ credentials ['shared_secret ' ]);
159+ $ signature = new Signature ($ params , $ credentials ['signature_secret ' ]);
159160 $ params = $ signature ->getSignedParams ();
160161 $ body ->rewind ();
161162 $ body ->write (http_build_query ($ params , null , '& ' ));
@@ -164,7 +165,7 @@ public static function signRequest(RequestInterface $request, SharedSecret $cred
164165 $ query = [];
165166 parse_str ($ request ->getUri ()->getQuery (), $ query );
166167 $ query ['api_key ' ] = $ credentials ['api_key ' ];
167- $ signature = new Signature ($ query , $ credentials ['shared_secret ' ]);
168+ $ signature = new Signature ($ query , $ credentials ['signature_secret ' ]);
168169 $ request = $ request ->withUri ($ request ->getUri ()->withQuery (http_build_query ($ signature ->getSignedParams ())));
169170 break ;
170171 }
@@ -206,6 +207,83 @@ public static function authRequest(RequestInterface $request, Basic $credentials
206207 }
207208
208209 /**
210+ * Takes a URL and a key=>value array to generate a GET PSR-7 request object
211+ *
212+ * @param string $url The URL to make a request to
213+ * @param array $params Key=>Value array of data to use as the query string
214+ * @return \Psr\Http\Message\ResponseInterface
215+ */
216+ public function get ($ url , array $ params = [])
217+ {
218+ $ queryString = '? ' . http_build_query ($ params );
219+
220+ $ url = $ url . $ queryString ;
221+
222+ $ request = new Request (
223+ $ url ,
224+ 'GET '
225+ );
226+
227+ return $ this ->send ($ request );
228+ }
229+
230+ /**
231+ * Takes a URL and a key=>value array to generate a POST PSR-7 request object
232+ *
233+ * @param string $url The URL to make a request to
234+ * @param array $params Key=>Value array of data to send
235+ * @return \Psr\Http\Message\ResponseInterface
236+ */
237+ public function post ($ url , array $ params )
238+ {
239+ $ request = new Request (
240+ $ url ,
241+ 'POST ' ,
242+ 'php://temp ' ,
243+ ['content-type ' => 'application/json ' ]
244+ );
245+
246+ $ request ->getBody ()->write (json_encode ($ params ));
247+ return $ this ->send ($ request );
248+ }
249+
250+ /**
251+ * Takes a URL and a key=>value array to generate a PUT PSR-7 request object
252+ *
253+ * @param string $url The URL to make a request to
254+ * @param array $params Key=>Value array of data to send
255+ * @return \Psr\Http\Message\ResponseInterface
256+ */
257+ public function put ($ url , array $ params )
258+ {
259+ $ request = new Request (
260+ $ url ,
261+ 'PUT ' ,
262+ 'php://temp ' ,
263+ ['content-type ' => 'application/json ' ]
264+ );
265+
266+ $ request ->getBody ()->write (json_encode ($ params ));
267+ return $ this ->send ($ request );
268+ }
269+
270+ /**
271+ * Takes a URL and a key=>value array to generate a DELETE PSR-7 request object
272+ *
273+ * @param string $url The URL to make a request to
274+ * @return \Psr\Http\Message\ResponseInterface
275+ */
276+ public function delete ($ url )
277+ {
278+ $ request = new Request (
279+ $ url ,
280+ 'DELETE '
281+ );
282+
283+ return $ this ->send ($ request );
284+ }
285+
286+ /**
209287 * Wraps the HTTP Client, creates a new PSR-7 request adding authentication, signatures, etc.
210288 *
211289 * @param \Psr\Http\Message\RequestInterface $request
@@ -220,8 +298,8 @@ public function send(\Psr\Http\Message\RequestInterface $request)
220298 $ request = self ::authRequest ($ request , $ this ->credentials ->get (Basic::class));
221299 }
222300 } elseif ($ this ->credentials instanceof Keypair){
223- $ request = $ request ->withHeader ('Authorization ' , 'Bearer ' . $ this ->credentials ->get (Keypair::class)-> generateJwt ());
224- } elseif ($ this ->credentials instanceof SharedSecret ){
301+ $ request = $ request ->withHeader ('Authorization ' , 'Bearer ' . $ this ->credentials ->generateJwt ());
302+ } elseif ($ this ->credentials instanceof SignatureSecret ){
225303 $ request = self ::signRequest ($ request , $ this ->credentials );
226304 } elseif ($ this ->credentials instanceof Basic){
227305 $ request = self ::authRequest ($ request , $ this ->credentials );
0 commit comments