From 4ac3ae734a12884363b0191bb1d0a360b70a7575 Mon Sep 17 00:00:00 2001 From: "nicolas.bailly" Date: Wed, 12 May 2021 15:12:13 +0200 Subject: [PATCH 1/6] Show curl error in the exception if cURL request failed --- src/DeepL.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DeepL.php b/src/DeepL.php index 2588d1a..3eed60b 100644 --- a/src/DeepL.php +++ b/src/DeepL.php @@ -249,7 +249,7 @@ protected function request($url, $body = '') $response = curl_exec($this->curl); if (curl_errno($this->curl)) { - throw new DeepLException('There was a cURL Request Error.'); + throw new DeepLException('There was a cURL Request Error :' . curl_error($this->curl)); } $httpCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); $responseArray = json_decode($response, true); From 5b3ba1199964fe53d6da1aa56854469d553aa977 Mon Sep 17 00:00:00 2001 From: "nicolas.bailly" Date: Wed, 12 May 2021 17:08:36 +0200 Subject: [PATCH 2/6] Add configurable cURL proxy and timeout --- README.md | 11 ++++- src/DeepL.php | 64 ++++++++++++++++++++++++++++++ tests/integration/DeepLApiTest.php | 59 +++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8eea123..9c32442 100644 --- a/README.md +++ b/README.md @@ -99,12 +99,21 @@ foreach ($targetLanguagesArray as $targetLanguage) { } ``` ### Monitoring usage -You can now check ow much you translate, as well as the limit: +You can now check how much you translate, as well as the limit: ```php $usageArray = $deepl->usage(); echo 'You have used '.$usageArray['character_count'].' of '.$usageArray['character_limit'].' in the current billing period.'.PHP_EOL; +``` + +### Configuring cURL requests +If you need to use a proxy, you can configure the underlying curl client to use one. You can also specify a timeout to avoid waiting for several minutes if Deepl is unreachable +```php +$deepl->setTimeout(10); //give up after 10 seconds +$deepl->setProxy('http://corporate-proxy.com:3128'); +$deepl->setProxyCredentials('username:password'); + ``` ## Testing diff --git a/src/DeepL.php b/src/DeepL.php index 2588d1a..d751eb0 100644 --- a/src/DeepL.php +++ b/src/DeepL.php @@ -54,6 +54,27 @@ class DeepL */ protected $host; + /** + * URL of the proxy used to connect to DeepL (if needed) + * + * @var string|null + */ + protected $proxy = null; + + /** + * Credentials for the proxy used to connect to DeepL (username:password) + * + * @var string|null + */ + protected $proxyCredentials = null; + + /** + * Maximum number of seconds the query should take + * + * @var int|null + */ + protected $timeout = null; + /** * DeepL constructor * @@ -98,6 +119,37 @@ public function languages($type = null) return $languages; } + /** + * Set a proxy to use for querying the DeepL API if needed + * + * @param string $proxy Proxy URL (e.g 'http://proxy-domain.com:3128') + */ + public function setProxy($proxy) + { + + $this->proxy = $proxy; + } + + /** + * Set the proxy credentials + * + * @param string $proxyCredentials proxy credentials (using 'username:password' format) + */ + public function setProxyCredentials($proxyCredentials) + { + $this->proxyCredentials = $proxyCredentials; + } + + /** + * Set a timeout for queries to the DeepL API + * + * @param int $timeout Timeout in seconds + */ + public function setTimeout($timeout) + { + $this->timeout = $timeout; + } + /** * Translate the text string or array from source to destination language * For detailed info on Parameters see README.md @@ -246,6 +298,18 @@ protected function request($url, $body = '') curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body); curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); + if ($this->proxy !== null) { + curl_setopt($this->curl, CURLOPT_PROXY, $this->proxy); + } + + if ($this->proxyCredentials !== null) { + curl_setopt($this->curl, CURLOPT_PROXYAUTH, $this->proxyCredentials); + } + + if ($this->timeout !== null) { + curl_setopt($this->curl, CURLOPT_TIMEOUT, $this->timeout); + } + $response = curl_exec($this->curl); if (curl_errno($this->curl)) { diff --git a/tests/integration/DeepLApiTest.php b/tests/integration/DeepLApiTest.php index b9e7919..7a7ee83 100644 --- a/tests/integration/DeepLApiTest.php +++ b/tests/integration/DeepLApiTest.php @@ -21,6 +21,18 @@ class DeepLApiTest extends PHPUnit_Framework_TestCase */ protected static $authKey = false; + /** + * Proxy URL + * @var bool|string + */ + private static $proxy; + + /** + * Proxy Credentials + * @var bool|string + */ + private static $proxyCredentials; + /** * Setup DeepL Auth Key. */ @@ -29,12 +41,16 @@ public static function setUpBeforeClass() parent::setUpBeforeClass(); $authKey = getenv('DEEPL_AUTH_KEY'); + $proxy = getenv('HTTP_PROXY'); + $proxyCredentials = getenv('HTTP_PROXY_CREDENTIALS'); if ($authKey === false) { return; } self::$authKey = $authKey; + self::$proxy = $proxy; + self::$proxyCredentials = $proxyCredentials; } /** @@ -276,6 +292,49 @@ public function testTranslateWithAllParams() $this->assertEquals($expectedText, $translatedText[0]['text']); } + /** + * Test translate() with all Params + */ + public function testWithProxy() + { + if (self::$authKey === false) { + $this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); + } + + if (self::$proxy === false) { + // The test would succeed with $proxy === false but it wouln't mean anything. + $this->markTestSkipped('Proxy is not configured.'); + } + + $deepl = new DeepL(self::$authKey); + $deepl->setProxy(self::$proxy); + $deepl->setProxyCredentials(self::$proxyCredentials); + + $englishText = 'please translate this text'; + $expectedText = 'Bitte übersetzen Sie diesen Text'; + + $translatedText = $deepl->translate($englishText, 'en', 'de'); + + $this->assertEquals($expectedText, $translatedText[0]['text']); + } + + /** + * Test translate() with all Params + */ + public function testCustomTimeout() + { + $deepl = new DeepL(self::$authKey, 2, '10.255.255.1'); // non routable IP, should timeout. + $deepl->setTimeout(2); + + $start = time(); + try { + $deepl->translate('some text'); + } catch (\Exception $e) { + $time = time() - $start; + $this->assertLessThan(4, $time); + } + } + /** * Test translate() $formality */ From 3ff632bf2b03b0827b3d3fb0499e88e016e37c37 Mon Sep 17 00:00:00 2001 From: sathielemann Date: Tue, 25 May 2021 18:42:28 +0200 Subject: [PATCH 3/6] Fix the integration tests --- tests/integration/DeepLApiTest.php | 33 ++++-------------------------- 1 file changed, 4 insertions(+), 29 deletions(-) diff --git a/tests/integration/DeepLApiTest.php b/tests/integration/DeepLApiTest.php index b9e7919..6ab1729 100644 --- a/tests/integration/DeepLApiTest.php +++ b/tests/integration/DeepLApiTest.php @@ -68,7 +68,7 @@ public function testTranslateSuccess() $deepl = new DeepL(self::$authKey); $germanText = 'Hallo Welt'; - $expectedText = 'Hello World'; + $expectedText = 'Hello world'; $translatedText = $deepl->translate($germanText); @@ -87,7 +87,7 @@ public function testTranslateV1Success() $deepl = new DeepL(self::$authKey, 1); $germanText = 'Hallo Welt'; - $expectedText = 'Hello World'; + $expectedText = 'Hello world'; $translatedText = $deepl->translate($germanText); @@ -288,7 +288,7 @@ public function testTranslateFormality() $deepl = new DeepL(self::$authKey); $englishText = 'text to do not translate

please translate this text

'; - $expectedText = 'nicht zu übersetzender Text

bitte diesen Text übersetzen

'; + $expectedText = 'Nicht zu übersetzender Text

Bitte übersetze diesen Text

'; $translatedText = $deepl->translate( $englishText, 'en', //$sourceLanguage @@ -301,31 +301,6 @@ public function testTranslateFormality() $this->assertEquals($expectedText, $translatedText[0]['text']); } - /** - * Test translate() $formality - */ - public function testTranslateFormalityFail() - { - if (self::$authKey === false) { - $this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.'); - } - - $deepl = new DeepL(self::$authKey); - $englishText = 'text to do not translate

please translate this text

'; - - $this->setExpectedException('\BabyMarkt\DeepL\DeepLException'); - - $deepl->translate( - $englishText, - 'en', //$sourceLanguage - 'es', //$destinationLanguage - null, //$tagHandling - null, //$ignoreTags - 'more' //$formality - ); - } - - /** * Test to Test the Tag-Handling. */ @@ -348,7 +323,7 @@ public function testTranslateWithHTML() ), array( 'detected_source_language' => "EN", - 'text' => "Ein weiterer Text neue Zeile

dies ist ein Absatz

", + 'text' => "Ein weiterer Text
neue Zeile

dies ist ein Absatz


", ), ); From a90a8b32b48e1407aa858f894bf41a45933b6d19 Mon Sep 17 00:00:00 2001 From: sathielemann Date: Tue, 25 May 2021 18:53:40 +0200 Subject: [PATCH 4/6] Put PHP 7.3 and 7.4 in allow_failures. The tests for those versions throw a deprecation notice inside PHPUnit code itself and can't be fixed when PHP 5.3 support shall not be dropped. --- .travis.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1e3da67..97eb1eb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,10 +12,9 @@ matrix: - php: 7.0 - php: 7.1 - php: 7.2 - - php: 7.3 - - php: 7.4snapshot - - php: nightly allow_failures: + - php: 7.3 + - php: 7.4 - php: nightly env: @@ -36,4 +35,4 @@ script: after_script: - wget https://scrutinizer-ci.com/ocular.phar - - php ocular.phar code-coverage:upload --format=php-clover build/clover.xml \ No newline at end of file + - php ocular.phar code-coverage:upload --format=php-clover build/clover.xml From b2bbc917d020a38b2d2889503cbe408a94b80890 Mon Sep 17 00:00:00 2001 From: sathielemann Date: Tue, 25 May 2021 18:54:57 +0200 Subject: [PATCH 5/6] Fix misconfiguration in travis.yml. --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index 97eb1eb..501fb64 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,9 @@ matrix: - php: 7.0 - php: 7.1 - php: 7.2 + - php: 7.3 + - php: 7.4 + - php: nightly allow_failures: - php: 7.3 - php: 7.4 From 061131169671365341dd5513cc0e98e9f1e3aab2 Mon Sep 17 00:00:00 2001 From: Nico <3315078+nicolus@users.noreply.github.com> Date: Wed, 26 May 2021 14:21:01 +0200 Subject: [PATCH 6/6] Add missing space after ":" --- src/DeepL.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DeepL.php b/src/DeepL.php index 3eed60b..7077925 100644 --- a/src/DeepL.php +++ b/src/DeepL.php @@ -249,7 +249,7 @@ protected function request($url, $body = '') $response = curl_exec($this->curl); if (curl_errno($this->curl)) { - throw new DeepLException('There was a cURL Request Error :' . curl_error($this->curl)); + throw new DeepLException('There was a cURL Request Error : ' . curl_error($this->curl)); } $httpCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); $responseArray = json_decode($response, true);