Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to fake Vuforia API requests #1

Open
robgwin opened this issue Nov 20, 2020 · 4 comments
Open

Ability to fake Vuforia API requests #1

robgwin opened this issue Nov 20, 2020 · 4 comments

Comments

@robgwin
Copy link

robgwin commented Nov 20, 2020

Hi @teodos,

I added an option to my local copy of this library that lets me skip the Vuforia API calls and instead log the request data and return a fake success response. This makes it easier to work on sparkd features without throwing tons of junk at vuforia 😄 Here is the diff of my code in case you'd like to add it for future use. Let me know if you'd like me to push it to a feature branch (I would need privs to do so). -rob

diff --git a/src/Request/AbstractRequest.php b/src/Request/AbstractRequest.php
index 0136503..caffd81 100644
--- a/src/Request/AbstractRequest.php
+++ b/src/Request/AbstractRequest.php
@@ -7,6 +7,7 @@ use DateTimeZone;
 use Exception;
 use HTTP_Request2;
 use HTTP_Request2_Exception;
+use Illuminate\Support\Facades\Log;
 
 abstract class AbstractRequest
 {
@@ -25,6 +26,7 @@ abstract class AbstractRequest
         $this->naming_rule = $config['naming_rule'];
         $this->max_meta_size = $config['max_meta_size'];
         $this->max_image_size = $config['max_image_size'];
+        $this->fake_success = $config['fake_success'];
     }
 
     protected function request(
@@ -35,6 +37,22 @@ abstract class AbstractRequest
     )
     {
 
+        if ($this->fake_success) {
+            Log::debug('Vuforia Request: '.$method.' '.$uri);
+            if (!empty($body)){
+                $body = json_decode($body);
+                if (!empty($body->image))
+                    $body->image = "[".strlen($body->image)." chars of image data]";
+                $body = json_encode($body);
+                Log::debug($body);
+            }
+            return [
+                'status' => 200,
+                'message' => 'faking success',
+                'data' => [],
+            ];
+        }
+
         if (empty($this->access_key) || empty($this->secret_key)) {
             throw new Exception('Missing Vuforia Access/Secret Key(s)');
         }
diff --git a/src/VuforiaClient.php b/src/VuforiaClient.php
index c1f0d7e..7ab16d1 100644
--- a/src/VuforiaClient.php
+++ b/src/VuforiaClient.php
@@ -59,6 +59,15 @@ class VuforiaClient
         |
         */
         'api_base' => 'https://vws.vuforia.com/',
+
+        /*
+        |--------------------------------------------------------------
+        | Don't make requests, just log them and fake the response
+        |--------------------------------------------------------------
+        |
+        |
+        */
+        'fake_success' => false,
     ];
 
     /**

@teodos
Copy link
Contributor

teodos commented Nov 23, 2020

hey @robgwin, can you get rid of Illuminate\Support\Facades\Log; it's an extra dependency in this case
Thanks

@robgwin
Copy link
Author

robgwin commented Nov 23, 2020

The Log class is needed for the debug output. I can make it degrade gracefully and then we add only to require-dev.

diff --git a/src/Request/AbstractRequest.php b/src/Request/AbstractRequest.php
index 0136503..0491512 100644
--- a/src/Request/AbstractRequest.php
+++ b/src/Request/AbstractRequest.php
@@ -7,6 +7,7 @@ use DateTimeZone;
 use Exception;
 use HTTP_Request2;
 use HTTP_Request2_Exception;
+use Illuminate\Support\Facades\Log;
 
 abstract class AbstractRequest
 {
@@ -16,6 +17,7 @@ abstract class AbstractRequest
     protected $naming_rule;
     protected $max_meta_size;
     protected $max_image_size;
+    protected $fake_success;
 
     public function __construct(array $config)
     {
@@ -25,6 +27,7 @@ abstract class AbstractRequest
         $this->naming_rule = $config['naming_rule'];
         $this->max_meta_size = $config['max_meta_size'];
         $this->max_image_size = $config['max_image_size'];
+        $this->fake_success = $config['fake_success'];
     }
 
     protected function request(
@@ -35,6 +38,16 @@ abstract class AbstractRequest
     )
     {
 
+        if ($this->fake_success) {
+            $this->logRequest($method, $uri, $body, $headers);
+
+            return [
+                'status' => 200,
+                'message' => 'faking success',
+                'data' => [],
+            ];
+        }
+
         if (empty($this->access_key) || empty($this->secret_key)) {
             throw new Exception('Missing Vuforia Access/Secret Key(s)');
         }
@@ -89,6 +102,28 @@ abstract class AbstractRequest
         }
     }
 
+    protected function logRequest(
+        $method,
+        $uri,
+        $body = null,
+        $headers = null
+    )
+    {
+        if (!class_exists('Log')) return false;
+
+        if (!empty($body)){
+            $body = json_decode($body);
+            if (!empty($body->image))
+                $body->image = "[".strlen($body->image)." chars of image data]";
+            if (!empty($body->application_metadata))
+                $body->application_metadata = base64_decode($body->application_metadata);
+            $body = json_encode($body);
+        }
+
+        Log::debug('Vuforia Request: '.$method.' '.$uri);
+        Log::debug($body);
+    }
+
     private function sign(HTTP_Request2 $request)
     {
         $method = $request->getMethod();
diff --git a/src/VuforiaClient.php b/src/VuforiaClient.php
index c1f0d7e..7ab16d1 100644
--- a/src/VuforiaClient.php
+++ b/src/VuforiaClient.php
@@ -59,6 +59,15 @@ class VuforiaClient
         |
         */
         'api_base' => 'https://vws.vuforia.com/',
+
+        /*
+        |--------------------------------------------------------------
+        | Don't make requests, just log them and fake the response
+        |--------------------------------------------------------------
+        |
+        |
+        */
+        'fake_success' => false,
     ];
 
     /**

@robgwin
Copy link
Author

robgwin commented Nov 25, 2020

I made a fork, so I can just keep using that for local dev if you don't want to add:
robgwin@86b61b7

I also split the logging to a separate 'log_requests' option so I can troubleshoot real vuforia requests as well.

@bakome
Copy link

bakome commented Nov 26, 2020

@robgwin Thank you for contributing to this package.

The idea is good, but there is a couple of things we need to consider before merging this changes. This library is open source and can be used by everyone. Here are couple of notes that you need to consider.

AbstractRequest is the core class of this library and We like to keep it very clean at this stage.

Please don't use logger here and find some other implementation outside this either with Decorator or Proxy class. They can also exists in root tests folder.

In tests folder you can create FakeAbstractClass and use that for mocking or testing purposes. Even better you can wrap HTTP_Request2 class with domain interface or class and use some HTTP request mocker later for testing.

For logging purpose if is inevitable better use Monolog then Laravel Facades. 

Keep in mind that this can be used also in automated tests and should not affect core files at all.

TIP: For logging you can create Console Writer instead of File logs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants