Skip to content

Commit

Permalink
support for post data + add constituent example
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferdi committed Jan 3, 2011
1 parent 66009b8 commit 3f5363b
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 46 deletions.
71 changes: 36 additions & 35 deletions bsdapi.class.inc
Expand Up @@ -39,11 +39,10 @@ class BsdApi {
'timeout' => 10,
'readTimeout' => array(10, 0),
'allowRedirects' => true,
'maxRedirects' => 3
);
}

public function request($url, $query_params = array()){
public function request($url, $query_params = array(), $data = NULL){
// prepend URL with base path for the API
$url = $this->http_request_base . $url;

Expand All @@ -61,47 +60,16 @@ class BsdApi {
$url .= '?';
$url .= $this->_attrs_to_query($query_params);

$result = drupal_http_request($url, $this->http_request_options);
$result = drupal_http_request($url = $url, $headers = $this->http_request_options, $method = 'POST', $data = $data, $retry = 3);

// is this a deferred result?
if ($result->code == self::HTTP_CODE_DEFERRED_RESULT) {
$result = $this->_deferredResult($result->data);
}
//dpm($result);
return $result->data;
return $result;
}

/**
* 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 ..
$query_string = $this -> _attrs_to_query($query_params);

// 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;

Expand Down Expand Up @@ -135,6 +103,39 @@ class BsdApi {
return $req;
}

/**
* 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 ..
$query_string = $this -> _attrs_to_query($query_params);

// 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;

}



protected function _hex2b64($str){
$raw = '';
for($i = 0; $i < strlen($str); $i += 2) {
Expand Down
160 changes: 149 additions & 11 deletions modules/bsdapi_example.module
Expand Up @@ -13,22 +13,36 @@ function bsdapi_example_menu() {

'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);

$items['bsdapi_examples/get_constituents_by_id'] = array(
'title' => 'Blue State Digital API examples',
'page callback' => 'bsdapi_examples_get_constituents_by_id',
$items['bsdapi_examples/set_constituent_data'] = array(
'title' => 'Blue State Digital API examples - set_constituent_data',
'page callback' => 'drupal_get_form',
'page arguments' => array('bsdapi_examples_set_constituent_data_form'),

'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);

$items['bsdapi_examples/get_constituents_by_id/%'] = array(
'title' => 'Blue State Digital API examples - bsdapi_examples/get_constituents_by_id',
'page callback' => 'bsdapi_examples_get_constituents_by_id',
'page arguments' => array(2),

'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);

$items['bsdapi_examples/get_constituents'] = array(
'title' => 'Blue State Digital API examples',
'title' => 'Blue State Digital API examples - bsdapi_examples/get_constituents',
'page callback' => 'bsdapi_examples_get_constituents',

'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK ,
);

return $items;
Expand All @@ -38,8 +52,8 @@ function bsdapi_examples(){
$links = array();


// Three ways to do our animal picking wizard.
$links[] = l('get_constituents_by_id', 'bsdapi_examples/get_constituents_by_id');
$links[] = l('set_constituent_data', 'bsdapi_examples/set_constituent_data');
$links[] = l('get_constituents_by_id', 'bsdapi_examples/get_constituents_by_id/1');
$links[] = l('get_constituents', 'bsdapi_examples/get_constituents');


Expand All @@ -48,20 +62,144 @@ function bsdapi_examples(){
return $output;
}

function bsdapi_examples_get_constituents_by_id(){



/**
* example of cons/get_constituents_by_id
*/
function bsdapi_examples_get_constituents_by_id($id){
module_load_include('inc', 'bsdapi', 'bsdapi.class');

$obj = new BsdApi();
$obj -> request('cons/get_constituents_by_id', array('cons_ids'=> '1'));
$result = $obj -> request('cons/get_constituents_by_id', array('cons_ids'=> $id, 'bundles' => 'cons_addr'));

return 'done';
return bsdapi_example_const_table($result, 'filter: id = 1');
}

/**
* example of cons/get_constituents
*/
function bsdapi_examples_get_constituents(){
module_load_include('inc', 'bsdapi', 'bsdapi.class');

$obj = new BsdApi();
$obj -> request('cons/get_constituents', array('filter' => 'state_cd=(NY,MA,IA,NV,NH,SC)'));
$result = $obj -> request('cons/get_constituents', array('filter' => 'state_cd=(NY,MA,IA,NV,NH,SC)', 'bundles' => 'cons_addr'));
//print_r($result -> data);
return bsdapi_example_const_table($result, 'filter = state_cd=(NY,MA,IA,NV,NH,SC)');

}

function bsdapi_examples_set_constituent_data_form() {


$form['firstname'] = array(
'#title' => t('the first name of the constituent'),
'#type' => 'textfield',
'#required' => TRUE,
);

$form['lastname'] = array(
'#title' => t('the last name of the constituent'),
'#type' => 'textfield',
'#required' => TRUE,
);

$form['email'] = array(
'#title' => t('the e-mail of the constituent'),
'#type' => 'textfield',
'#required' => TRUE,
);

$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);

return $form;
}


function bsdapi_examples_set_constituent_data_form_submit($form, &$form_state){
$firstname = $form_state['values']['firstname'];
$lastname = $form_state['values']['lastname'];
$email = $form_state['values']['email'];

$post = '

<?xml version="1.0" encoding="utf-8"?>
<api>
<cons>
<firstname>' . $firstname . '</firstname>
<lastname>' . $lastname . '</lastname>
<is_banned>0</is_banned>
<create_dt>'. time() . '</create_dt>

<cons_email>
<email>' . $email . '</email>
<is_subscribed>0</is_subscribed>
<is_primary>1</is_primary>
</cons_email>

</cons>
</api>
';

module_load_include('inc', 'bsdapi', 'bsdapi.class');

$obj = new BsdApi();
$result = $obj -> request('cons/set_constituent_data', array(), $post);

if ($result -> code == '200'){
$xml = simplexml_load_string($result->data);
$id = $xml -> cons['id'];
$l = l('here', 'bsdapi_examples/get_constituents_by_id/' . $id);
$message = 'The constituent added successfully. Check it out ' . $l;

drupal_set_message($message);
}
else {
drupal_set_message('Sorry, there was an error adding the constituent, error code -> ' . $result -> code);
}

}


/**
* outputs the result of a "cons" request as a table
*/
function bsdapi_example_const_table($result, $caption = NULL){
$xml = simplexml_load_string($result->data);
//print_r($xml);
$header = array(
'id',
'modified date',
'First name',
'Last Name',
'has account',
'is banned',
'addr1',
'city',
'state_cd',
'zip',
'country',
);
$rows = array();
foreach ( $xml->cons as $record ) {
$rows[] = array(
$record['id'],
format_date($record['modified_dt']),
$record->firstname,
$record->lastname,
$record->is_banned,
$record->has_account,
$record -> cons_addr -> addr1,
$record -> cons_addr -> city,
$record -> cons_addr -> state_cd,
$record -> cons_addr -> zip,
$record -> cons_addr -> country,
);
}

return 'done';
return theme_table($header, $rows, $attributes = array(), $caption = $caption);
}

0 comments on commit 3f5363b

Please sign in to comment.