commit 5de63a22bf54d47b5b922159444e2895ed62c9f4 Author: Marek Isalski Date: Sat Mar 12 14:15:58 2016 +0100 initial version of fulcrm_apiv2 diff --git a/fulcrm_apiv2.admin.inc b/fulcrm_apiv2.admin.inc new file mode 100644 index 0000000..e7de9c9 --- /dev/null +++ b/fulcrm_apiv2.admin.inc @@ -0,0 +1,46 @@ + 'item', + '#title' => 'fulcrm APIv2 Settings', + '#description' => '', + ); + + $form[ 'api' ] = array( '#type' => 'fieldset', + '#title' => 'fulcrm.org APIv2 Credentials', + ); + + $form[ 'api' ][ 'apiuser' ] = array( '#type' => 'textfield', + '#title' => 'API Username', + '#description' => 'fulcrm.org APIv2 username', + '#default_value' => variable_get( 'fulcrm_apiv2_user', '' ), + ); + + $form[ 'api' ][ 'apikey' ] = array( '#type' => 'textfield', + '#title' => 'API Key', + '#description' => 'fulcrm.org APIv2 key', + '#default_value' => variable_get( 'fulcrm_apiv2_key', '' ), + ); + $form[ 'api' ][ 'clientid' ] = array( '#type' => 'textfield', + '#title' => 'Client ID', + '#description' => 'fulcrm.org APIv2 client ID', + '#default_value' => variable_get( 'fulcrm_apiv2_client_id', '' ), + ); + + $form[ 'actions' ] = array( '#type' => 'actions' ); + $form[ 'actions' ][ 'save' ] = array( '#type' => 'submit', + '#value' => t('Save'), + '#submit' => array( 'fulcrm_apiv2_admin_form_submit' ), + ); + return $form; +} + +function fulcrm_apiv2_admin() { + return drupal_get_form( 'fulcrm_apiv2_admin_form' ); +} diff --git a/fulcrm_apiv2.info b/fulcrm_apiv2.info new file mode 100644 index 0000000..30d5c6d --- /dev/null +++ b/fulcrm_apiv2.info @@ -0,0 +1,7 @@ +name = fulcrm APIv2 +description = fulcrm API version 2 +core = 7.x +package = fulcrm +;files[] = fulcrm_apiv2.admin.inc +configure = admin/config/services/fulcrm/apiv2 +dependencies[] = fulcrm_uuid diff --git a/fulcrm_apiv2.install b/fulcrm_apiv2.install new file mode 100644 index 0000000..4ab98c9 --- /dev/null +++ b/fulcrm_apiv2.install @@ -0,0 +1,11 @@ + array( 'title' => t('Administer fulcrm APIv2'), + 'description' => t('Perform main installation/administration tasks for fulcrm APIv2.'), + ), + ); +} + +function fulcrm_apiv2_menu() { + /* + $items[ 'admin/config/services/fulcrm' ] = array( 'page callback' => 'system_admin_menu_block_page', + 'file' => 'system.admin.inc', + 'file path' => drupal_get_path('module', 'system'), + 'title' => 'fulcrm', + 'description' => 'Configuration integration with fulcrm.org.', + 'position' => 'right', + 'access callback' => 'user_access', + 'access arguments' => array('access administration pages'), + ); + */ + $items[ 'admin/config/services/fulcrm/apiv2' ] = array( 'page callback' => 'fulcrm_apiv2_admin', + 'file' => 'fulcrm_apiv2.admin.inc', + 'title' => 'fulcrm APIv2', + 'description' => 'Credentials and settings for interacting with fulcrm.org APIv2.', + 'access callback' => 'user_access', + 'access arguments' => array('administer fulcrm APIv2'), + ); + + return $items; +} + +function fulcrm_apiv2_http( $method, $object, $data = null ) { + $curl = curl_init( 'https://fulcrm.org' . $object ); + curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, $method ); + + $request_headers = array( 'Authorization: ApiKey ' . variable_get( 'fulcrm_apiv2_user' ) . ':' . variable_get( 'fulcrm_apiv2_key' ), + ); + if ( $data !== null ) { + if ( is_array( $data ) ) { + $data = json_encode( $data ); + $request_headers[] = 'Content-Type: application/json'; + } + curl_setopt( $curl, CURLOPT_POSTFIELDS, $data ); + $request_headers[] = 'Content-Length: ' . strlen( $data ); + } + + $request_headers[] = 'X-fulcrm-Client-ID: ' . variable_get( 'fulcrm_apiv2_client_id', $GLOBALS[ 'base_url' ] ); + + curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); + curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true ); + curl_setopt( $curl, CURLOPT_HEADER, true ); + curl_setopt( $curl, CURLOPT_HTTPHEADER, $request_headers ); + curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true ); + curl_setopt( $curl, CURLOPT_TCP_NODELAY, true ); + + $result = curl_exec( $curl ); + $response = curl_getinfo( $curl ); + $header_size = curl_getinfo( $curl, CURLINFO_HEADER_SIZE ); + $header = substr( $result, 0, $header_size ); + $body = substr( $result, $header_size ); + $headers = array(); + foreach ( preg_split( '/[\r\n]+/', $header ) as $headerline ) { + if ( preg_match( '/([-A-Za-z0-9_]+):\s*(.*)/', $headerline, $matches ) ) + $headers[ $matches[ 1 ] ] = $matches[ 2 ]; + } + + $r = array( 'headers' => $headers, + 'body' => $body, + 'code' => $response[ 'http_code' ], + ); + + if ( $response[ 'http_code' ] == 200 ) { + $r[ 'success' ] = true; // GET + } else if ( $response[ 'http_code' ] == 201 ) { + $r[ 'success' ] = true; // POST + } else if ( $response[ 'http_code' ] == 202 ) { + $r[ 'success' ] = true; // PATCH + } else if ( $response[ 'http_code' ] == 204 ) { + $r[ 'success' ] = true; // DELETE + } else { + $r[ 'success' ] = false; + } + + return $r; +} + +function fulcrm_apiv2_GET( $object ) { + return fulcrm_apiv2_http( 'GET', $object ); +} + +function fulcrm_apiv2_POST( $object, $data ) { + return fulcrm_apiv2_http( 'POST', $object, $data ); +} + +function fulcrm_apiv2_PUT( $object, $data ) { + return fulcrm_apiv2_http( 'PUT', $object, $data ); +} + +function fulcrm_apiv2_PATCH( $object, $data ) { + return fulcrm_apiv2_http( 'PATCH', $object, $data ); +} + +function fulcrm_apiv2_DELETE( $object ) { + return fulcrm_apiv2_http( 'DELETE', $object ); +}