Kaynağa Gözat

FD3-223 Refactory metodo get request webservice

Guillermo Espinoza 7 yıl önce
ebeveyn
işleme
9bbbea215b
1 değiştirilmiş dosya ile 17 ekleme ve 13 silme
  1. 17 13
      Services/Webservice.php

+ 17 - 13
Services/Webservice.php

@@ -109,28 +109,32 @@ class Webservice
     /**
      * @param string $url
      * @param string $method
+     * @param array $data
+     * @param array $credentials
      *
      * @return HttpResponse
      */
-    public function makeGetRequest($url, $method = HttpRequestInterface::METHOD_GET, $data = array())
+    public function makeGetRequest($url, $method = HttpRequestInterface::METHOD_GET, $data = array(), $credentials = array())
     {
-        $response = '';
+        $headers = array();
         if ($token = $this->securityTokenStorage->getToken()) {
-            $headers = array();
-            $request = new HttpRequest($method, $url);
-            if (!empty($data)) {
-                $headers[] = 'Content-Type: application/x-www-form-urlencoded';
-                $request->setContent(http_build_query($data));
-            }
-            $response = new HttpResponse();
             if (method_exists($token, 'getAccessToken')) {
                 $headers[] = 'Authorization: Bearer ' . $token->getAccessToken();
             }
-            $request->setHeaders($headers);
-
-            $this->httpClient->send($request, $response);
-            $response = $response->getContent();
+        } elseif (!empty($credentials) && isset($credentials['username']) && isset($credentials['password'])) {    
+            $headers[] = 'Authorization: Basic ' . base64_encode($credentials['username'] . ":" . $credentials['password']);
+        } else {
+            return '';
+        }
+        $request = new HttpRequest($method, $url);
+        if (!empty($data)) {
+            $headers[] = 'Content-Type: application/x-www-form-urlencoded';
+            $request->setContent(http_build_query($data));
         }
+        $request->setHeaders($headers);
+        $response = new HttpResponse();
+        $this->httpClient->send($request, $response);
+        $response = $response->getContent();
 
         return $response;
     }