Ver código fonte

default log to lumen logs; request Luticate backend

tags/0.1.2^0
Robin Thoni 9 anos atrás
pai
commit
100b0fb6e9
6 arquivos alterados com 291 adições e 2061 exclusões
  1. 3
    0
      composer.json
  2. 98
    2047
      composer.lock
  3. 2
    1
      src/Utils/LuLog.php
  4. 8
    13
      src/Utils/LuOutputFormatter.php
  5. 70
    0
      src/Utils/LuRequest.php
  6. 110
    0
      src/Utils/LuRequestDbo.php

+ 3
- 0
composer.json Ver arquivo

@@ -7,6 +7,9 @@
7 7
             "email": "robin@rthoni.com"
8 8
         }
9 9
     ],
10
+    "require": {
11
+    "guzzlehttp/guzzle": "^6.1"
12
+    },
10 13
     "autoload": {
11 14
       "psr-4": {
12 15
         "Luticate\\": "src/"

+ 98
- 2047
composer.lock
Diferenças do arquivo suprimidas por serem muito extensas
Ver arquivo


+ 2
- 1
src/Utils/LuLog.php Ver arquivo

@@ -1,11 +1,12 @@
1 1
 <?php
2 2
 
3 3
 namespace Luticate\Utils;
4
+use Log;
4 5
 
5 6
 class LuLog {
6 7
 
7 8
     public static function log($data)
8 9
     {
9
-
10
+        Log::info($data);
10 11
     }
11 12
 }

+ 8
- 13
src/Utils/LuOutputFormatter.php Ver arquivo

@@ -7,26 +7,21 @@ class LuOutputFormatter
7 7
 {
8 8
     public static $APP_VERSION = 1.0;
9 9
 
10
-    public static function getResponse()
11
-    {
12
-        return array(
13
-            "Version" => self::$APP_VERSION,
14
-            "Data" => null,
15
-            "Message" => null
16
-        );
17
-    }
18
-
19 10
     public static function formatSuccess($data)
20 11
     {
21
-        $r = self::getResponse();
22
-        $r["Data"] = $data;
12
+        $r = new LuRequestDbo();
13
+        $r->setVersion(self::$APP_VERSION);
14
+        $r->setData($data);
15
+        $r->setStatusCode(200);
23 16
         return $r;
24 17
     }
25 18
 
26 19
     public static function formatError($code, $message)
27 20
     {
28
-        $r = self::getResponse();
29
-        $r["Message"] = $message;
21
+        $r = new LuRequestDbo();
22
+        $r->setVersion(self::$APP_VERSION);
23
+        $r->setMessage($message);
24
+        $r->setStatusCode($code);
30 25
         return $r;
31 26
     }
32 27
 }

+ 70
- 0
src/Utils/LuRequest.php Ver arquivo

@@ -0,0 +1,70 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 11/23/15
6
+ * Time: 10:56 PM
7
+ */
8
+
9
+namespace Luticate\Utils;
10
+
11
+
12
+use GuzzleHttp\Client;
13
+use GuzzleHttp\Exception\ClientException;
14
+use GuzzleHttp\Exception\RequestException;
15
+
16
+class LuRequest
17
+{
18
+    /**
19
+     * @param $method
20
+     * @param $url
21
+     * @param array $get
22
+     * @param array $post
23
+     * @param array $headers
24
+     * @return LuRequestDbo
25
+     */
26
+    public static function request($method, $url, $get = [], $post = null, $headers = [])
27
+    {
28
+        $client = new Client();
29
+        $options = [
30
+            "headers" => $headers,
31
+            "form_params" => $post
32
+        ];
33
+        foreach ($get as $key => $value) {
34
+            $url .= (strpos($url, '?') === false ? "?" : "&") . urlencode($key) . "=" . urlencode($value);
35
+        }
36
+        $response = null;
37
+        try {
38
+            $response = $client->request($method, $url, $options);
39
+        } catch (ClientException $e) {
40
+            $response = $e->getResponse();
41
+            LuLog::log($response->getBody());
42
+            LuLog::log($e);
43
+        } catch (RequestException $e) {
44
+            $response = $e->getResponse();
45
+            LuLog::log($response->getBody());
46
+            LuLog::log($e);
47
+        } catch (\Exception $e) {
48
+            LuLog::log($e);
49
+        }
50
+        if (!is_null($response)) {
51
+            $body = json_decode($response->getBody(), true);
52
+            $request = LuRequestDbo::jsonDeserialize($body);
53
+            $request->setStatusCode($response->getStatusCode());
54
+            return $request;
55
+        }
56
+        $request = new LuRequestDbo();
57
+        $request->setMessage("Failed to parse response");
58
+        $request->setStatusCode(500);
59
+        return $request;
60
+    }
61
+
62
+    public static function proxy($method, $url, $get = [], $post = null, $headers = [])
63
+    {
64
+        $response = LuRequest::request($method, $url, $get, $post, $headers);
65
+        if ($response->getStatusCode() != 200) {
66
+            abort($response->getStatusCode(), $response->getMessage());
67
+        }
68
+        return $response->getData();
69
+    }
70
+}

+ 110
- 0
src/Utils/LuRequestDbo.php Ver arquivo

@@ -0,0 +1,110 @@
1
+<?php
2
+/**
3
+ * Created by PhpStorm.
4
+ * User: robin
5
+ * Date: 11/23/15
6
+ * Time: 10:47 PM
7
+ */
8
+
9
+namespace Luticate\Utils;
10
+
11
+
12
+class LuRequestDbo extends LuDbo
13
+{
14
+    /**
15
+     * Specify data which should be serialized to JSON
16
+     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
17
+     * @return mixed data which can be serialized by <b>json_encode</b>,
18
+     * which is a value of any type other than a resource.
19
+     * @since 5.4.0
20
+     */
21
+    function jsonSerialize()
22
+    {
23
+        return array(
24
+            "Version" => $this->_version,
25
+            "Data" => $this->_data,
26
+            "Message" => $this->_message
27
+        );
28
+    }
29
+
30
+    public static function jsonDeserialize($json)
31
+    {
32
+        $dbo = new LuRequestDbo();
33
+        if (isset($json["Version"])) {
34
+            $dbo->setVersion($json["Version"]);
35
+        }
36
+        if (isset($json["Data"])) {
37
+            $dbo->setData($json["Data"]);
38
+        }
39
+        if (isset($json["Message"])) {
40
+            $dbo->setMessage($json["Message"]);
41
+        }
42
+        return $dbo;
43
+    }
44
+
45
+    private $_version;
46
+    /**
47
+     * @return mixed
48
+     */
49
+    public function getVersion()
50
+    {
51
+        return $this->_version;
52
+    }
53
+    /**
54
+     * @param mixed $version
55
+     */
56
+    public function setVersion($version)
57
+    {
58
+        $this->_version = $version;
59
+    }
60
+
61
+    private $_data;
62
+    /**
63
+     * @return mixed
64
+     */
65
+    public function getData()
66
+    {
67
+        return $this->_data;
68
+    }
69
+    /**
70
+     * @param mixed $data
71
+     */
72
+    public function setData($data)
73
+    {
74
+        $this->_data = $data;
75
+    }
76
+
77
+    private $_message;
78
+    /**
79
+     * @return mixed
80
+     */
81
+    public function getMessage()
82
+    {
83
+        return $this->_message;
84
+    }
85
+    /**
86
+     * @param mixed $message
87
+     */
88
+    public function setMessage($message)
89
+    {
90
+        $this->_message = $message;
91
+    }
92
+
93
+    private $_statusCode;
94
+
95
+    /**
96
+     * @return mixed
97
+     */
98
+    public function getStatusCode()
99
+    {
100
+        return $this->_statusCode;
101
+    }
102
+
103
+    /**
104
+     * @param mixed $statusCode
105
+     */
106
+    public function setStatusCode($statusCode)
107
+    {
108
+        $this->_statusCode = $statusCode;
109
+    }
110
+}

Carregando…
Cancelar
Salvar