Skip to content

Commit

Permalink
FFWEB-3113: Support only API v5 and FF version NG
Browse files Browse the repository at this point in the history
Support only API v5 and FF version NG. Add test connection button for API key verification
  • Loading branch information
Rayn93 authored Jul 22, 2024
1 parent 55b10df commit ffb4c0e
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 53 deletions.
8 changes: 2 additions & 6 deletions spec/Storefront/Controller/ProxyControllerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PhpSpec\Wrapper\Collaborator;
use PHPUnit\Framework\Assert;
use Prophecy\Argument;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Expand All @@ -36,14 +35,11 @@ public function let(
$serverUrl = 'https://example.fact-finder.de/fact-finder';
$config->getServerUrl()->willReturn($serverUrl);
$config->getVersion()->willReturn('ng');
$config->getCredentials()->willReturn([
'username',
'pass',
]);
$config->getApiKey()->willReturn('api-key-123');
$this->beConstructedWith($config);
$clientBuilder->build()->willReturn($client);
$clientBuilder->withServerUrl(Argument::any())->willReturn($clientBuilder);
$clientBuilder->withCredentials(Argument::any())->willReturn($clientBuilder);
$clientBuilder->withApiKey(Argument::any())->willReturn($clientBuilder);
$clientBuilder->withVersion(Argument::any())->willReturn($clientBuilder);
$this->client = $client;
$this->clientBuilder = $clientBuilder;
Expand Down
34 changes: 27 additions & 7 deletions src/Api/TestConnectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,9 @@ public function __construct(
)]
public function testApiConnection(): JsonResponse
{
$client = $this->clientBuilder
->withCredentials(new Credentials(...$this->config->getCredentials()))
->withServerUrl($this->config->getServerUrl())
->withVersion($this->config->getVersion())
->build();

try {
$client = $this->clientBuilder
->withCredentials(new Credentials(...$this->config->getCredentials()))
->withApiKey($this->config->getApiKey())
->withServerUrl($this->config->getServerUrl())
->withVersion($this->config->getVersion())
->build();
Expand Down Expand Up @@ -79,6 +73,32 @@ public function testFTPConnection(): JsonResponse
}
}

#[Route(
'/api/_action/test-connection/push-import',
name: 'api.action.fact_finder.test_push_import_connection',
defaults: ['XmlHttpRequest' => true],
methods: ['GET']
)]
public function testPushImportApiConnection(): JsonResponse
{
try {
$client = $this->clientBuilder
->withCredentials(new Credentials(...$this->config->getCredentials()))
->withServerUrl($this->config->getServerUrl())
->withVersion($this->config->getVersion())
->build();

$endpoint = $this->createTestEndpoint();
$client->request('GET', $endpoint);

return new JsonResponse(['message' => 'Connection successfully established'], 200);
} catch (\Exception $e) {
$this->factfinderLogger->error($e->getMessage());

return new JsonResponse(['message' => 'Connection could not be established'], 400);
}
}

private function createTestEndpoint(): string
{
$apiVersion = $this->config->getApiVersion();
Expand Down
2 changes: 1 addition & 1 deletion src/Config/Communication.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getVersion(): string

public function getApiVersion(): string
{
return (string) $this->config('apiVersion') ?? 'v4';
return 'v5';
}

public function isProxyEnabled(): bool
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import template from './test-push-import-connection.html.twig';

const {Component, Mixin} = Shopware;

Component.register('test-push-import-connection', {
template,
mixins: [
Mixin.getByName('notification'),
Mixin.getByName('sw-inline-snippet'),
],
data() {
return {
isLoading: false,
isSaveSuccessful: false,
};
},

methods: {
async onClick() {
this.isLoading = true;
const httpClient = Shopware.Service('syncService').httpClient;
const url = '_action/test-connection/push-import';
const basicHeaders = {
Authorization: `Bearer ${Shopware.Context.api.authToken.access}`,
'Content-Type': 'application/json'
};

httpClient
.get(url, {
headers: basicHeaders
})
.then((response) => {
if (response.status === 200) {
this.createNotificationSuccess({
message: Shopware.Snippet.tc('configuration.testConnection.success')
});
} else {
this.createNotificationError({
title: Shopware.Snippet.tc('configuration.testConnection.fail'),
message: Shopware.Snippet.tc('configuration.testConnection.helpText')
});
}
})
.catch(() => {
this.createNotificationError({
title: Shopware.Snippet.tc('configuration.testConnection.fail'),
message: Shopware.Snippet.tc('configuration.testConnection.helpText')
});
})
.finally(() => {
this.isSaveSuccessful = true;
this.isLoading = false;
});
},
},
},);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<sw-button-process
class="test-connection-btn"
variant="primary"
:isLoading="isLoading"
:disabled="isLoading"
:processSuccess="isSaveSuccessful"
@click="onClick">
{{ $tc('configuration.testApiConnection.testConnection') }}
</sw-button-process>
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './component/update-field-roles';
import './component/test-api-connection';
import './component/test-ftp-connection';
import './component/test-push-import-connection';
51 changes: 19 additions & 32 deletions src/Resources/config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,13 @@
<required>true</required>
</input-field>

<input-field>
<name>username</name>
<label>Username</label>
<label lang="de-DE">Benutzername</label>
<required>true</required>
</input-field>

<input-field type="password">
<name>password</name>
<label>Password</label>
<label lang="de-DE">Passwort</label>
<required>true</required>
</input-field>

<input-field>
<name>apiKey</name>
<label>API key</label>
<label lang="de-DE">API-Schlüssel</label>
<required>true</required>
</input-field>

<input-field type="single-select">
<name>apiVersion</name>
<options>
<option>
<id>v4</id>
<name>v4</name>
</option>
<option>
<id>v5</id>
<name>v5</name>
</option>
</options>
<defaultValue>v4</defaultValue>
<label>API Version</label>
<label lang="de-DE">API-Version</label>
<required>true</required>
</input-field>

<input-field type="bool">
<name>useSsr</name>
<label>Use server-side rendering?</label>
Expand Down Expand Up @@ -223,6 +191,21 @@
<card>
<title>Import Settings</title>
<title lang="de-DE">Import-Konfiguration</title>

<input-field>
<name>username</name>
<label>Username</label>
<label lang="de-DE">Benutzername</label>
<required>true</required>
</input-field>

<input-field type="password">
<name>password</name>
<label>Password</label>
<label lang="de-DE">Passwort</label>
<required>true</required>
</input-field>

<input-field type="multi-select">
<name>pushImport</name>
<label>Enable Automatic Import for</label>
Expand All @@ -242,5 +225,9 @@
</option>
</options>
</input-field>

<component name="test-push-import-connection">
<name>testApiConnection</name>
</component>
</card>
</config>

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/Storefront/Controller/ProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Omikron\FactFinder\Shopware6\Storefront\Controller;

use Omikron\FactFinder\Communication\Client\ClientBuilder;
use Omikron\FactFinder\Communication\Credentials;
use Omikron\FactFinder\Shopware6\Config\Communication;
use Omikron\FactFinder\Shopware6\Events\BeforeProxyErrorResponseEvent;
use Omikron\FactFinder\Shopware6\Events\EnrichProxyDataEvent;
Expand Down Expand Up @@ -42,7 +41,7 @@ public function execute(
): Response {
$client = $clientBuilder
->withServerUrl($this->config->getServerUrl())
->withCredentials(new Credentials(...$this->config->getCredentials()))
->withApiKey($this->config->getApiKey())
->withVersion($this->config->getVersion())
->build();
$query = (string) parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_QUERY);
Expand Down
2 changes: 0 additions & 2 deletions src/Subscriber/ConfigurationSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ public function onPageLoaded(ShopwareSalesChannelEvent $event): void
'url' => $this->getServerUrl(),
'channel' => $this->config->getChannel($salesChannelId),
'apiKey' => $this->config->getApiKey(),
'version' => $this->config->getVersion(),
'api' => $this->config->getApiVersion(),
'currencyCode' => $event->getSalesChannelContext()->getCurrency()->getIsoCode(),
'currencyCountryCode' => $event->getRequest()->getLocale(),
];
Expand Down
3 changes: 1 addition & 2 deletions src/Utilites/Ssr/SearchAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Omikron\FactFinder\Communication\Client\ClientBuilder;
use Omikron\FactFinder\Communication\Client\ClientException;
use Omikron\FactFinder\Communication\Credentials;
use Omikron\FactFinder\Shopware6\Config\Communication;
use Psr\Http\Message\ResponseInterface;

Expand All @@ -33,7 +32,7 @@ public function search(
): array {
$client = $this->clientBuilder
->withServerUrl($this->config->getServerUrl())
->withCredentials(new Credentials(...$this->config->getCredentials()))
->withApiKey($this->config->getApiKey())
->withVersion($this->config->getVersion())
->build();

Expand Down

0 comments on commit ffb4c0e

Please sign in to comment.