Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kuturune
committed
Jan 1, 2011
1 parent
ddb90de
commit 945ff30
Showing
1 changed file
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
<?php | ||
|
||
class BSD_API { | ||
|
||
var $api_id; | ||
var $api_secret; | ||
|
||
var $http_request_base; | ||
var $http_request_options; | ||
|
||
var $deferred_result_call_interval; | ||
var $deferred_result_call_max_attempts; | ||
|
||
var $output_format; | ||
|
||
const HTTP_CODE_OK = 200; | ||
const HTTP_CODE_DEFERRED_RESULT = 202; | ||
const HTTP_CODE_DEFERRED_RESULT_COMPILING = 503; | ||
|
||
const OUTPUT_40CHARHEX = 1; | ||
const OUTPUT_BASE64 = 2; | ||
|
||
const API_VER = 1; | ||
|
||
public function __construct(){ | ||
// store vars requied by BSD API | ||
$this->http_request_base = variable_get('bsdapi_url', ''); //'http://socialcontxt.bsdtoolsdemo.com/page/api/'; | ||
$this->api_id = variable_get('bsdapi_key', ''); //'activ8' | ||
$this->api_secret = variable_get('bsdapi_secret', ''); //'98d5dcb47e25a9af48da0234920f146fe1d2825c' | ||
|
||
|
||
// set deferred result defaults | ||
|
||
|
||
// *these are sample values and may be modified as you see fit* | ||
$this->deferred_result_call_interval = 30; // in seconds | ||
$this->deferred_result_call_max_attempts = 20; | ||
|
||
$this->output_format = self::OUTPUT_40CHARHEX; | ||
|
||
$this->http_request_options = array( | ||
'timeout' => 10, | ||
'readTimeout' => array(10, 0), | ||
'allowRedirects' => true, | ||
'maxRedirects' => 3 | ||
); | ||
} | ||
|
||
public function callApi($url, $query_params = array()){ | ||
// prepend URL with base path for the API | ||
$url = $this->http_request_base . $url; | ||
|
||
// add api_id, timestamp, and version number to query string | ||
$query_params['api_id'] = $this->api_id; | ||
if (! array_key_exists('api_ts', $query_params)) { | ||
$query_params['api_ts'] = time(); | ||
} | ||
$query_params['api_ver'] = self::API_VER; | ||
|
||
// add api_mac to query string after using existing query and request url to build | ||
// the api_mac | ||
$query_params['api_mac'] = $this->_buildApiMac($url, $query_params); | ||
|
||
$url .= '?'; | ||
$d = array(); | ||
foreach ( $query_params as $key => $value ) { | ||
$d[] = $key . '=' . $value; | ||
} | ||
$url .= implode('&', $d); | ||
|
||
/* | ||
$api_ver = 1; | ||
$api_id = "activ8"; | ||
$api_ts = time(); | ||
$api_call = "/page/api/cons/get_constituents_by_id"; | ||
$api_params = "cons_ids=1&api_ver=" . $api_ver . "&api_id=" . $api_id . "&api_ts=" . $api_ts; | ||
$api_secret = "98d5dcb47e25a9af48da0234920f146fe1d2825c"; | ||
$signing_string = $api_id . "\n" . $api_ts . "\n" . $api_call . "\n" . $api_params; | ||
$api_mac = hash_hmac('sha1', $signing_string, $api_secret); | ||
$url = "http://socialcontxt.bsdtoolsdemo.com" . $api_call . "?" . $api_params . "&api_mac=" . $api_mac; | ||
*/ | ||
|
||
$result = drupal_http_request($url, $this->http_request_options); | ||
dpm($result); | ||
return; | ||
} | ||
|
||
/** | ||
* Calculate hash | ||
* | ||
* @param $url | ||
* @param $query_params | ||
* @return unknown_type | ||
*/ | ||
private function _buildApiMac($url, $query_params){ | ||
// break URL into parts to get the path | ||
// i.e. "/page/api/cons/get_constituents_by_id" | ||
$url_parts = parse_url($url); | ||
|
||
// build query string from given parameters | ||
// $query_string = urldecode(http_build_query($query_params)); // does NOT work | ||
// Instead .. | ||
$d = array(); | ||
foreach ( $query_params as $key => $value ) { | ||
$d[] = $key . '=' . $value; | ||
} | ||
|
||
$query_string = implode('&', $d); | ||
|
||
// combine strings to build the signing string | ||
$signing_string = $query_params['api_id'] . "\n" . $query_params['api_ts'] . "\n" . $url_parts['path'] . "\n" . $query_string; | ||
|
||
// calculate hash | ||
$hash = hash_hmac('sha1', $signing_string, $this->api_secret); | ||
|
||
if ($this->output_format == self::OUTPUT_BASE64) { | ||
$hash = $this->_hex2b64($hash); | ||
} | ||
// else, output_format is the default self::OUTPUT_40CHARHEX | ||
return $hash; | ||
|
||
} | ||
|
||
private function _deferredResult($deferred_id){ | ||
$attempt = 0; | ||
|
||
// loop until result is ready or until we give up | ||
do { | ||
// delay between calls (in seconds) | ||
sleep($this->deferred_result_call_interval); | ||
|
||
// check to see if result is ready | ||
$req = $this->callApi('get_deferred_results', array( | ||
'deferred_id' => $deferred_id | ||
)); | ||
|
||
// increment attempts counter | ||
$attempt ++; | ||
} while ( $req->getResponseCode() == self::HTTP_CODE_DEFERRED_RESULT_COMPILING && $attempt < $this->deferred_result_call_max_attempts ); | ||
|
||
// if the response code isn't HTTP_CODE_OK then we didn't get the result we wanted | ||
if ($req->getResponseCode() != self::HTTP_CODE_OK) { | ||
// did we go over our "max attempts"? | ||
if ($iteration >= $this->deferred_result_call_max_attempts) { | ||
throw new Exception('Could not retrieve deferred result. Max attempts reached.', 1); | ||
} | ||
// we must have received an unexpected HTTP code | ||
else { | ||
throw new Exception('Could not retrieve deferred result. HTTP Code ' . $req->getResponseCode() . ' was returned, with the following message: ' . $req->getResponseBody(), 2); | ||
} | ||
} | ||
|
||
// return request result | ||
return $req; | ||
} | ||
|
||
protected function _hex2b64($str){ | ||
$raw = ''; | ||
for($i = 0; $i < strlen($str); $i += 2) { | ||
$raw .= chr(hexdec(substr($str, $i, 2))); | ||
} | ||
return base64_encode($raw); | ||
} | ||
|
||
} |