Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit fb2e151

Browse files
author
sathielemann
committed
Merge branch 'master' into PHP-8-0
# Conflicts: # tests/integration/DeepLApiTest.php
2 parents 5a6060d + 4c5c585 commit fb2e151

File tree

3 files changed

+134
-2
lines changed

3 files changed

+134
-2
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,21 @@ foreach ($targetLanguagesArray as $targetLanguage) {
9999
}
100100
```
101101
### Monitoring usage
102-
You can now check ow much you translate, as well as the limit:
102+
You can now check how much you translate, as well as the limit:
103103
```php
104104
$usageArray = $deepl->usage();
105105

106106
echo 'You have used '.$usageArray['character_count'].' of '.$usageArray['character_limit'].' in the current billing period.'.PHP_EOL;
107107

108+
```
109+
110+
### Configuring cURL requests
111+
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
112+
```php
113+
$deepl->setTimeout(10); //give up after 10 seconds
114+
$deepl->setProxy('http://corporate-proxy.com:3128');
115+
$deepl->setProxyCredentials('username:password');
116+
108117
```
109118
## Testing
110119

src/DeepL.php

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,27 @@ class DeepL
5656
*/
5757
protected $host;
5858

59+
/**
60+
* URL of the proxy used to connect to DeepL (if needed)
61+
*
62+
* @var string|null
63+
*/
64+
protected $proxy = null;
65+
66+
/**
67+
* Credentials for the proxy used to connect to DeepL (username:password)
68+
*
69+
* @var string|null
70+
*/
71+
protected $proxyCredentials = null;
72+
73+
/**
74+
* Maximum number of seconds the query should take
75+
*
76+
* @var int|null
77+
*/
78+
protected $timeout = null;
79+
5980
/**
6081
* DeepL constructor
6182
*
@@ -100,6 +121,37 @@ public function languages($type = null)
100121
return $languages;
101122
}
102123

124+
/**
125+
* Set a proxy to use for querying the DeepL API if needed
126+
*
127+
* @param string $proxy Proxy URL (e.g 'http://proxy-domain.com:3128')
128+
*/
129+
public function setProxy($proxy)
130+
{
131+
132+
$this->proxy = $proxy;
133+
}
134+
135+
/**
136+
* Set the proxy credentials
137+
*
138+
* @param string $proxyCredentials proxy credentials (using 'username:password' format)
139+
*/
140+
public function setProxyCredentials($proxyCredentials)
141+
{
142+
$this->proxyCredentials = $proxyCredentials;
143+
}
144+
145+
/**
146+
* Set a timeout for queries to the DeepL API
147+
*
148+
* @param int $timeout Timeout in seconds
149+
*/
150+
public function setTimeout($timeout)
151+
{
152+
$this->timeout = $timeout;
153+
}
154+
103155
/**
104156
* Translate the text string or array from source to destination language
105157
* For detailed info on Parameters see README.md
@@ -248,10 +300,22 @@ protected function request($url, $body = '')
248300
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body);
249301
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
250302

303+
if ($this->proxy !== null) {
304+
curl_setopt($this->curl, CURLOPT_PROXY, $this->proxy);
305+
}
306+
307+
if ($this->proxyCredentials !== null) {
308+
curl_setopt($this->curl, CURLOPT_PROXYAUTH, $this->proxyCredentials);
309+
}
310+
311+
if ($this->timeout !== null) {
312+
curl_setopt($this->curl, CURLOPT_TIMEOUT, $this->timeout);
313+
}
314+
251315
$response = curl_exec($this->curl);
252316

253317
if (curl_errno($this->curl)) {
254-
throw new DeepLException('There was a cURL Request Error.');
318+
throw new DeepLException('There was a cURL Request Error : ' . curl_error($this->curl));
255319
}
256320
$httpCode = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
257321
$responseArray = json_decode($response, true);

tests/integration/DeepLApiTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ class DeepLApiTest extends TestCase
2121
*/
2222
protected static $authKey = false;
2323

24+
/**
25+
* Proxy URL
26+
* @var bool|string
27+
*/
28+
private static $proxy;
29+
30+
/**
31+
* Proxy Credentials
32+
* @var bool|string
33+
*/
34+
private static $proxyCredentials;
35+
2436
/**
2537
* Setup DeepL Auth Key.
2638
*/
@@ -29,12 +41,16 @@ public static function setUpBeforeClass(): void
2941
parent::setUpBeforeClass();
3042

3143
$authKey = getenv('DEEPL_AUTH_KEY');
44+
$proxy = getenv('HTTP_PROXY');
45+
$proxyCredentials = getenv('HTTP_PROXY_CREDENTIALS');
3246

3347
if ($authKey === false) {
3448
return;
3549
}
3650

3751
self::$authKey = $authKey;
52+
self::$proxy = $proxy;
53+
self::$proxyCredentials = $proxyCredentials;
3854
}
3955

4056
/**
@@ -276,6 +292,49 @@ public function testTranslateWithAllParams()
276292
self::assertEquals($expectedText, $translatedText[0]['text']);
277293
}
278294

295+
/**
296+
* Test translate() with all Params
297+
*/
298+
public function testWithProxy()
299+
{
300+
if (self::$authKey === false) {
301+
$this->markTestSkipped('DeepL Auth Key (DEEPL_AUTH_KEY) is not configured.');
302+
}
303+
304+
if (self::$proxy === false) {
305+
// The test would succeed with $proxy === false but it wouln't mean anything.
306+
$this->markTestSkipped('Proxy is not configured.');
307+
}
308+
309+
$deepl = new DeepL(self::$authKey);
310+
$deepl->setProxy(self::$proxy);
311+
$deepl->setProxyCredentials(self::$proxyCredentials);
312+
313+
$englishText = 'please translate this text';
314+
$expectedText = 'Bitte übersetzen Sie diesen Text';
315+
316+
$translatedText = $deepl->translate($englishText, 'en', 'de');
317+
318+
$this->assertEquals($expectedText, $translatedText[0]['text']);
319+
}
320+
321+
/**
322+
* Test translate() with all Params
323+
*/
324+
public function testCustomTimeout()
325+
{
326+
$deepl = new DeepL(self::$authKey, 2, '10.255.255.1'); // non routable IP, should timeout.
327+
$deepl->setTimeout(2);
328+
329+
$start = time();
330+
try {
331+
$deepl->translate('some text');
332+
} catch (\Exception $e) {
333+
$time = time() - $start;
334+
$this->assertLessThan(4, $time);
335+
}
336+
}
337+
279338
/**
280339
* Test translate() $formality
281340
*/

0 commit comments

Comments
 (0)