Skip to content

Commit

Permalink
Merge pull request #44 from sitegeist/bugfix-variable-assignment
Browse files Browse the repository at this point in the history
BUGFIX: API Request with Body was never re-assigned
  • Loading branch information
gradinarufelix authored May 17, 2024
2 parents 87c3812 + ea0de2b commit f186992
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions Classes/Infrastructure/DeepL/DeepLTranslationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ public function translate(array $texts, string $targetLanguage, ?string $sourceL
$body .= '&text=' . urlencode($part);
}

$apiRequest = $this->createRequest('translate', 'POST');
$apiRequest->withBody($this->streamFactory->createStream($body));
$apiRequest = $this->createRequest('translate', 'POST', $body);

$browser = $this->getBrowser();

Expand Down Expand Up @@ -239,20 +238,29 @@ protected function getBrowser(): Browser
}

/**
* @param string $method
* @param string $endpoint
* @param string $endpoint
*
* @param string $method
* @param string|null $body
*
* @return ServerRequestInterface
*/
protected function createRequest(
string $endpoint,
string $method = 'GET'
string $method = 'GET',
string $body = null
): ServerRequestInterface {
$deeplAuthenticationKey = $this->getDeeplAuthenticationKey();
$baseUri = $deeplAuthenticationKey->isFree ? $this->settings['baseUriFree'] : $this->settings['baseUri'];
return $this->serverRequestFactory->createServerRequest($method, $baseUri . $endpoint)
$request = $this->serverRequestFactory->createServerRequest($method, $baseUri . $endpoint)
->withHeader('Accept', 'application/json')
->withHeader('Authorization', sprintf('DeepL-Auth-Key %s', $deeplAuthenticationKey->authenticationKey))
->withHeader('Content-Type', 'application/x-www-form-urlencoded');

if ($body) {
$request = $request->withBody($this->streamFactory->createStream($body));
}

return $request;
}
}

0 comments on commit f186992

Please sign in to comment.