Skip to content

Commit

Permalink
Fixed store action and added support for 201 responses
Browse files Browse the repository at this point in the history
  • Loading branch information
gauquier committed Nov 12, 2024
1 parent 1aae72e commit 1e065a8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
12 changes: 10 additions & 2 deletions src/Component/Action/StoreAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@ public function init(): void
if ([] !== $falseAction) {
$this->falseActionProcessor = $this->configuration->generateActionProcessor($falseAction);
}
$this->setOption('init', true);
$this->setOption('init', true);
}
}

public function apply(array $item): array
{
$this->init();
$entity = $this->getOption('entity');
$identifier = $entity.':'.$item['identifier']; // $this->getOption('identifier');
$identifierKey = $this->getOption('identifier');

$identifier = $entity.':'.$item[$identifierKey];
$event = $this->getOption('event');
if (empty($identifier) || empty($event)) {
return $item;
Expand Down Expand Up @@ -109,7 +111,13 @@ public function apply(array $item): array
if ([] !== $falseAction) {
$item = $this->falseActionProcessor->process($item);
}

$this->storeProduct($identifier);

return $item;
}

return [];
}

return $item;
Expand Down
2 changes: 1 addition & 1 deletion src/Component/Akeneo/Client/ApiWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private function doWrite(array $data)
$response = $this->execute($data);
}

if (!in_array($response->getCode(), [200, 204])) {
if (!in_array($response->getCode(), [200, 201, 204])) {
throw new InvalidItemException('API exception', ['message' => $response->getContent()], $data);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Component/Common/Client/BasicAuthApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public function sendRequest($method, $endpoint, $data = null, $headers = []): vo
$authHeader = 'Authorization: Basic ' . base64_encode($this->username . ':' . $this->password);
$headers[] = $authHeader;

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

if ($method === 'POST' || $method === 'PUT' || $method === 'PATCH') {
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
$headers[] = 'Content-Type: application/json';
}

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$this->response = json_decode(curl_exec($curl), true);
$this->status = \curl_getinfo($curl, CURLINFO_HTTP_CODE);

Expand All @@ -69,7 +69,7 @@ public function getResponse(): ApiResponse
if (is_string($this->response)) {
$response = new ApiResponse($this->status, null, $this->response);
}
if (in_array($this->status, [200, 204]) && !$this->response) {
if (in_array($this->status, [200, 201, 204]) && !$this->response) {
$response = ApiResponse::create([], $this->status);
}

Expand Down

0 comments on commit 1e065a8

Please sign in to comment.