Skip to content

Commit

Permalink
Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson committed Jan 16, 2015
1 parent dedea70 commit 8cb9bad
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
36 changes: 28 additions & 8 deletions placid/pi.placid.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,31 @@ class Plugin_placid extends Plugin {
'author_url' => 'http://www.alecritson.co.uk'
);
var $options = array();
var $curlOpts = array();

public function index()
{
// Get the request
// -------------------------------------------------------

$handle = $this->fetchParam('handle', null, null, false, false);
$request = $this->fetch($handle) ? $this->fetch($handle) : null;
$request = $this->fetch($handle, null, null, false, false);


// Set our options
// ---------------------------------------------------------
$options = array(
'cache' => (bool) $this->_getOption($request, 'cache', true, null, true, true),
'cache_length' => (int) $this->_getOption($request, 'refresh', $this->fetch('placid_defaults')['refresh'] ?: 3200),
'method' => $this->_getOption($request, 'method', 'GET'),
'curl' => $this->_getOption($request, 'curl'),
'access_token' => $this->_getOption($request, 'access_token'),
'query' => $this->_getOption($request, 'query'),
'headers' => $this->_getOption($request, 'headers', null)
);



// We only want to try and explode the query if it's been set as a parameter,
// not when there is a record.
if($options['query'] && !$request)
Expand Down Expand Up @@ -90,10 +95,19 @@ public function index()
}
$options['access_token'] = $token;
}


if($options['curl'])
{
// list($key, $value) = $options['curl'];
foreach($options['curl'] as $key => $value) {
$curlOpts[$key] = $value;
}
}

// Get the request object from the tasks
$request = $this->tasks->client()->request($url, $options['method']);


// Grab the query from the request
$query = $request->getQuery();

Expand All @@ -109,32 +123,37 @@ public function index()
// Do headers exist and is it an array?
if($options['headers'] && is_array($options['headers']))
{

foreach ($options['headers'] as $key => $value)
{
$request->setHeader($key, $value);
}
}


// Do we have an access token we need to append?
if($options['access_token'])
{
$query->set('access_token', $options['access_token']);
}

/**
* Try and get the response
*
* TODO:
* - Create a log if something goes wrong, at the mo Log::warn($e->getMessage(), $meta['name'], $meta['version']) isn't working :(
*
**/
try
{
$response = $this->tasks->client()->send($request);

try {

$response = $this->tasks->client()->send($request, $curlOpts);

$result = $response->json();

} catch(\Exception $e)
{
Log::error($e->getMessage());
// If an exception is thrown we set the result to null, this will help with the 'no_results' tag
// The error log bit should go here
$result = null;
Expand All @@ -150,7 +169,8 @@ public function index()
if(!$result) {
return Parse::template($this->content, array('no_results' => true));
}



return $result;
}

Expand All @@ -177,7 +197,7 @@ private function _getUrl($record) {
return $url;
}

private function _getOption($request, $id, $default=NULL, $validity_check=NULL, $is_boolean=FALSE, $force_lower=TRUE)
private function _getOption($request, $id, $default=NULL, $validity_check=NULL, $is_boolean=FALSE, $force_lower=FALSE)
{
return isset($request[$id]) ? $request[$id] : $this->fetchParam($id, $default, $validity_check, $is_boolean, $force_lower);
}
Expand Down
5 changes: 3 additions & 2 deletions placid/tasks.placid.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ public function request($url, $method = 'GET')
* @return GuzzleHttp\Message\Response Object
**/

public function send($request)
public function send($request, $options = array())
{
return $this->client->send($request);

return $this->client->get($request->getUrl(), $options);
}

// public function
Expand Down

0 comments on commit 8cb9bad

Please sign in to comment.