Skip to content
This repository was archived by the owner on May 18, 2020. It is now read-only.

Commit 71260ce

Browse files
committed
API-38: Added default timeout of 30 secs
1 parent 68b16d3 commit 71260ce

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function getConfigTreeBuilder()
2525
// more information on that topic.
2626
$rootNode->children()
2727
->scalarNode('api_key')->isRequired()->cannotBeEmpty()->end()
28+
->scalarNode('timeout')->defaultValue(30000)->end() //default timeout: 30 secs
2829
->end();
2930

3031
return $treeBuilder;

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ In your `app/config/config.yml`:
6060
```yaml
6161
sendin_blue_api:
6262
api_key: <Your access key>
63+
# Our library supports a timeout value, which is an optional parameter, default is 30,000 MS ( 30 secs )
64+
timeout: 5000
6365
```
6466
6567

Wrapper/Mailin.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@
1313

1414
class Mailin
1515
{
16-
public $api_key;
16+
public $parameters;
1717
public $base_url;
1818
public $curl_opts = array();
1919

20-
public function __construct($api_key)
20+
public function __construct($parameters)
2121
{
2222
if(!function_exists('curl_init'))
2323
{
2424
throw new \RuntimeException('Mailin requires cURL module');
2525
}
26-
$this->config = $api_key;
26+
$this->config = $parameters;
2727
$this->base_url = "https://api.sendinblue.com/v2.0";
2828
$this->api_key = $this->config['api_key'];
29+
$this->timeout = $this->config['timeout'];
2930
}
3031
/**
3132
* Do CURL request with authorization
@@ -36,6 +37,9 @@ private function do_request($resource,$method,$input)
3637
$ch = curl_init($called_url);
3738
$auth_header = 'api-key:'.$this->api_key;
3839
$content_header = "Content-Type:application/json";
40+
if (isset($this->timeout) && ($this->timeout <= 0 || $this->timeout > 60000)) {
41+
throw new \Exception('value not allowed for timeout');
42+
}
3943
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
4044
// Windows only over-ride
4145
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
@@ -44,14 +48,15 @@ private function do_request($resource,$method,$input)
4448
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
4549
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
4650
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
51+
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->timeout);
4752
curl_setopt($ch, CURLOPT_HEADER, 0);
4853
curl_setopt($ch, CURLOPT_POSTFIELDS, $input);
4954

5055
$data = curl_exec($ch);
5156
$info = curl_getinfo($ch);
5257
if(curl_errno($ch))
5358
{
54-
throw new \RuntimeException('Curl error: ' . curl_error($ch));
59+
throw new \RuntimeException('cURL error: ' . curl_error($ch));
5560
}
5661
curl_close($ch);
5762
return json_decode($data,true);

0 commit comments

Comments
 (0)