Salesforce.com interface to directly access Salesforce.com
Dependencies: HTTPClient-SSL MbedJSONValue
Dependents: df-2014-salesforce-hrm-k64f
Fork of SalesforceInterface by
Revision 17:6c774354b599, committed 2014-09-23
- Comitter:
- ansond
- Date:
- Tue Sep 23 22:01:43 2014 +0000
- Parent:
- 16:3d160f224084
- Child:
- 18:7dc9b949bbc3
- Commit message:
- updates for external id usage and sanity checking of http codes
Changed in this revision
SalesforceInterface.cpp | Show annotated file Show diff for this revision Revisions of this file |
SalesforceInterface.h | Show annotated file Show diff for this revision Revisions of this file |
--- 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
--- a/SalesforceInterface.h Tue Sep 23 20:26:24 2014 +0000 +++ b/SalesforceInterface.h Tue Sep 23 22:01:43 2014 +0000 @@ -202,12 +202,42 @@ logger->turnLEDGreen(); } + void Test_create_external_id(ErrorHandler *logger,SalesforceInterface *sf) { + logger->log("\r\n\r\nExecuting create(ExternalID)"); + logger->turnLEDPurple(); + + // create a new record + MbedJSONValue new_record; + new_record[external_id_field_name] = external_id_field_value; + + // DEBUG + logger->log("create(ExternalID): new record: %s",new_record.serialize().c_str()); + + // create... + MbedJSONValue response = sf->createRecord(object_name,new_record); + + // display the result + char *result = (char *)response.serialize().c_str(); + if (result != NULL && strlen(result) > 0 && strcmp(result,"null") != 0) { + // save off the token if we succeeded + logger->log("create(ExternalID): result: %s",result); + logger->log("create(ExternalID): http_code=%d",sf->httpResponseCode()); + RESET_SML_BUFFER(record_id); + strcpy(record_id,(char *)response["id"].get<std::string>().c_str()); + } + else { + // failure + logger->log("create(ExternalID): FAILED http_code=%d",sf->httpResponseCode()); + } + logger->turnLEDGreen(); + } + void Test_read_by_external_id_and_value(ErrorHandler *logger,SalesforceInterface *sf) { - logger->log("\r\n\r\nExecuting Read(externalID)..."); + logger->log("\r\n\r\nExecuting read(externalID)..."); logger->turnLEDPurple(); // DEBUG - logger->log("Read: reading: %s from %s with value %s",object_name,external_id_field_name,external_id_field_value); + logger->log("read(externalID): reading: %s from %s with value %s",object_name,external_id_field_name,external_id_field_value); // read (external ID)... MbedJSONValue response = sf->readRecord(object_name,external_id_field_name,external_id_field_value); @@ -216,12 +246,12 @@ char *result = (char *)response.serialize().c_str(); if (result != NULL && strlen(result) > 0 && strcmp(result,"null") != 0) { // save off the token if we succeeded - logger->log("Read(externalID): result: %s",result); - logger->log("Read(externalID): http_code=%d",sf->httpResponseCode()); + logger->log("read(externalID): result: %s",result); + logger->log("read(externalID): http_code=%d",sf->httpResponseCode()); } else { // failure - logger->log("Read(externalID): FAILED http_code=%d",sf->httpResponseCode()); + logger->log("read(externalID): FAILED http_code=%d",sf->httpResponseCode()); } logger->turnLEDGreen(); @@ -253,8 +283,8 @@ logger->turnLEDGreen(); } - void Test_upsert(ErrorHandler *logger,SalesforceInterface *sf) { - logger->log("\r\n\r\nExecuting upsert()"); + void Test_upsert_external_id(ErrorHandler *logger,SalesforceInterface *sf) { + logger->log("\r\n\r\nExecuting upsert(ExternalID)"); logger->turnLEDPurple(); // update am existing record - assume "name" is the proper key for the record you wish to update... @@ -262,7 +292,7 @@ changed_record["name"] = updated_account_name; // DEBUG - logger->log("Upsert: upserted record: %s",changed_record.serialize().c_str()); + logger->log("upsert(ExternalID): upserted record: %s",changed_record.serialize().c_str()); // Upsert... bool updated = sf->upsertRecord(object_name,external_id_field_name,external_id_field_value,changed_record); @@ -270,11 +300,11 @@ // display the result if (updated) { // SUCCESS - logger->log("Upsert: successful! http_code=%d",sf->httpResponseCode()); + logger->log("upsert(ExternalID): successful! http_code=%d",sf->httpResponseCode()); } else { // failure - logger->log("Upsert: FAILED http_code=%d",sf->httpResponseCode()); + logger->log("upsert(ExternalID): FAILED http_code=%d",sf->httpResponseCode()); } logger->turnLEDGreen(); } @@ -346,7 +376,7 @@ object_name = "Account"; // use the account object account_name = "ARM"; // add this record (name) updated_account_name = "ARM Holdings"; // update the existing record's name to this - external_id_field_name = "Device_c"; // External ID field name + external_id_field_name = "Device__c"; // External ID field name external_id_field_value = "ABC123"; // External ID field value RESET_SML_BUFFER(record_id); // buffer for the record's token @@ -365,8 +395,11 @@ // Perform a second Read to visually confirm the update above... Test_read(&logger,sf); + // Perform a Create (External ID) + Test_create_external_id(&logger,sf); + // Perform an Upsert - Test_update(&logger,sf); + Test_upsert_external_id(&logger,sf); // Perform a read of the external ID'ed specified by a given value Test_read_by_external_id_and_value(&logger,sf); @@ -410,7 +443,7 @@ Thread::wait(10*WAIT_TIME_MS); } } - + * @endcode * */ @@ -589,6 +622,9 @@ // needed to replace substrings within std::string void replace(string& line, string& oldString, string& newString); + + // validate that http status is in the "n" range + bool httpResponseCodeInRange(int n); }; #endif // _SALESFORCE_INTERFACE_H_ \ No newline at end of file