Skip to content

Commit b2aebe9

Browse files
authored
Merge pull request #452 from cultuurnet/WID-589-widgets-clientIds
WID-589 Widgets with clientIds
2 parents 297b06c + b6dd220 commit b2aebe9

File tree

11 files changed

+127
-11
lines changed

11 files changed

+127
-11
lines changed

app/ApplicationBase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ function ($translator, $app) {
176176
$this->register(
177177
new SearchAPIServiceProvider(),
178178
[
179+
'search_api.use_client_ids' => $this['config']['search_api']['use_client_ids'] ?? false,
179180
'search_api.base_url' => $this['config']['search_api']['live']['base_url'],
180181
'search_api.api_key' => $this['config']['search_api']['live']['api_key'],
181182
'search_api_test.base_url' => $this['config']['search_api']['test']['base_url'],

app/SearchAPI/SearchAPIServiceProvider.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@ public function register(Container $pimple)
1515

1616
$pimple['search_api'] = function (Container $pimple) {
1717

18+
$headers = [];
19+
if ($pimple['search_api.use_client_ids']) {
20+
$headers['X-Client-Id'] = $pimple['search_api.client_id'];
21+
} else {
22+
$headers['X-Api-Key'] = $pimple['search_api.api_key'];
23+
}
24+
$headers['X-Client-Properties'] = 'cluster|widgets, cluster|snowplow';
25+
1826
$guzzleClient = new Client(
1927
[
2028
'base_uri' => $pimple['search_api.base_url'],
21-
'headers' => [
22-
'X-Api-Key' => $pimple['search_api.api_key'],
23-
'X-Client-Properties' => 'cluster|widgets, cluster|snowplow',
24-
],
29+
'headers' => $headers,
2530
'handler' => $this->getHandlerStack('search_api', $pimple),
2631
]
2732
);
@@ -36,7 +41,11 @@ public function register(Container $pimple)
3641
$config = $searchClient->getClient()->getConfig();
3742
$config['base_uri'] = $pimple['search_api_test.base_url'];
3843
$headers = $config['headers'] ?? [];
39-
$headers['X-Api-Key'] = $pimple['search_api_test.api_key'];
44+
if ($pimple['search_api.use_client_ids']) {
45+
$headers['X-Client-Id'] = $pimple['search_api_test.client_id'];
46+
} else {
47+
$headers['X-Api-Key'] = $pimple['search_api_test.api_key'];
48+
}
4049
$config['headers'] = $headers;
4150

4251
$searchClient->setClient(new \GuzzleHttp\Client($config));

app/Widget/WidgetServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function register(Container $pimple)
6666

6767
/** @var RequestContext $requestContext */
6868
$requestContext = $pimple['request_context'];
69-
$renderer = new Renderer($pimple['widget_layout_manager'], $pimple['google_tag_manager'], $pimple['project_repository'], $pimple['search_api'], $pimple['search_api_test'], $pimple['curatoren_api'], $pimple['curatoren_api_test']);
69+
$renderer = new Renderer($pimple['widget_layout_manager'], $pimple['google_tag_manager'], $pimple['project_repository'], $pimple['search_api'], $pimple['search_api_test'], $pimple['curatoren_api'], $pimple['curatoren_api_test'], $pimple['search_api.use_client_ids'] ?? false);
7070
$renderer->addSettings(['apiUrl' => $requestContext->getScheme() . '://' . $requestContext->getHost() . $requestContext->getBaseUrl() . '/widgets/api']);
7171

7272
return $renderer;

src/Entity/Project.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,20 @@ class Project implements ProjectInterface
153153
*/
154154
protected $testApiKeySapi3;
155155

156+
/**
157+
* @ORM\Column(name="live_client_id", type="string", length=255, nullable=true)
158+
* @var ?string
159+
* @Type("string")
160+
*/
161+
protected $liveClientId;
162+
163+
/**
164+
* @ORM\Column(name="test_client_id", type="string", length=255, nullable=true)
165+
* @var ?string
166+
* @Type("string")
167+
*/
168+
protected $testClientId;
169+
156170
/**
157171
* @ORM\Column(name="description", type="string", length=255, nullable=true)
158172
* @var string
@@ -312,6 +326,42 @@ public function setTestApiKeySapi3(string $testApiKeySapi3): Project
312326
return $this;
313327
}
314328

329+
/**
330+
* @return string
331+
*/
332+
public function getLiveClientId()
333+
{
334+
return $this->liveClientId;
335+
}
336+
337+
/**
338+
* @param string $liveClientId
339+
* @return Project
340+
*/
341+
public function setLiveClientId(string $liveClientId)
342+
{
343+
$this->liveClientId = $liveClientId;
344+
return $this;
345+
}
346+
347+
/**
348+
* @return string
349+
*/
350+
public function getTestClientId()
351+
{
352+
return $this->testClientId;
353+
}
354+
355+
/**
356+
* @param string $testApiKeySapi3
357+
* @return Project
358+
*/
359+
public function setTestClientId(string $testClientId): Project
360+
{
361+
$this->testClientId = $testClientId;
362+
return $this;
363+
}
364+
315365
/**
316366
* @return int
317367
*/

src/Entity/ProjectInterface.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,18 @@ public function setLiveApiKeySapi3(string $liveApiKeySapi3);
6262
public function getTestApiKeySapi3();
6363

6464
/**
65-
* Set the test search api 3 key.
66-
*
67-
* @param string $liveApiKeySapi3
65+
* @param string $testApiKeySapi3
6866
*/
6967
public function setTestApiKeySapi3(string $testApiKeySapi3);
7068

69+
public function getLiveClientId();
70+
71+
public function setLiveClientId(string $liveClientId);
72+
73+
public function getTestClientId();
74+
75+
public function setTestClientId(string $testClientId);
76+
7177
/**
7278
* @return int
7379
*/

src/Project/Command/ImportProject.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ class ImportProject
3939
*/
4040
private $liveApiKeySapi3;
4141

42+
/**
43+
* @var string
44+
*/
45+
private $testClientId;
46+
47+
/**
48+
* @var string
49+
*/
50+
private $liveClientId;
51+
4252
/**
4353
* @var string
4454
*/
@@ -52,6 +62,8 @@ public function __construct(
5262
int $groupId,
5363
string $testApiKeySapi3,
5464
string $liveApiKeySapi3,
65+
string $testClientId,
66+
string $liveClientId,
5567
string $state
5668
) {
5769
$this->platformUuid = $platformUuid;
@@ -61,6 +73,8 @@ public function __construct(
6173
$this->groupId = $groupId;
6274
$this->testApiKeySapi3 = $testApiKeySapi3;
6375
$this->liveApiKeySapi3 = $liveApiKeySapi3;
76+
$this->testClientId = $testClientId;
77+
$this->liveClientId = $liveClientId;
6478
$this->state = $state;
6579
}
6680

@@ -99,6 +113,16 @@ public function getLiveApiKeySapi3(): string
99113
return $this->liveApiKeySapi3;
100114
}
101115

116+
public function getTestClientId(): string
117+
{
118+
return $this->testClientId;
119+
}
120+
121+
public function getLiveClientId(): string
122+
{
123+
return $this->liveClientId;
124+
}
125+
102126
public function getState(): string
103127
{
104128
return $this->state;

src/Project/CommandHandler/ImportProjectCommandHandler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public function handle(ImportProject $importProject): void
3939
$project->setPlatformUuid($importProject->getPlatformUuid());
4040
$project->setTestApiKeySapi3($importProject->getTestApiKeySapi3());
4141
$project->setLiveApiKeySapi3($importProject->getLiveApiKeySapi3());
42+
$project->setTestClientId($importProject->getTestClientId());
43+
$project->setLiveClientId($importProject->getLiveClientId());
4244
}
4345
$project->setName($importProject->getName());
4446
$project->setDescription($importProject->getDescription());

src/Project/Controller/ImportProjectController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public function importProject(string $uuid, Request $request): JsonResponse
3939
$postedProject->groupId,
4040
$postedProject->testApiKeySapi3,
4141
$postedProject->liveApiKeySapi3,
42+
$postedProject->testClientId,
43+
$postedProject->liveClientId,
4244
$postedProject->state
4345
)
4446
);

src/Widget/Renderer.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ class Renderer implements RendererInterface
6161
*/
6262
protected $curatorenClientTest;
6363

64+
/**
65+
* @var bool
66+
*/
67+
protected $useClientIds;
68+
6469
/**
6570
* @var array
6671
*/
@@ -82,7 +87,7 @@ class Renderer implements RendererInterface
8287
* @param $googleTagManagerId
8388
* @param ProjectServiceInterface $projectService
8489
*/
85-
public function __construct(WidgetPluginManager $widgetPluginManager, $googleTagManagerId, EntityRepository $projectRepository, SearchClientInterface $searchClient, SearchClientInterface $searchClientTest, CuratorenClient $curatorenClient, CuratorenClient $curatorenClientTest)
90+
public function __construct(WidgetPluginManager $widgetPluginManager, $googleTagManagerId, EntityRepository $projectRepository, SearchClientInterface $searchClient, SearchClientInterface $searchClientTest, CuratorenClient $curatorenClient, CuratorenClient $curatorenClientTest, bool $useClientIds)
8691
{
8792
$this->widgetPluginManager = $widgetPluginManager;
8893
$this->googleTagManagerId = $googleTagManagerId;
@@ -91,6 +96,7 @@ public function __construct(WidgetPluginManager $widgetPluginManager, $googleTag
9196
$this->searchClientTest = $searchClientTest;
9297
$this->curatorenClient = $curatorenClient;
9398
$this->curatorenClientTest = $curatorenClientTest;
99+
$this->useClientIds = $useClientIds;
94100
}
95101

96102
public function addSettings(array $settings)
@@ -105,16 +111,22 @@ public function setProject(ProjectInterface $project)
105111
// If a project is not live yet. We should use the test api + test key.
106112
if ($project->getStatus() !== ProjectInterface::PROJECT_STATUS_ACTIVE) {
107113
$apiKey = $project->getTestApiKeySapi3();
114+
$clientId = $project->getTestClientId();
108115
$config = $this->searchClientTest->getClient()->getConfig();
109116
$curatorenConfig = $this->curatorenClientTest->getClient()->getConfig();
110117
} else {
111118
$config = $this->searchClient->getClient()->getConfig();
112119
$apiKey = $project->getLiveApiKeySapi3();
120+
$clientId = $project->getLiveClientId();
113121
$curatorenConfig = $this->curatorenClient->getClient()->getConfig();
114122
}
115123

116124
$headers = $config['headers'] ?? [];
117-
$headers['X-Api-Key'] = $apiKey;
125+
if ($this->useClientIds) {
126+
$headers['X-Client-Id'] = $clientId;
127+
} else {
128+
$headers['X-Api-Key'] = $apiKey;
129+
}
118130
$config['headers'] = $headers;
119131

120132
$this->searchClient->setClient(new \GuzzleHttp\Client($config));

test/Project/CommandHandler/ImportProjectCommandHandlerTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public function testHandleNewImport(): void
6868
24378,
6969
'SAPI3 test key',
7070
'SAPI3 live key',
71+
'Test client id',
72+
'Live client id',
7173
'active'
7274
);
7375

@@ -79,6 +81,8 @@ public function testHandleNewImport(): void
7981
$project->setPlatformUuid($importProject->getPlatformUuid());
8082
$project->setTestApiKeySapi3($importProject->getTestApiKeySapi3());
8183
$project->setLiveApiKeySapi3($importProject->getLiveApiKeySapi3());
84+
$project->setTestClientId($importProject->getTestClientId());
85+
$project->setLiveClientId($importProject->getLiveClientId());
8286
$project->setStatus(Project::PROJECT_STATUS_ACTIVE);
8387

8488
$this->logger->expects($this->exactly(2))
@@ -101,6 +105,8 @@ public function testHandleUpdateImport(): void
101105
24378,
102106
'SAPI3 test key',
103107
'SAPI3 live key',
108+
'Test client id',
109+
'Live client id',
104110
'active'
105111
);
106112

0 commit comments

Comments
 (0)