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

Commit

Permalink
Merge branch 'master' into PHP-8-0
Browse files Browse the repository at this point in the history
# Conflicts:
#	tests/integration/DeepLApiTest.php
  • Loading branch information
sathielemann committed Jun 24, 2021
2 parents 5a6060d + 4c5c585 commit fb2e151
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 2 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
66 changes: 65 additions & 1 deletion src/DeepL.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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
*
Expand Down Expand Up @@ -100,6 +121,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
Expand Down Expand Up @@ -248,10 +300,22 @@ 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)) {
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);
Expand Down
59 changes: 59 additions & 0 deletions tests/integration/DeepLApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ class DeepLApiTest extends 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.
*/
Expand All @@ -29,12 +41,16 @@ public static function setUpBeforeClass(): void
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;
}

/**
Expand Down Expand Up @@ -276,6 +292,49 @@ public function testTranslateWithAllParams()
self::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
*/
Expand Down

0 comments on commit fb2e151

Please sign in to comment.