From 3f5363b349df709f78b08a1c6806ac8558a4feaf Mon Sep 17 00:00:00 2001 From: Ferdi Date: Sun, 2 Jan 2011 18:25:30 -0600 Subject: [PATCH] support for post data + add constituent example --- bsdapi.class.inc | 71 +++++++-------- modules/bsdapi_example.module | 160 +++++++++++++++++++++++++++++++--- 2 files changed, 185 insertions(+), 46 deletions(-) diff --git a/bsdapi.class.inc b/bsdapi.class.inc index c98e492..634f5f6 100644 --- a/bsdapi.class.inc +++ b/bsdapi.class.inc @@ -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; @@ -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; @@ -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) { diff --git a/modules/bsdapi_example.module b/modules/bsdapi_example.module index 0d0a5c2..c5dc9a7 100644 --- a/modules/bsdapi_example.module +++ b/modules/bsdapi_example.module @@ -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; @@ -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'); @@ -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 = ' + + + + + ' . $firstname . ' + ' . $lastname . ' + 0 + '. time() . ' + + + ' . $email . ' + 0 + 1 + + + + + '; + + 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); } \ No newline at end of file