Skip to content

Commit 2d93fd3

Browse files
Merge pull request #11 from ninsuo/master
Added curlOptions property to customize curl instance
2 parents 9ea0345 + 151b07c commit 2d93fd3

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

Diff for: lib/Client.php

+23-11
Original file line numberDiff line numberDiff line change
@@ -29,50 +29,54 @@ class Client
2929
/** @var array */
3030
protected $path;
3131
/** @var array */
32+
protected $curlOptions;
33+
/** @var array */
3234
private $methods;
3335

3436
/**
3537
* Initialize the client
3638
*
37-
* @param string $host the base url (e.g. https://api.sendgrid.com)
38-
* @param array $headers global request headers
39-
* @param string $version api version (configurable)
40-
* @param array $path holds the segments of the url path
39+
* @param string $host the base url (e.g. https://api.sendgrid.com)
40+
* @param array $headers global request headers
41+
* @param string $version api version (configurable)
42+
* @param array $path holds the segments of the url path
43+
* @param array $curlOptions extra options to set during curl initialization
4144
*/
42-
public function __construct($host, $headers = null, $version = null, $path = null)
45+
public function __construct($host, $headers = null, $version = null, $path = null, $curlOptions = null)
4346
{
4447
$this->host = $host;
4548
$this->headers = $headers ?: [];
4649
$this->version = $version;
4750
$this->path = $path ?: [];
51+
$this->curlOptions = $curlOptions ?: [];
4852
// These are the supported HTTP verbs
4953
$this->methods = ['delete', 'get', 'patch', 'post', 'put'];
5054
}
51-
55+
5256
/**
5357
* @return string
5458
*/
5559
public function getHost()
5660
{
5761
return $this->host;
5862
}
59-
63+
6064
/**
6165
* @return array
6266
*/
6367
public function getHeaders()
6468
{
6569
return $this->headers;
6670
}
67-
71+
6872
/**
6973
* @return string|null
7074
*/
7175
public function getVersion()
7276
{
7377
return $this->version;
7478
}
75-
79+
7680
/**
7781
* @return array
7882
*/
@@ -81,6 +85,14 @@ public function getPath()
8185
return $this->path;
8286
}
8387

88+
/**
89+
* @return array
90+
*/
91+
public function getCurlOptions()
92+
{
93+
return $this->curlOptions;
94+
}
95+
8496
/**
8597
* Make a new Client object
8698
*
@@ -129,12 +141,12 @@ public function makeRequest($method, $url, $body = null, $headers = null)
129141
{
130142
$curl = curl_init($url);
131143

132-
curl_setopt_array($curl, [
144+
curl_setopt_array($curl, array_merge([
133145
CURLOPT_RETURNTRANSFER => true,
134146
CURLOPT_HEADER => 1,
135147
CURLOPT_CUSTOMREQUEST => strtoupper($method),
136148
CURLOPT_SSL_VERIFYPEER => false,
137-
]);
149+
], $this->curlOptions));
138150

139151
if (isset($headers)) {
140152
$this->headers = array_merge($this->headers, $headers);

Diff for: test/unit/ClientTest.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected function setUp()
2020
'Content-Type: application/json',
2121
'Authorization: Bearer SG.XXXX'
2222
];
23-
$this->client = new MockClient($this->host, $this->headers, '/v3', null);
23+
$this->client = new MockClient($this->host, $this->headers, '/v3', null, null);
2424
}
2525

2626
public function testConstructor()
@@ -29,6 +29,7 @@ public function testConstructor()
2929
$this->assertAttributeEquals($this->headers, 'headers', $this->client);
3030
$this->assertAttributeEquals('/v3', 'version', $this->client);
3131
$this->assertAttributeEquals([], 'path', $this->client);
32+
$this->assertAttributeEquals([], 'curlOptions', $this->client);
3233
$this->assertAttributeEquals(['delete', 'get', 'patch', 'post', 'put'], 'methods', $this->client);
3334
}
3435

@@ -96,4 +97,13 @@ public function testGetPath()
9697
$client = new Client('https://localhost:4010', null, null, null);
9798
$this->assertSame([], $client->getPath());
9899
}
100+
101+
public function testGetCurlOptions()
102+
{
103+
$client = new Client('https://localhost:4010', null, null, null, [CURLOPT_PROXY => '127.0.0.1:8080']);
104+
$this->assertSame([CURLOPT_PROXY => '127.0.0.1:8080'], $client->getCurlOptions());
105+
106+
$client = new Client('https://localhost:4010', null, null, null, null);
107+
$this->assertSame([], $client->getCurlOptions());
108+
}
99109
}

0 commit comments

Comments
 (0)