Skip to content

Commit

Permalink
Abstracting curl setup
Browse files Browse the repository at this point in the history
  • Loading branch information
drewm committed Jan 30, 2019
1 parent 3f56630 commit b1a854b
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions src/Drip.php
Expand Up @@ -38,7 +38,7 @@ public static function subscribeToWebhook($event, callable $callback)

public static function receiveWebhook($input = null)
{
if (is_null($input)) {
if ($input === null) {
if (self::$receivedWebhook !== false) {
$input = self::$receivedWebhook;
} else {
Expand Down Expand Up @@ -84,7 +84,7 @@ protected static function dispatchWebhookEvent($event, $data)
*
* @param string $accountID
*
* @return bool
* @return void
*/
public function setAccountId($accountID)
{
Expand Down Expand Up @@ -123,20 +123,7 @@ protected function makeRequest($http_verb, $api_method, $args = [], $timeout = 1
$this->checkDependencies();

$url = $this->constructRequestUrl($url, $api_method);

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept: application/vnd.api+json',
'Content-Type: application/vnd.api+json',
]);
curl_setopt($ch, CURLOPT_USERAGENT, 'DrewM/Drip (github.com/drewm/drip)');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $this->token . ': ');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_URL, $url);
$ch = $this->createCurlSession($url, $timeout);

switch ($http_verb) {
case 'post':
Expand Down Expand Up @@ -186,10 +173,9 @@ private function checkDependencies()
return true;
}


/**
* @param string|null $url
* @param string $api_method
* @param string $api_method
*
* @return string
* @throws DripException
Expand All @@ -207,6 +193,33 @@ private function constructRequestUrl($url, $api_method)
return $this->api_endpoint . '/' . $this->accountID . '/' . $api_method;
}

/**
* Create a new CURL session (common setup etc)
*
* @param string $url
* @param int $timeout
*
* @return false|resource
*/
private function createCurlSession($url, $timeout = 10)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept: application/vnd.api+json',
'Content-Type: application/vnd.api+json',
]);
curl_setopt($ch, CURLOPT_USERAGENT, 'DrewM/Drip (github.com/drewm/drip)');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $this->token . ': ');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_URL, $url);

return $ch;
}

/**
* Make a GET request to a top-level method outside of this account
*
Expand Down

0 comments on commit b1a854b

Please sign in to comment.