Skip to content

Commit

Permalink
Merge pull request #40 from rtconner/master
Browse files Browse the repository at this point in the history
Code cleanup commits - no functionality changes.
  • Loading branch information
omniphx committed Jan 28, 2015
2 parents d168de4 + 47d64e6 commit e9ea262
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 194 deletions.
181 changes: 165 additions & 16 deletions src/Omniphx/Forrest/Client.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
<?php namespace Omniphx\Forrest;

use Omniphx\Forrest\Resource;
use Omniphx\Forrest\Exceptions\MissingTokenException;
abstract class Client {

abstract class Client extends Resource {
/**
* HTTP request client
* @var Client
*/
protected $client;

/**
* Config options
* @var array
*/
protected $settings;

/**
* Storage handler
* @var storage
*/
protected $storage;

/**
* Reqeust headers
* @var Array
*/
private $headers;

/**
* GET method call using any custom path
Expand Down Expand Up @@ -414,6 +435,16 @@ public function custom($customURI, $options = [])
return $this->request($url, $options);
}

/**
* Public accessor to the Guzzle Client Object
*
* @return GuzzleHttp\ClientInterface
*/
public function getClient()
{
return $this->client;
}

/**
* Returns any resource that is available to the authenticated
* user. Reference Force.com's REST API guide to read about more
Expand Down Expand Up @@ -497,8 +528,8 @@ protected function storeVersion()
}
else {
$versions = $this->versions(['format'=>'json']);
$lastestVersion = end($versions);
$this->storage->put('version', $lastestVersion);
$latestVersion = end($versions);
$this->storage->put('version', $latestVersion);
}
}

Expand All @@ -523,21 +554,139 @@ protected function storeResources()
}

/**
* Encodes array of key values into encoded url.
* @param [type] $parameters [description]
* @return [type] [description]
* Method returns the response for the requested resource
* @param string $pURI
* @param array $pOptions
* @return mixed
*/
private function encodeParameters($parameters){
$url = '';
protected function requestResource($pURL, array $pOptions)
{
$options = array_replace_recursive($this->settings['defaults'], $pOptions);

foreach ($parameters as $key => $value) {
$url .= '&';
$url .= $key;
$url .= '=';
$url .= $value;
$format = $options['format'];
$method = $options['method'];

$this->setHeaders($options);

$parameters['headers'] = $this->headers;

if (isset($options['body'])) {
$parameters['body'] = $this->formatBody($options);
}

return $url;
$request = $this->client->createRequest($method,$pURL,$parameters);

try {
$response = $this->client->send($request);

$this->event->fire('forrest.response', array($request));

return $this->responseFormat($response,$format);

} catch(RequestException $e) {
$this->assignExceptions($e);
}


}

/**
* Set the headers for the request
* @param array $options
* @return array $headers
*/
private function setHeaders(array $options)
{
$format = $options['format'];

$authToken = $this->storage->getToken();

$accessToken = $authToken['access_token'];
$tokenType = $authToken['token_type'];

$this->headers['Authorization'] = "$tokenType $accessToken";

$this->setRequestFormat($options['format']);
$this->setCompression($options);
}

/**
* Format the body for the request
* @param array $options
* @return array $body
*/
private function formatBody(array $options)
{
$format = $options['format'];
$data = $options['body'];

if ($format == 'json') {
$body = json_encode($data);
}
else if($format == 'xml') {
$body = urlencode($data);
}

return $body;
}

//Need to think through this for it to work
private function setRequestFormat($format)
{
if ($format == 'json') {
$this->headers['Accept'] = 'application/json';
$this->headers['Content-Type'] = 'application/json';
}
else if ($format == 'xml') {
$this->headers['Accept'] = 'application/xml';
$this->headers['Content-Type'] = 'application/xml';
}
else if ($format == 'urlencoded') {
$this->headers['Accept'] = 'application/x-www-form-urlencoded';
$this->headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
}

private function setCompression($options)
{
if ($options['compression'] == true) {
$this->headers['Accept-Encoding'] = $options['compressionType'];
$this->headers['Content-Encoding'] = $options['compressionType'];
}
}

/**
* Returns the response in the configured format
* @param Response $response
* @param string $format
* @return mixed $response
*/
private function responseFormat($response,$format)
{
if ($format == 'json') {
return $response->json();
}
else if ($format == 'xml') {
return $response->xml();
}

return $response;
}

/**
* Method will elaborate on RequestException
* @param GuzzleHttp\Exception\ClientException $e
* @return mixed
*/
private function assignExceptions($e)
{
if ($e->hasResponse() && $e->getResponse()->getStatusCode() == '401') {
throw new TokenExpiredException(sprintf("Salesforce token has expired"));
} else if($e->hasResponse()){
throw new SalesforceException(sprintf("Salesforce response error: %s",$e->getResponse()));
} else {
throw new SalesforceException(sprintf("Invalid request: %s",$e->getRequest()));
}
}

}
178 changes: 0 additions & 178 deletions src/Omniphx/Forrest/Resource.php

This file was deleted.

0 comments on commit e9ea262

Please sign in to comment.