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

Commit 5b3ba11

Browse files
committed
Add configurable cURL proxy and timeout
1 parent 3ec0f3a commit 5b3ba11

File tree

3 files changed

+133
-1
lines changed

3 files changed

+133
-1
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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,27 @@ class DeepL
5454
*/
5555
protected $host;
5656

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

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

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

251315
if (curl_errno($this->curl)) {

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 PHPUnit_Framework_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()
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
$this->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)