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:
17:6c774354b599
Parent:
16:3d160f224084
Child:
18:7dc9b949bbc3
--- a/SalesforceInterface.cpp	Tue Sep 23 20:26:24 2014 +0000
+++ b/SalesforceInterface.cpp	Tue Sep 23 22:01:43 2014 +0000
@@ -33,6 +33,9 @@
  // salesforce URL API version token
  #define SF_URL_API_VER_TOKEN   "{version}"
  
+ // HTTP response code to give for errored out conditions
+ #define SF_GEN_ERR_HTTP_CODE   500
+ 
  // include class definition
  #include "SalesforceInterface.h"
  
@@ -348,6 +351,7 @@
          // invalid or NULL parameters
          this->logger()->log("createRecord: error - invalid or NULL parameters...");
      }
+     this->m_http_response_code = SF_GEN_ERR_HTTP_CODE;
      return NULL;
  }
 
@@ -393,6 +397,7 @@
          // invalid or NULL parameters
          this->logger()->log("readRecord: error - invalid or NULL parameters...");
      }
+     this->m_http_response_code = SF_GEN_ERR_HTTP_CODE;
      return NULL;
  }
 
@@ -430,7 +435,7 @@
                 DEBUG("updateRecord: http status=%d",this->httpResponseCode());
                 
                 // return our status
-                if (this->httpResponseCode() == 204) return true;
+                if (this->httpResponseCodeInRange(200)) return true;
                 return false;
             }
          }
@@ -443,6 +448,7 @@
          // invalid or NULL parameters
          this->logger()->log("updateRecord: error - invalid or NULL parameters...");
      }
+     this->m_http_response_code = SF_GEN_ERR_HTTP_CODE;
      return false;  
  }
  
@@ -486,7 +492,7 @@
                 DEBUG("upsertRecord: http status=%d",this->httpResponseCode());
                 
                 // return our status
-                if (this->httpResponseCode() == 204) return true;
+                if (this->httpResponseCodeInRange(200)) return true;
                 return false;
             }
          }
@@ -499,6 +505,7 @@
          // invalid or NULL parameters
          this->logger()->log("upsertRecord: error - invalid or NULL parameters...");
      }
+     this->m_http_response_code = SF_GEN_ERR_HTTP_CODE;
      return false;  
  }
   
@@ -533,7 +540,7 @@
                 DEBUG("deleteRecord: http status=%d",this->httpResponseCode());
                 
                 // return our status
-                if (this->httpResponseCode() == 204) return true;
+                if (this->httpResponseCodeInRange(200)) return true;
                 return false;
             }
          }
@@ -546,6 +553,7 @@
          // invalid or NULL parameters
          this->logger()->log("deleteRecord: error - invalid or NULL parameters...");
      }
+     this->m_http_response_code = SF_GEN_ERR_HTTP_CODE;
      return false;
  }
  
@@ -672,7 +680,7 @@
      // process any return results that we have
      if (this->httpStatus() == HTTP_OK || this->httpStatus() == HTTP_REDIRECT) {
          // do we have any redirections?
-         if (this->httpResponseCode() == 302 /* REDIRECT */ && strlen(this->m_http_redirection_url) > 0) {
+         if (this->httpResponseCodeInRange(300) /* REDIRECT */ && strlen(this->m_http_redirection_url) > 0) {
             // we have a redirect - so reset the output buffer
             memset(output_buffer,0,output_buffer_length);
             
@@ -684,7 +692,7 @@
             DEBUG("invoke: redirecting to: %s",redirect_url);  
             return this->invoke((const char *)redirect_url,input_type,input_data,input_data_len,output_buffer,output_buffer_length,verb);
          }
-         else if (this->httpResponseCode() == 302 /* REDIRECT */) {
+         else if (this->httpResponseCodeInRange(300) /* REDIRECT */) {
             // error - got a redirect but have no URL
             this->logger()->log("invoke error: received redirect but no URL...");
             this->m_http_status = HTTP_ERROR;
@@ -770,4 +778,12 @@
             line.insert( pos, newString );
         }
     }
+ }
+
+ // validate that a given HTTP result code is in the "n" range 
+ bool SalesforceInterface::httpResponseCodeInRange(int n) {
+     int http_response = this->httpResponseCode();
+     int diff = http_response - n;
+     if (diff >= 0 && diff < 100) return true;
+     return false;
  }
\ No newline at end of file