Skip to content

Commit 144c56a

Browse files
author
Matthijs van Dorp
committed
Add the ability to add additional HTTP options to the HttpClient [messagebird#48]
1 parent 850fbff commit 144c56a

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/MessageBird/Client.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,10 @@ private function getPhpVersion()
212212

213213
return 'PHP/' . PHP_VERSION_ID;
214214
}
215+
216+
public function addHttpOption($key, $value) {
217+
$this->ChatAPIHttpClient->addHttpOption($key, $value);
218+
$this->HttpClient->addHttpOption($key, $value);
219+
$this->VoiceAPIHttpClient->addHttpOption($key, $value);
220+
}
215221
}

src/MessageBird/Common/HttpClient.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ class HttpClient
5050
*/
5151
private $headers = array();
5252

53+
54+
/**
55+
* @var array
56+
*/
57+
private $httpOptions = array();
58+
5359
/**
5460
* @param string $endpoint
5561
* @param int $timeout > 0
@@ -123,6 +129,15 @@ public function setHeaders(array $headers)
123129
$this->headers = $headers;
124130
}
125131

132+
133+
public function addHttpOption($key, $value) {
134+
$this->httpOptions[$key] = $value;
135+
}
136+
137+
public function getHttpOption( $key) {
138+
return isset($this->httpOptions[$key]) ? $this->httpOptions[$key] : null;
139+
}
140+
126141
/**
127142
* @param string $method
128143
* @param string $resourceName
@@ -159,6 +174,10 @@ public function performHttpRequest($method, $resourceName, $query = null, $body
159174
curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout);
160175
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->connectionTimeout);
161176

177+
foreach( $this->httpOptions as $key=>$value) {
178+
curl_setopt($curl, $key, $value);
179+
}
180+
162181
if ($method === self::REQUEST_GET) {
163182
curl_setopt($curl, CURLOPT_HTTPGET, true);
164183
} elseif ($method === self::REQUEST_POST) {

tests/integration/HttpClientTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,19 @@ public function testHttpClientWithoutAuthenticationException()
6060
$client = new HttpClient(Client::ENDPOINT);
6161
$client->performHttpRequest('foo', 'bar');
6262
}
63+
64+
/**
65+
* Test that it allows additional CURL options
66+
*/
67+
public function testHttpClientWithHttpOptions()
68+
{
69+
$client = new HttpClient(Client::ENDPOINT);
70+
$url = "127.0.0.1:8080";
71+
72+
$client->addHttpOption(CURLOPT_PROXY, $url);
73+
74+
$this->assertSame($client->getHttpOption(CURLOPT_PROXY), $url);
75+
76+
}
77+
6378
}

0 commit comments

Comments
 (0)