Salesforce.com interface to directly access Salesforce.com

Dependencies:   HTTPClient-SSL MbedJSONValue

Dependents:   df-2014-salesforce-hrm-k64f

Fork of SalesforceInterface by Doug Anson

Revision:
14:3c8d11b48814
Parent:
10:845ea6d00b65
Child:
15:89044c68ad36
--- a/SalesforceInterface.cpp	Tue Sep 23 16:56:25 2014 +0000
+++ b/SalesforceInterface.cpp	Tue Sep 23 17:31:36 2014 +0000
@@ -54,7 +54,7 @@
      RESET_BUFFER(this->m_http_redirection_url);
      memset(this->m_salesforce_api,0,SALESFORCE_API_VERSION_LENGTH);
      strcpy(this->m_salesforce_api,SALESFORCE_API_VERSION);
-     this->resetSalesforceID();
+     this->resetSalesforceToken();
  }
  
  // destructor
@@ -139,18 +139,18 @@
  }
  
  // reset our salesforce ID and OAUTH tokens
- void SalesforceInterface::resetSalesforceID() {
+ void SalesforceInterface::resetSalesforceToken() {
      this->resetOauthToken();
      RESET_BUFFER(this->m_salesforce_id);
  }
 
  // do we have a valid salesforce.com ID?
- bool SalesforceInterface::haveSalesforceID(bool fetch) {
+ bool SalesforceInterface::haveSalesforceToken(bool fetch) {
      if (this->m_salesforce_id != NULL && strlen(this->m_salesforce_id) > 0) return true;
      if (fetch) {
-        this->logger()->log("No Salesforce ID found... fetching...");
-        this->getSalesforceID();
-        return this->haveSalesforceID(false);
+        this->logger()->log("No Salesforce Token found... fetching...");
+        this->getSalesforceToken();
+        return this->haveSalesforceToken(false);
      }
      return false;
  }
@@ -232,7 +232,7 @@
  }
  
  // Salesforce.com: Get our ID
- char *SalesforceInterface::getSalesforceID(bool fetch) {
+ char *SalesforceInterface::getSalesforceToken(bool fetch) {
     // proceed only if we have a valid OAUTH Token
     if (this->validOauthToken(fetch) == true) {
         // pull the ID from salesforce
@@ -240,12 +240,12 @@
         char *id = this->invoke(this->oauth()->id.c_str(),this->m_salesforce_id,MAX_BUFFER_LENGTH);
         
         // log any error status and return what we have...
-        if (this->httpStatus() != HTTP_OK) this->logger()->log("Unable to get Salesforce ID: status=%d httpCode=%d",this->httpStatus(),this->httpResponseCode());
+        if (this->httpStatus() != HTTP_OK) this->logger()->log("Unable to get Salesforce Token: status=%d httpCode=%d",this->httpStatus(),this->httpResponseCode());
         return id;
     }
     else {
         // unable to get ID - no OAUTH token
-        this->logger()->log("Unable to get Salesforce ID: no valid OAUTH token.");
+        this->logger()->log("Unable to get Salesforce Token: no valid OAUTH token.");
     }
     return NULL;
  }
@@ -253,7 +253,7 @@
  // QUERY: Salesforce.com
  char *SalesforceInterface::query(char *query_str,char *output_buffer,int output_buffer_length) {
      // first we have to ensure that we have valid salesforce ID
-     if (this->haveSalesforceID()) {        
+     if (this->haveSalesforceToken()) {        
         // get the query url
         ALLOC_BUFFER(url);
         char *sf_url = this->getSalesforceURL("query",url,MAX_BUFFER_LENGTH);
@@ -288,35 +288,35 @@
      return NULL;
  }
  
- // CREATE: a field in Salesforce.com
- MbedJSONValue SalesforceInterface::createField(char *object_name,MbedJSONValue &field) { 
+ // CREATE: a record in Salesforce.com
+ MbedJSONValue SalesforceInterface::createRecord(char *object_name,MbedJSONValue &record) { 
     ALLOC_BUFFER(output_buffer);
-    char *reply = this->createField(object_name,(char *)field.serialize().c_str(),output_buffer,MAX_BUFFER_LENGTH);
+    char *reply = this->createRecord(object_name,(char *)record.serialize().c_str(),output_buffer,MAX_BUFFER_LENGTH);
     MbedJSONValue response;
     if (reply != NULL && strlen(reply) > 0) parse(response,reply);
     return response;
  }
 
- // READ: a specific field in Salesforce.com
- MbedJSONValue SalesforceInterface::readField(char *object_name,char *field_id) {
+ // READ: a specific record in Salesforce.com
+ MbedJSONValue SalesforceInterface::readRecord(char *object_name,char *record_id) {
     ALLOC_BUFFER(output_buffer);
-    char *reply = this->readField(object_name,field_id,output_buffer,MAX_BUFFER_LENGTH);
+    char *reply = this->readRecord(object_name,record_id,output_buffer,MAX_BUFFER_LENGTH);
     MbedJSONValue response;
     if (reply != NULL && strlen(reply) > 0) parse(response,reply);
     return response; 
  }
 
- // UPDATE: a specific field in Salesforce.com
- bool SalesforceInterface::updateField(char *object_name,char *field_id,MbedJSONValue &field) {
-    return this->updateField(object_name,field_id,(char *)field.serialize().c_str());
+ // UPDATE: a specific record in Salesforce.com
+ bool SalesforceInterface::updateRecord(char *object_name,char *record_id,MbedJSONValue &record) {
+    return this->updateRecord(object_name,record_id,(char *)record.serialize().c_str());
  }
  
- // CREATE: a field in Salesforce.com
- char *SalesforceInterface::createField(char *object_name,char *json_data,char *output_buffer,int output_buffer_length) {
+ // CREATE: a record in Salesforce.com
+ char *SalesforceInterface::createRecord(char *object_name,char *json_data,char *output_buffer,int output_buffer_length) {
      // parameter check
      if (object_name != NULL && strlen(object_name) > 0 && json_data != NULL && strlen(json_data) > 0 && output_buffer != NULL && output_buffer_length > 0) {
          // first we have to ensure that we have valid salesforce ID
-         if (this->haveSalesforceID()) {        
+         if (this->haveSalesforceToken()) {        
             // get the sobjects url
             ALLOC_BUFFER(url);
             char *sf_url = this->getSalesforceURL("sobjects",url,MAX_BUFFER_LENGTH);
@@ -324,11 +324,11 @@
                 // convert to string
                 string str_url(sf_url);
                          
-                // add object name that we want to create a field in
+                // add object name that we want to create a record in
                 str_url += object_name;
                 
                 // DEBUG
-                DEBUG("createField: URL: %s  DATA: %s",str_url.c_str(),json_data);
+                DEBUG("createRecord: URL: %s  DATA: %s",str_url.c_str(),json_data);
                 
                 // now invoke with POST with JSON data type
                 return this->invoke(str_url.c_str(),json_data,strlen(json_data)+1,output_buffer,output_buffer_length);
@@ -336,22 +336,22 @@
          }
          else {
              // dont have a valid salesforce ID
-             this->logger()->log("createField: error - no valid salesforced ID was found...");
+             this->logger()->log("createRecord: error - no valid salesforced ID was found...");
          }
      }
      else {
          // invalid or NULL parameters
-         this->logger()->log("createField: error - invalid or NULL parameters...");
+         this->logger()->log("createRecord: error - invalid or NULL parameters...");
      }
      return NULL;
  }
 
- // READ: a specific field in Salesforce.com
- char *SalesforceInterface::readField(char *object_name,char *field_id,char *output_buffer,int output_buffer_length) {
+ // READ: a specific record in Salesforce.com
+ char *SalesforceInterface::readRecord(char *object_name,char *record_id,char *output_buffer,int output_buffer_length) {
      // parameter check
-     if (object_name != NULL && strlen(object_name) > 0 && field_id != NULL && strlen(field_id) > 0 && output_buffer != NULL && output_buffer_length > 0) {
+     if (object_name != NULL && strlen(object_name) > 0 && record_id != NULL && strlen(record_id) > 0 && output_buffer != NULL && output_buffer_length > 0) {
          // first we have to ensure that we have valid salesforce ID
-         if (this->haveSalesforceID()) {        
+         if (this->haveSalesforceToken()) {        
             // get the sobjects url
             ALLOC_BUFFER(url);
             char *sf_url = this->getSalesforceURL("sobjects",url,MAX_BUFFER_LENGTH);
@@ -359,15 +359,15 @@
                 // convert to string
                 string str_url(sf_url);
                          
-                // add object name that we want to create a field in
+                // add object name that we want to create a record in
                 str_url += object_name;
                 
-                // add the field ID
+                // add the record ID
                 str_url += "/";
-                str_url += field_id;
+                str_url += record_id;
                 
                 // DEBUG
-                DEBUG("readField: URL: %s",str_url.c_str());
+                DEBUG("readRecord: URL: %s",str_url.c_str());
                 
                 // now invoke with GET with JSON data type
                 return this->invoke(str_url.c_str(),output_buffer,output_buffer_length);
@@ -375,22 +375,22 @@
          }
          else {
              // dont have a valid salesforce ID
-             this->logger()->log("readField: error - no valid salesforced ID was found...");
+             this->logger()->log("readRecord: error - no valid salesforced ID was found...");
          }
      }
      else {
          // invalid or NULL parameters
-         this->logger()->log("readField: error - invalid or NULL parameters...");
+         this->logger()->log("readRecord: error - invalid or NULL parameters...");
      }
      return NULL;
  }
 
- // UPDATE: a specific field in Salesforce.com
- bool SalesforceInterface::updateField(char *object_name,char *field_id,char *json_data) {  
+ // UPDATE: a specific record in Salesforce.com
+ bool SalesforceInterface::updateRecord(char *object_name,char *record_id,char *json_data) {  
      // parameter check
      if (object_name != NULL && strlen(object_name) > 0 && json_data != NULL && strlen(json_data) > 0) {
          // first we have to ensure that we have valid salesforce ID
-         if (this->haveSalesforceID()) {        
+         if (this->haveSalesforceToken()) {        
             // get the sobjects url
             ALLOC_BUFFER(url);
             char *sf_url = this->getSalesforceURL("sobjects",url,MAX_BUFFER_LENGTH);
@@ -398,25 +398,25 @@
                 // convert to string
                 string str_url(sf_url);
                          
-                // add object name that we want to create a field in
+                // add object name that we want to create a record in
                 str_url += object_name;
                 
-                // add the field ID
+                // add the record ID
                 str_url += "/";
-                str_url += field_id;
+                str_url += record_id;
                 
                 // HTTPClient does not support PATCH, so we have to use POST with a special added parameter
                 str_url += "?_HttpMethod=PATCH";
                 
                 // DEBUG
-                DEBUG("updateField: URL: %s DATA: %s",str_url.c_str(),json_data);
+                DEBUG("updateRecord: URL: %s DATA: %s",str_url.c_str(),json_data);
                 
                 // now invoke with POST with JSON data type
                 ALLOC_SML_BUFFER(output_buffer);
                 char *reply = this->invoke(str_url.c_str(),json_data,strlen(json_data)+1,output_buffer,MAX_SMALL_BUFFER_LENGTH);
                 
                 // DEBUG
-                DEBUG("updateField: http status=%d",this->httpResponseCode());
+                DEBUG("updateRecord: http status=%d",this->httpResponseCode());
                 
                 // return our status
                 if (this->httpResponseCode() == 204) return true;
@@ -425,22 +425,22 @@
          }
          else {
              // dont have a valid salesforce ID
-             this->logger()->log("updateField: error - no valid salesforced ID was found...");
+             this->logger()->log("updateRecord: error - no valid salesforced ID was found...");
          }
      }
      else {
          // invalid or NULL parameters
-         this->logger()->log("updateField: error - invalid or NULL parameters...");
+         this->logger()->log("updateRecord: error - invalid or NULL parameters...");
      }
      return false;  
  }
   
- // DELETE: a specific field in Salesforce.com
- bool SalesforceInterface::deleteField(char *object_name,char *field_id) {
+ // DELETE: a specific record in Salesforce.com
+ bool SalesforceInterface::deleteRecord(char *object_name,char *record_id) {
      // parameter check
-     if (object_name != NULL && strlen(object_name) > 0 && field_id != NULL && strlen(field_id) > 0) {
+     if (object_name != NULL && strlen(object_name) > 0 && record_id != NULL && strlen(record_id) > 0) {
          // first we have to ensure that we have valid salesforce ID
-         if (this->haveSalesforceID()) {        
+         if (this->haveSalesforceToken()) {        
             // get the sobjects url
             ALLOC_BUFFER(url);
             char *sf_url = this->getSalesforceURL("sobjects",url,MAX_BUFFER_LENGTH);
@@ -448,22 +448,22 @@
                 // convert to string
                 string str_url(sf_url);
                          
-                // add object name that we want to create a field in
+                // add object name that we want to create a record in
                 str_url += object_name;
                 
-                // add the field ID
+                // add the record ID
                 str_url += "/";
-                str_url += field_id;
+                str_url += record_id;
                 
                 // DEBUG
-                DEBUG("deleteField: URL: %s",str_url.c_str());
+                DEBUG("deleteRecord: URL: %s",str_url.c_str());
                 
                 // now invoke with DELETE 
                 ALLOC_SML_BUFFER(output_buffer);
                 char *reply = this->invoke(str_url.c_str(),output_buffer,MAX_SMALL_BUFFER_LENGTH,DELETE);
                 
                 // DEBUG
-                DEBUG("deleteField: http status=%d",this->httpResponseCode());
+                DEBUG("deleteRecord: http status=%d",this->httpResponseCode());
                 
                 // return our status
                 if (this->httpResponseCode() == 204) return true;
@@ -472,12 +472,12 @@
          }
          else {
              // dont have a valid salesforce ID
-             this->logger()->log("deleteField: error - no valid salesforced ID was found...");
+             this->logger()->log("deleteRecord: error - no valid salesforced ID was found...");
          }
      }
      else {
          // invalid or NULL parameters
-         this->logger()->log("deleteField: error - invalid or NULL parameters...");
+         this->logger()->log("deleteRecord: error - invalid or NULL parameters...");
      }
      return false;
  }