Skip to content

Added curlOptions property to customize curl instance #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,50 +29,54 @@ class Client
/** @var array */
protected $path;
/** @var array */
protected $curlOptions;
/** @var array */
private $methods;

/**
* Initialize the client
*
* @param string $host the base url (e.g. https://api.sendgrid.com)
* @param array $headers global request headers
* @param string $version api version (configurable)
* @param array $path holds the segments of the url path
* @param string $host the base url (e.g. https://api.sendgrid.com)
* @param array $headers global request headers
* @param string $version api version (configurable)
* @param array $path holds the segments of the url path
* @param array $curlOptions extra options to set during curl initialization
*/
public function __construct($host, $headers = null, $version = null, $path = null)
public function __construct($host, $headers = null, $version = null, $path = null, $curlOptions = null)
{
$this->host = $host;
$this->headers = $headers ?: [];
$this->version = $version;
$this->path = $path ?: [];
$this->curlOptions = $curlOptions ?: [];
// These are the supported HTTP verbs
$this->methods = ['delete', 'get', 'patch', 'post', 'put'];
}

/**
* @return string
*/
public function getHost()
{
return $this->host;
}

/**
* @return array
*/
public function getHeaders()
{
return $this->headers;
}

/**
* @return string|null
*/
public function getVersion()
{
return $this->version;
}

/**
* @return array
*/
Expand All @@ -81,6 +85,14 @@ public function getPath()
return $this->path;
}

/**
* @return array
*/
public function getCurlOptions()
{
return $this->curlOptions;
}

/**
* Make a new Client object
*
Expand Down Expand Up @@ -129,12 +141,12 @@ public function makeRequest($method, $url, $body = null, $headers = null)
{
$curl = curl_init($url);

curl_setopt_array($curl, [
curl_setopt_array($curl, array_merge([
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => 1,
CURLOPT_CUSTOMREQUEST => strtoupper($method),
CURLOPT_SSL_VERIFYPEER => false,
]);
], $this->curlOptions));

if (isset($headers)) {
$this->headers = array_merge($this->headers, $headers);
Expand Down
12 changes: 11 additions & 1 deletion test/unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected function setUp()
'Content-Type: application/json',
'Authorization: Bearer SG.XXXX'
];
$this->client = new MockClient($this->host, $this->headers, '/v3', null);
$this->client = new MockClient($this->host, $this->headers, '/v3', null, null);
}

public function testConstructor()
Expand All @@ -29,6 +29,7 @@ public function testConstructor()
$this->assertAttributeEquals($this->headers, 'headers', $this->client);
$this->assertAttributeEquals('/v3', 'version', $this->client);
$this->assertAttributeEquals([], 'path', $this->client);
$this->assertAttributeEquals([], 'curlOptions', $this->client);
$this->assertAttributeEquals(['delete', 'get', 'patch', 'post', 'put'], 'methods', $this->client);
}

Expand Down Expand Up @@ -96,4 +97,13 @@ public function testGetPath()
$client = new Client('https://localhost:4010', null, null, null);
$this->assertSame([], $client->getPath());
}

public function testGetCurlOptions()
{
$client = new Client('https://localhost:4010', null, null, null, [CURLOPT_PROXY => '127.0.0.1:8080']);
$this->assertSame([CURLOPT_PROXY => '127.0.0.1:8080'], $client->getCurlOptions());

$client = new Client('https://localhost:4010', null, null, null, null);
$this->assertSame([], $client->getCurlOptions());
}
}