Przeglądaj źródła

Created listener and request to get Token and Onus

Jean Sumara 5 lat temu
rodzic
commit
5eecea9e63

+ 5 - 0
.env.example

@@ -0,0 +1,5 @@
+HOST_AMQP=
+PORT_AMQP=
+USER_AMQP=
+PASS_AMQP=
+FTTH_AMQP=

+ 3 - 1
composer.json

@@ -12,7 +12,9 @@
     "vlucas/phpdotenv": "*",
     "php-amqplib/php-amqplib": ">=2.6.1",
     "doctrine/orm": "^2.5",
-    "symfony/yaml": "2.*"
+    "symfony/yaml": "2.*",
+    "kriswallsmith/buzz": "v0.13",
+    "ext-json": "*"
   },
   "autoload": {
     "psr-4": {

+ 49 - 1
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
         "This file is @generated automatically"
     ],
-    "content-hash": "f021b783ac62b6cc14a2b5c8b6c62951",
+    "content-hash": "566894b83b7ad87ceeb7cd82e64b0c63",
     "packages": [
         {
             "name": "doctrine/annotations",
@@ -908,6 +908,54 @@
             ],
             "time": "2020-03-27T11:06:43+00:00"
         },
+        {
+            "name": "kriswallsmith/buzz",
+            "version": "v0.13",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/kriswallsmith/Buzz.git",
+                "reference": "487760b05d6269a4c2c374364325326cfa65b12c"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/kriswallsmith/Buzz/zipball/487760b05d6269a4c2c374364325326cfa65b12c",
+                "reference": "487760b05d6269a4c2c374364325326cfa65b12c",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=5.3.0"
+            },
+            "require-dev": {
+                "phpunit/phpunit": "3.7.*"
+            },
+            "suggest": {
+                "ext-curl": "*"
+            },
+            "type": "library",
+            "autoload": {
+                "psr-0": {
+                    "Buzz": "lib/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Kris Wallsmith",
+                    "email": "kris.wallsmith@gmail.com",
+                    "homepage": "http://kriswallsmith.net/"
+                }
+            ],
+            "description": "Lightweight HTTP client",
+            "homepage": "https://github.com/kriswallsmith/Buzz",
+            "keywords": [
+                "curl",
+                "http client"
+            ],
+            "time": "2014-09-15T12:42:36+00:00"
+        },
         {
             "name": "ocramius/package-versions",
             "version": "1.4.2",

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

@@ -4,6 +4,7 @@
 namespace Flowdat\Export\App\Listener;
 
 
+use Flowdat\Export\App\Service\OnuService;
 use PhpAmqpLib\Connection\AMQPStreamConnection;
 
 class ExportListener
@@ -44,7 +45,8 @@ class ExportListener
         $callbackMessage = function($msg) {
             $msgBody = unserialize($msg->body);
 
-            var_dump($msgBody);
+            $onuService = new OnuService();
+            $onuService->exportOnus($msgBody);
         };
 
         $this->channelAMQP->queue_declare($envNameQueue, false, true ,false, false);

+ 40 - 0
src/App/Service/OnuService.php

@@ -0,0 +1,40 @@
+<?php
+
+
+namespace Flowdat\Export\App\Service;
+
+use Buzz\Message\RequestInterface as HttpRequestInterface;
+
+class OnuService
+{
+    public function exportOnus($data){
+        $tenancyId = $data['tenancyId'];
+        $format = $data['format'];
+        $endpointToken = $data['endpointToken'];
+        $endpointOnus = $data['endpointOnus'];
+        $params = $data['params'];
+
+        $requestService = new RequestService();
+        try {
+            $token = $requestService->makeRequest($endpointToken, HttpRequestInterface::METHOD_POST, $params);
+        } catch (\Exception $ex) {
+            throw new \Exception($ex->getMessage());
+        }
+
+
+        $bodyRequestOnu = [
+            'Authorization' => "Bearer {$token}",
+            'ClientId' => $params['client_id'],
+            'ClientSecret' => $params['client_secret'],
+            'Microservice' => 1
+        ];
+
+
+        try {
+            $onus = $requestService->makeRequest($endpointOnus, HttpRequestInterface::METHOD_GET, null, $bodyRequestOnu);
+        } catch (\Exception $ex) {
+            throw new \Exception($ex->getMessage());
+        }
+
+    }
+}

+ 33 - 0
src/App/Service/RequestService.php

@@ -0,0 +1,33 @@
+<?php
+
+
+namespace Flowdat\Export\App\Service;
+
+use Buzz\Client\Curl;
+use Buzz\Exception\RequestException;
+use Buzz\Message\Request as HttpRequest;
+use Buzz\Message\RequestInterface as HttpRequestInterface;
+use Buzz\Message\Response as HttpResponse;
+
+class RequestService
+{
+    public function makeRequest($url, $method = HttpRequestInterface::METHOD_GET, $data = array(), $headers = array(), $timeout = 60){
+        try {
+            $curl = new Curl();
+            $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();
+            $curl->setTimeout($timeout);
+            $curl->send($request, $response);
+            $response = $response->getContent();
+        } catch (RequestException $ex) {
+            throw new \Exception($ex->getMessage());
+        }
+
+        return json_decode($response, true);
+    }
+}