Browse Source

feat: clearer webapi return value

data field should only contain array of data, and error msg should only be put in msg field.
M1Screw 2 years ago
parent
commit
5f57aa2c9c

+ 1 - 1
src/Controllers/WebAPI/FuncController.php

@@ -17,7 +17,7 @@ final class FuncController extends BaseController
     {
         return $response->withJson([
             'ret' => 1,
-            'data' => 'Pong? Pong!',
+            'msg' => 'Pong? Pong!',
         ]);
     }
 

+ 1 - 1
src/Controllers/WebAPI/NodeController.php

@@ -23,7 +23,7 @@ final class NodeController extends BaseController
         if ($node === null) {
             return $response->withJson([
                 'ret' => 0,
-                'data' => 'Node not found.',
+                'msg' => 'Node not found.',
             ]);
         }
 

+ 13 - 13
src/Controllers/WebAPI/UserController.php

@@ -38,16 +38,16 @@ final class UserController extends BaseController
         if ($node === null) {
             return $response->withJson([
                 'ret' => 0,
-                'data' => 'Node not found.',
+                'msg' => 'Node not found.',
             ]);
         }
 
         $node->update(['node_heartbeat' => time()]);
 
-        if (($node->node_bandwidth_limit !== 0) && $node->node_bandwidth_limit < $node->node_bandwidth) {
+        if ($node->node_bandwidth_limit !== 0 && $node->node_bandwidth_limit <= $node->node_bandwidth) {
             return $response->withJson([
-                'ret' => 1,
-                'data' => [],
+                'ret' => 0,
+                'msg' => 'Node out of bandwidth.',
             ]);
         }
 
@@ -145,7 +145,7 @@ final class UserController extends BaseController
         if (! $data || ! is_array($data->data)) {
             return $response->withJson([
                 'ret' => 0,
-                'data' => 'Invalid data.',
+                'msg' => 'Invalid data.',
             ]);
         }
 
@@ -156,7 +156,7 @@ final class UserController extends BaseController
         if ($node === null) {
             return $response->withJson([
                 'ret' => 0,
-                'data' => 'Node not found.',
+                'msg' => 'Node not found.',
             ]);
         }
 
@@ -202,7 +202,7 @@ final class UserController extends BaseController
 
         return $response->withJson([
             'ret' => 1,
-            'data' => 'ok',
+            'msg' => 'ok',
         ]);
     }
 
@@ -222,7 +222,7 @@ final class UserController extends BaseController
         if (! $data || ! is_array($data->data)) {
             return $response->withJson([
                 'ret' => 0,
-                'data' => 'Invalid data.',
+                'msg' => 'Invalid data.',
             ]);
         }
 
@@ -232,7 +232,7 @@ final class UserController extends BaseController
         if ($node_id === null || ! Node::where('id', $node_id)->exists()) {
             return $response->withJson([
                 'ret' => 0,
-                'data' => 'Node not found.',
+                'msg' => 'Node not found.',
             ]);
         }
 
@@ -259,7 +259,7 @@ final class UserController extends BaseController
 
         return $response->withJson([
             'ret' => 1,
-            'data' => 'ok',
+            'msg' => 'ok',
         ]);
     }
 
@@ -279,7 +279,7 @@ final class UserController extends BaseController
         if (! $data || ! is_array($data->data)) {
             return $response->withJson([
                 'ret' => 0,
-                'data' => 'Invalid data.',
+                'msg' => 'Invalid data.',
             ]);
         }
 
@@ -289,7 +289,7 @@ final class UserController extends BaseController
         if ($node_id === null || ! Node::where('id', $node_id)->exists()) {
             return $response->withJson([
                 'ret' => 0,
-                'data' => 'Node not found.',
+                'msg' => 'Node not found.',
             ]);
         }
 
@@ -307,7 +307,7 @@ final class UserController extends BaseController
 
         return $response->withJson([
             'ret' => 1,
-            'data' => 'ok',
+            'msg' => 'ok',
         ]);
     }
 }

+ 4 - 4
src/Middleware/NodeToken.php

@@ -26,7 +26,7 @@ final class NodeToken implements MiddlewareInterface
         if ($key === null) {
             return AppFactory::determineResponseFactory()->createResponse(401)->withJson([
                 'ret' => 0,
-                'data' => 'Invalid request.',
+                'msg' => 'Invalid request.',
             ]);
         }
 
@@ -38,7 +38,7 @@ final class NodeToken implements MiddlewareInterface
         ) {
             return AppFactory::determineResponseFactory()->createResponse(401)->withJson([
                 'ret' => 0,
-                'data' => 'Invalid request.',
+                'msg' => 'Invalid request.',
             ]);
         }
 
@@ -48,7 +48,7 @@ final class NodeToken implements MiddlewareInterface
         ) {
             return AppFactory::determineResponseFactory()->createResponse(401)->withJson([
                 'ret' => 0,
-                'data' => 'Invalid request.',
+                'msg' => 'Invalid request.',
             ]);
         }
 
@@ -58,7 +58,7 @@ final class NodeToken implements MiddlewareInterface
             if ($ip !== '127.0.0.1' && ! Node::where('node_ip', $ip)->exists()) {
                 return AppFactory::determineResponseFactory()->createResponse(401)->withJson([
                     'ret' => 0,
-                    'data' => 'Invalid request IP.',
+                    'msg' => 'Invalid request IP.',
                 ]);
             }
         }

+ 26 - 26
src/Utils/ResponseHelper.php

@@ -12,7 +12,7 @@ use function json_encode;
 
 final class ResponseHelper
 {
-    public static function success(Response $response, string $msg): ResponseInterface
+    public static function success(Response $response, string $msg = ''): ResponseInterface
     {
         return $response->withJson([
             'ret' => 1,
@@ -27,7 +27,7 @@ final class ResponseHelper
      *
      * @return ResponseInterface
      */
-    public static function successWithData(Response $response, string $msg, array $data): ResponseInterface
+    public static function successWithData(Response $response, string $msg = '', array $data = []): ResponseInterface
     {
         return $response->withJson([
             'ret' => 1,
@@ -36,30 +36,6 @@ final class ResponseHelper
         ]);
     }
 
-    public static function error(Response $response, string $msg): ResponseInterface
-    {
-        return $response->withJson([
-            'ret' => 0,
-            'msg' => $msg,
-        ]);
-    }
-
-    /**
-     * @param Response $response
-     * @param string $msg
-     * @param array $data
-     *
-     * @return ResponseInterface
-     */
-    public static function errorWithData(Response $response, string $msg, array $data): ResponseInterface
-    {
-        return $response->withJson([
-            'ret' => 0,
-            'msg' => $msg,
-            'data' => $data,
-        ]);
-    }
-
     /**
      * Build a JSON response with ETag header.
      *
@@ -84,4 +60,28 @@ final class ResponseHelper
 
         return $response->withHeader('ETag', $etag)->withJson($data);
     }
+
+    public static function error(Response $response, string $msg = ''): ResponseInterface
+    {
+        return $response->withJson([
+            'ret' => 0,
+            'msg' => $msg,
+        ]);
+    }
+
+    /**
+     * @param Response $response
+     * @param string $msg
+     * @param array $data
+     *
+     * @return ResponseInterface
+     */
+    public static function errorWithData(Response $response, string $msg = '', array $data = []): ResponseInterface
+    {
+        return $response->withJson([
+            'ret' => 0,
+            'msg' => $msg,
+            'data' => $data,
+        ]);
+    }
 }