Skip to content
This repository has been archived by the owner on Sep 6, 2019. It is now read-only.

Commit

Permalink
added an addFile method to add files to the request
Browse files Browse the repository at this point in the history
  • Loading branch information
pulkit committed Dec 19, 2014
1 parent e6b435f commit 2222ae3
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 42 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"require": {
"php": ">=5.4.0",
"guzzlehttp/guzzle": "5.0.*",
"guzzlehttp/retry-subscriber": "2.0.*"
"guzzlehttp/retry-subscriber": "2.0.*",
"illuminate/support": "~4|~5"
},
"autoload": {
"psr-4": {
Expand Down
137 changes: 96 additions & 41 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ class Client
*/
protected $guzzleClient;

/**
* @var array
*/
protected $config = [];

/**
* Url
*
Expand Down Expand Up @@ -54,20 +59,33 @@ class Client
protected $retry = 5;

/**
* @param \GuzzleHttp\Client $guzzleClient
* @param \GuzzleHttp\Client $guzzleClient
* @param array $config
*/
public function __construct(GuzzleClient $guzzleClient)
public function __construct(GuzzleClient $guzzleClient, array $config = [])
{
$this->guzzleClient = $guzzleClient;
$this->config = $config;

$this->initilize();
}

/**
* Getter for guzzle client
*
* @return \GuzzleHttp\Client
*/
public function getGuzzleClient()
{
return $this->guzzleClient;
}

/**
* Set the url
* will automatically append the protocol
*
* @param string $base the base url, can be url or something from config
* @param string $protocol custom protocol to add
*
* @param string $base the base url, can be url or something from config
* @param string $protocol custom protocol to add
* @return \PulkitJalan\Requester\Client
*/
public function url($url)
Expand All @@ -80,8 +98,7 @@ public function url($url)
/**
* Use secure endpoint or not
*
* @param boolean $secure
*
* @param boolean $secure
* @return \PulkitJalan\Requester\Client
*/
public function secure($secure)
Expand All @@ -94,8 +111,7 @@ public function secure($secure)
/**
* Verify ssl or not
*
* @param boolean|string $verify boolean or path to certificate
*
* @param boolean|string $verify boolean or path to certificate
* @return \PulkitJalan\Requester\Client
*/
public function verify($verify)
Expand All @@ -108,22 +124,20 @@ public function verify($verify)
/**
* Set headers for the request
*
* @param array $headers
*
* @param array $headers
* @return \PulkitJalan\Requester\Client
*/
public function headers(array $headers)
{
$this->options = array_merge($this->options, ['headers' => $headers]);
$this->options = array_merge_recursive($this->options, ['headers' => $headers]);

return $this;
}

/**
* Number if times to retry
*
* @param int $retry times to retry
*
* @param int $retry times to retry
* @return \PulkitJalan\Requester\Client
*/
public function retry($retry)
Expand All @@ -136,8 +150,7 @@ public function retry($retry)
/**
* Delay between retrying
*
* @param int $retryDelay delay between retrying
*
* @param int $retryDelay delay between retrying
* @return \PulkitJalan\Requester\Client
*/
public function every($retryDelay)
Expand All @@ -150,7 +163,7 @@ public function every($retryDelay)
/**
* Types of errors to retry on
*
* @param array $retryOn errors to retry on
* @param array $retryOn errors to retry on
* @return \PulkitJalan\Requester\Client
*/
public function on(array $retryOn)
Expand All @@ -160,11 +173,29 @@ public function on(array $retryOn)
return $this;
}

/**
* Add a file to the request
*
* @param string $filepath path to file
* @param string $key optional post key, default to file
* @return \PulkitJalan\Requester\Client
*/
public function addFile($filepath, $key = 'file')
{
$this->options = array_merge_recursive($this->options, [
'body' => [
$key => fopen($filepath, 'r')
]
]);

return $this;
}

/**
* Send get request
*
* @param array $options
* @return guzzle response
* @param array $options
* @return \GuzzleHttp\Message\ResponseInterface
*/
public function get(array $options = [])
{
Expand All @@ -174,8 +205,8 @@ public function get(array $options = [])
/**
* Send head request
*
* @param array $options
* @return guzzle response
* @param array $options
* @return \GuzzleHttp\Message\ResponseInterface
*/
public function head(array $options = [])
{
Expand All @@ -185,8 +216,8 @@ public function head(array $options = [])
/**
* Send delete request
*
* @param array $options
* @return guzzle response
* @param array $options
* @return \GuzzleHttp\Message\ResponseInterface
*/
public function delete(array $options = [])
{
Expand All @@ -196,8 +227,8 @@ public function delete(array $options = [])
/**
* Send put request
*
* @param array $options
* @return guzzle response
* @param array $options
* @return \GuzzleHttp\Message\ResponseInterface
*/
public function put(array $options = [])
{
Expand All @@ -207,8 +238,8 @@ public function put(array $options = [])
/**
* Send patch request
*
* @param array $options
* @return guzzle response
* @param array $options
* @return \GuzzleHttp\Message\ResponseInterface
*/
public function patch(array $options = [])
{
Expand All @@ -218,8 +249,8 @@ public function patch(array $options = [])
/**
* Send post request
*
* @param array $options
* @return guzzle response
* @param array $options
* @return \GuzzleHttp\Message\ResponseInterface
*/
public function post(array $options = [])
{
Expand All @@ -229,8 +260,8 @@ public function post(array $options = [])
/**
* Send options request
*
* @param array $options
* @return guzzle response
* @param array $options
* @return \GuzzleHttp\Message\ResponseInterface
*/
public function options(array $options = [])
{
Expand All @@ -239,7 +270,6 @@ public function options(array $options = [])

/**
* Getter for the url will append protocol if one does not exist
*
* @return string
*/
public function getUrl()
Expand All @@ -256,27 +286,33 @@ public function getUrl()
/**
* Send the request using guzzle
*
* @param string $function function to call on guzzle
* @param array $options options to pass
*
* @return guzzle response
* @param string $function function to call on guzzle
* @param array $options options to pass
* @return \GuzzleHttp\Message\ResponseInterface
*/
protected function send($function, array $options = [])
{
$guzzle = $this->getGuzzleClient();

if ($this->retry) {
$this->addRetrySubscriber();
$guzzle = $this->addRetrySubscriber($guzzle);
}

$url = $this->getUrl();

// merge options
$options = array_merge($this->options, $options);
$options = array_merge_recursive($this->options, $options);

// need to reset after every request
$this->initilize();

return $this->guzzleClient->$function($this->getUrl(), $options);
return $guzzle->$function($url, $options);
}

/**
* Add the retry subscriber to the guzzle client
*/
protected function addRetrySubscriber()
protected function addRetrySubscriber(GuzzleClient $guzzle)
{
// Build retry subscriber
$retry = new RetrySubscriber([
Expand All @@ -288,7 +324,9 @@ protected function addRetrySubscriber()
]);

// add the retry emitter
$this->guzzleClient->getEmitter()->attach($retry);
$guzzle->getEmitter()->attach($retry);

return $guzzle;
}

/**
Expand All @@ -306,4 +344,21 @@ protected function getProtocol()

return $protocol . '://';
}

/**
* Resets all variables to default values
* required if using the same instance for multiple requests
*
* @return void
*/
protected function initilize()
{
$this->url = '';
$this->options = [];
$this->secure = array_get($this->config, 'secure', true);
$this->retryOn = array_get($this->config, 'retry.on', [500, 502, 503, 504]);
$this->retryDelay = array_get($this->config, 'retry.delay', 10);
$this->retry = array_get($this->config, 'retry.times', 5);
$this->verify(array_get($this->config, 'verify', true));
}
}

0 comments on commit 2222ae3

Please sign in to comment.