Browse code

Add debug output for shop api tester command

Benjamin Roth authored on13/11/2024 16:05:01
Showing1 changed files
... ...
@@ -78,6 +78,7 @@ class ShopwareApiTester extends Command
78 78
         }
79 79
 
80 80
         $io->error(sprintf('Error when querying the Shopware API. HTTP Status returned: %s',$Result->getStatusCode()));
81
+        $dumper->dump(($cloner->cloneVar(json_validate($Result->getContent(false)) ? json_decode($Result->getContent(false), true) : $Result)));
81 82
 
82 83
         return self::FAILURE;
83 84
     }
Browse code

Optimize API functions and CLI commands

Benjamin Roth authored on21/10/2024 15:41:50
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,84 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+ * This file is part of vonRotenberg Shopware API Bundle.
7
+ *
8
+ * (c) vonRotenberg
9
+ *
10
+ * @license proprietary
11
+ */
12
+
13
+namespace vonRotenberg\ShopwareApiBundle\Command;
14
+
15
+use Contao\CoreBundle\Framework\ContaoFramework;
16
+use Contao\System;
17
+use Symfony\Component\Console\Command\Command;
18
+use Symfony\Component\Console\Input\InputArgument;
19
+use Symfony\Component\Console\Input\InputInterface;
20
+use Symfony\Component\Console\Input\InputOption;
21
+use Symfony\Component\Console\Output\OutputInterface;
22
+use Symfony\Component\Console\Style\SymfonyStyle;
23
+use Symfony\Component\VarDumper\Cloner\VarCloner;
24
+use Symfony\Component\VarDumper\Dumper\CliDumper;
25
+use vonRotenberg\ShopwareApiBundle\API\Shopware;
26
+
27
+class ShopwareApiTester extends Command
28
+{
29
+    protected static $defaultName = 'shopware:api-test';
30
+
31
+    protected $framework;
32
+
33
+    public function __construct(ContaoFramework $framework)
34
+    {
35
+        $this->framework = $framework;
36
+
37
+        parent::__construct();
38
+    }
39
+
40
+    protected function configure(): void
41
+    {
42
+        $this
43
+            ->setName(self::$defaultName)
44
+            ->setDescription('Query the Shopware API')
45
+            ->addOption('authenticate','a',InputOption::VALUE_NONE, 'Use Token authentication with request')
46
+        ;
47
+    }
48
+    protected function execute(InputInterface $input, OutputInterface $output): int
49
+    {
50
+        $this->framework->initialize();
51
+        $cloner = new VarCloner();
52
+        $dumper = new CliDumper();
53
+
54
+        $io = new SymfonyStyle($input, $output);
55
+        $io->title('Shopware API Tester');
56
+        $strUrlFragment = $io->ask('Enter URL fragment');
57
+        $strMethod = $io->choice('Request type',['GET','POST','PUT','DELETE','PATCH'], 'GET');
58
+        $strBody = $io->ask('Enter request body (JSON)', null, function ($strBody) {
59
+            if ($strBody !== null && !json_validate($strBody)) {
60
+                throw new \RuntimeException('Invalid JSON body');
61
+            }
62
+
63
+            return $strBody;
64
+        });
65
+
66
+        /** @var Shopware $Shopware */
67
+        $Shopware = System::getContainer()->get(Shopware::class);
68
+
69
+        $Result = $Shopware->queryAPI($strUrlFragment,$strBody, $strMethod, (bool)$input->getOption('authenticate'));
70
+
71
+        if ($Result->getStatusCode() === 200)
72
+        {
73
+            $dumper->dump(($cloner->cloneVar(json_validate($Result->getContent()) ? json_decode($Result->getContent(), true) : $Result)));
74
+
75
+            $io->success('Successful response');
76
+
77
+            return self::SUCCESS;
78
+        }
79
+
80
+        $io->error(sprintf('Error when querying the Shopware API. HTTP Status returned: %s',$Result->getStatusCode()));
81
+
82
+        return self::FAILURE;
83
+    }
84
+}