Sfoglia il codice sorgente

Creating requests with pagination from ONU

Jean Sumara 5 anni fa
parent
commit
8e7750bd5f

+ 1 - 1
src/App/Listener/ExportListener.php

@@ -36,7 +36,7 @@ class ExportListener
     }
 
     public function run(){
-        echo 'Running listener';
+        echo "Running listener\n";
 
         $envNameQueue = getenv('FTTH_AMQP');
         if($this->getNameQueue()){

+ 107 - 16
src/App/Service/OnuService.php

@@ -7,34 +7,125 @@ use Buzz\Message\RequestInterface as HttpRequestInterface;
 
 class OnuService
 {
+
+    private $requestService;
+
+    /**
+     * OnuService constructor.
+     */
+    public function __construct()
+    {
+        $this->requestService = new RequestService();
+    }
+
     public function exportOnus($data){
-        $tenancyId = $data['tenancyId'];
-        $format = $data['format'];
-        $endpointToken = $data['endpointToken'];
-        $endpointOnus = $data['endpointOnus'];
-        $params = $data['params'];
+        try{
+            $tenancyId = $data['tenancyId'];
+            $format = $data['format'];
+            $endpointToken = $data['endpointToken'];
+            $endpointOnus = $data['endpointOnus'];
+            $params = $data['params'];
+
+            $token = $this->getToken($endpointToken, $params);
+
+            $bodyRequest = [
+                'Authorization' => "Bearer {$token['access_token']}",
+                'ClientId' => $params['client_id'],
+                'ClientSecret' => $params['client_secret'],
+                'Microservice' => 1,
+                'TenancyId' => $tenancyId
+            ];
+
+            $onus = $this->getOnus($endpointOnus, $bodyRequest);
+
+            $clientsIds = array_map(function ($onu){
+                return $onu['clientId'];
+            }, $onus);
+            $clientsIds = array_unique($clientsIds);
 
-        $requestService = new RequestService();
+            $this->makeRequestClients($clientsIds, $bodyRequest);
+
+            return true;
+        } catch (\Exception $ex){
+            echo $ex->getMessage();
+        }
+
+        return true;
+    }
+
+    /**
+     * @param $endpoint
+     * @param $params
+     * @return bool|mixed
+     * @throws \Exception
+     */
+    public function getToken($endpoint, $params){
         try {
-            $token = $requestService->makeRequest($endpointToken, HttpRequestInterface::METHOD_POST, $params);
+            $requestToken = $this->requestService->makeRequest($endpoint, HttpRequestInterface::METHOD_POST, $params);
+            if($requestToken['statusCode'] != 200){
+                echo "Não é possível fazer a requisição pro token \n";
+                return true;
+            }
+            return json_decode($requestToken['response'], true);
         } catch (\Exception $ex) {
             throw new \Exception($ex->getMessage());
         }
+    }
 
+    /**
+     * @param $endpoint
+     * @param $tenancyId
+     * @param $bodyRequest
+     * @return bool|mixed
+     * @throws \Exception
+     */
+    public function getOnus($endpoint, $bodyRequest){
+        try {
+            $page = 1;
+            $limit = 10;
+            $onus = [];
 
-        $bodyRequestOnu = [
-            'Authorization' => "Bearer {$token}",
-            'ClientId' => $params['client_id'],
-            'ClientSecret' => $params['client_secret'],
-            'Microservice' => 1
-        ];
-
+            $this->makeRequestAndInsertOnu($endpoint, $bodyRequest, $onus, $page, $limit);
 
-        try {
-            $onus = $requestService->makeRequest($endpointOnus, HttpRequestInterface::METHOD_GET, null, $bodyRequestOnu);
+            return $onus;
         } catch (\Exception $ex) {
             throw new \Exception($ex->getMessage());
         }
+    }
+
+    public function makeRequestAndInsertOnu($endpoint, $bodyRequest, &$onus, $currentPage, $limit){
+        try{
+            $bodyRequest['CurrentPage'] = $currentPage;
+            $bodyRequest['Limit'] = $limit;
+
+            $requestOnu = $this->requestService->makeRequest($endpoint, HttpRequestInterface::METHOD_GET, [], $bodyRequest);
+
+            if($requestOnu['statusCode'] != 200){
+                echo "Não é possivel fazer a requisição para as ONUs \n";
+                return true;
+            }
+            $response = json_decode($requestOnu['response'], true);
+            foreach ($response['data'] as $onu){
+                array_push($onus, $onu);
+            }
+
+            if($response['hasNext']){
+                $currentPage++;
+                return $this->makeRequestAndInsertOnu($endpoint, $bodyRequest, $onus, $currentPage, $limit);
+            }
+
+            return true;
+        }catch (\Exception $ex){
+            throw new \Exception($ex->getMessage());
+        }
+    }
+
+    public function makeRequestClients($clientsIds, $bodyRequest){
+        $paramsClient = [
+            'filters[qb-criteria]' => true,
+            'filters[qb-ids]' =>  implode(",", $clientsIds)
+        ];
 
+        $requestClient = $this->requestService->makeRequest('https://base.dev-sumara.flowdat.net/api/clients', HttpRequestInterface::METHOD_GET, $paramsClient, $bodyRequest);
     }
 }

+ 5 - 1
src/App/Service/RequestService.php

@@ -23,11 +23,15 @@ class RequestService
             $response = new HttpResponse();
             $curl->setTimeout($timeout);
             $curl->send($request, $response);
+            $statusCode = $response->getStatusCode();
             $response = $response->getContent();
         } catch (RequestException $ex) {
             throw new \Exception($ex->getMessage());
         }
 
-        return json_decode($response, true);
+        return [
+            'response' => $response,
+            'statusCode' => $statusCode
+        ];
     }
 }