Salesforce.com interface to directly access Salesforce.com
Dependencies: HTTPClient-SSL MbedJSONValue
Dependents: df-2014-salesforce-hrm-k64f
Fork of SalesforceInterface by
Revision 14:3c8d11b48814, committed 2014-09-23
- Comitter:
- ansond
- Date:
- Tue Sep 23 17:31:36 2014 +0000
- Parent:
- 13:3088dd4b4bef
- Child:
- 15:89044c68ad36
- Commit message:
- updates per SF feedback
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 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; }
--- a/SalesforceInterface.h Tue Sep 23 16:56:25 2014 +0000 +++ b/SalesforceInterface.h Tue Sep 23 17:31:36 2014 +0000 @@ -112,16 +112,16 @@ * char *object_name = NULL; * char *account_name = NULL; * char *updated_account_name = NULL; - * DEFINE_SML_BUFFER(field_id); + * DEFINE_SML_BUFFER(record_id); * * // *************** Test Cases ************************ * - * void Test_getSalesforceID(ErrorHandler *logger,SalesforceInterface *sf) { - * logger->log("\r\n\r\nGetting Salesforce ID..."); + * void Test_getSalesforceToken(ErrorHandler *logger,SalesforceInterface *sf) { + * logger->log("\r\n\r\nGetting Salesforce Token..."); * logger->turnLEDPurple(); * * // get the salesforce ID - * char *id = sf->getSalesforceID(); + * char *id = sf->getSalesforceToken(); * if (id != NULL && strlen(id) > 0) * logger->log("Saleforce ID: %s",id); * else @@ -148,15 +148,15 @@ * logger->log("\r\n\r\nExecuting create()"); * logger->turnLEDPurple(); * - * // create a new field - * MbedJSONValue new_field; - * new_field["name"] = account_name; + * // create a new record + * MbedJSONValue new_record; + * new_record["name"] = account_name; * * // DEBUG - * logger->log("Create: new field: %s",new_field.serialize().c_str()); + * logger->log("Create: new record: %s",new_record.serialize().c_str()); * * // create... - * MbedJSONValue response = sf->createField(object_name,new_field); + * MbedJSONValue response = sf->createRecord(object_name,new_record); * * // display the result * char *result = (char *)response.serialize().c_str(); @@ -164,8 +164,8 @@ * // save off the ID if we succeeded * logger->log("Create: result: %s",result); * logger->log("Create: http_code=%d",sf->httpResponseCode()); - * RESET_SML_BUFFER(field_id); - * strcpy(field_id,(char *)response["id"].get<std::string>().c_str()); + * RESET_SML_BUFFER(record_id); + * strcpy(record_id,(char *)response["id"].get<std::string>().c_str()); * } * else { * // failure @@ -179,10 +179,10 @@ * logger->turnLEDPurple(); * * // DEBUG - * logger->log("Read: reading: %s from %s",field_id,object_name); + * logger->log("Read: reading: %s from %s",record_id,object_name); * * // read... - * MbedJSONValue response = sf->readField(object_name,field_id); + * MbedJSONValue response = sf->readRecord(object_name,record_id); * * // display the result * char *result = (char *)response.serialize().c_str(); @@ -203,15 +203,15 @@ * logger->log("\r\n\r\nExecuting update()"); * logger->turnLEDPurple(); * - * // update am existing field - assume "name" is the proper key for the field you wish to update... - * MbedJSONValue changed_field; - * changed_field["name"] = updated_account_name; + * // update am existing record - assume "name" is the proper key for the record you wish to update... + * MbedJSONValue changed_record; + * changed_record["name"] = updated_account_name; * * // DEBUG - * logger->log("Update: updated field: %s",changed_field.serialize().c_str()); + * logger->log("Update: updated record: %s",changed_record.serialize().c_str()); * * // update... - * bool updated = sf->updateField(object_name,field_id,changed_field); + * bool updated = sf->updateRecord(object_name,record_id,changed_record); * * // display the result * if (updated) { @@ -230,10 +230,10 @@ * logger->turnLEDPurple(); * * // DEBUG - * logger->log("Delete: deleting: %s from %s",field_id,object_name); + * logger->log("Delete: deleting: %s from %s",record_id,object_name); * * // delete... - * bool deleted = sf->deleteField(object_name,field_id); + * bool deleted = sf->deleteRecord(object_name,record_id); * * // display the result * if (deleted) { @@ -249,9 +249,9 @@ * } * * void Test_reset_auth(ErrorHandler *logger,SalesforceInterface *sf) { - * logger->log("\r\n\r\nForcing API to reset OAUTH token and Salesforce ID..."); + * logger->log("\r\n\r\nForcing API to reset OAUTH token and Salesforce Token..."); * logger->turnLEDPurple(); - * sf->resetSalesforceID(); + * sf->resetSalesforceToken(); * logger->turnLEDGreen(); * } * @@ -290,9 +290,9 @@ * * // configuration for the test cases * object_name = "Account"; // use the account object - * account_name = "ARM"; // add this field (name) - * updated_account_name = "ARM Holdings"; // update the existing field's name to this - * RESET_SML_BUFFER(field_id); // buffer for the field's ID + * account_name = "ARM"; // add this record (name) + * updated_account_name = "ARM Holdings"; // update the existing record's name to this + * RESET_SML_BUFFER(record_id); // buffer for the record's ID * * // Perform a Create * Test_create(&logger,sf); @@ -309,23 +309,23 @@ * // Perform a second Read to visually confirm the update above... * Test_read(&logger,sf); * - * // force the API to re-acquire the OAUTH token and Salesforce ID + * // force the API to re-acquire the OAUTH token and Salesforce Token * Test_reset_auth(&logger,sf); * - * // Perform a Read (should re-acquire the OAUTH token and Salesforce ID) + * // Perform a Read (should re-acquire the OAUTH token and Salesforce Token) * Test_read(&logger,sf); * * // Perform a Delete * Test_delete(&logger,sf); * - * // reset the field ID buffer - * // RESET_SML_BUFFER(field_id); + * // reset the record ID buffer + * // RESET_SML_BUFFER(record_id); * * // Perform a Read - should error out * Test_read(&logger,sf); * - * // reset the field ID buffer - * RESET_SML_BUFFER(field_id); + * // reset the record ID buffer + * RESET_SML_BUFFER(record_id); * * // *************** BEGIN TEST CASES ***************** * @@ -395,12 +395,12 @@ @param fetch boolean that will direct the interface to fetch the ID if not already done (default = true) @return our salesforce ID in JSON format or NULL if in error */ - char *getSalesforceID(bool fetch = true); + char *getSalesforceToken(bool fetch = true); /** Force the interface to re-acquire the OAUTH token and salesforce ID */ - void resetSalesforceID(); + void resetSalesforceToken(); /** Set our salesforce.com API version @@ -421,46 +421,46 @@ char *getSalesforceAPIVersion(); /** - Salesforce.com API SQL QUERY method to invoke ad-hoc SQL queries into salesforce.com - @param query_str character string with the SQL query to invoke + Salesforce.com API SOQL QUERY method to invoke ad-hoc SOQL queries into salesforce.com + @param query_str character string with the SOQL query to invoke @param output_buffer allocated result buffer to use @param output_buffer_length allocated result buffer length - @return result of the SQL query in JSON format or NULL if in error + @return result of the SOQL query in JSON format or NULL if in error */ char *query(char *query_str,char *output_buffer,int output_buffer_length); /** - Salesforce.com API field creation method to create a new field within a salesforce.com object - @param object_name name of the salesforce.com object to create the field in (i.e. "Account") - @param field MbedJSONValue json structure that the new field will be comprised with + Salesforce.com API record creation method to create a new record within a salesforce.com object + @param object_name name of the salesforce.com object to create the record in (i.e. "Account") + @param record MbedJSONValue json structure that the new record will be comprised with @return MbedJSONValue structure with the results of the creation operation in JSON format */ - MbedJSONValue createField(char *object_name,MbedJSONValue &field); + MbedJSONValue createRecord(char *object_name,MbedJSONValue &record); /** - Salesforce.com API field read method to read a field within a salesforce.com object - @param object_name name of the salesforce.com object to create the field in (i.e. "Account") - @param field_id salesforce.com ID of the field instance to read + Salesforce.com API record read method to read a record within a salesforce.com object + @param object_name name of the salesforce.com object to create the record in (i.e. "Account") + @param record_id salesforce.com ID of the record instance to read @return MbedJSONValue structure with the results of the read operation in JSON format */ - MbedJSONValue readField(char *object_name,char *field_id); + MbedJSONValue readRecord(char *object_name,char *record_id); /** - Salesforce.com API field update method to update a field within a salesforce.com object - @param object_name name of the salesforce.com object to create the field in (i.e. "Account") - @param field_id salesforce.com ID of the field instance to read - @param field MbedJSONValue instance with updated data for the field + Salesforce.com API record update method to update a record within a salesforce.com object + @param object_name name of the salesforce.com object to create the record in (i.e. "Account") + @param record_id salesforce.com ID of the record instance to read + @param record MbedJSONValue instance with updated data for the record @return true - success, false - failure */ - bool updateField(char *object_name,char *field_id,MbedJSONValue &field); + bool updateRecord(char *object_name,char *record_id,MbedJSONValue &record); /** - Salesforce.com API field delete method to delete a field within a salesforce.com object - @param object_name name of the salesforce.com object to create the field in (i.e. "Account") - @param field_id salesforce.com ID of the field instance to delete + Salesforce.com API record delete method to delete a record within a salesforce.com object + @param object_name name of the salesforce.com object to create the record in (i.e. "Account") + @param record_id salesforce.com ID of the record instance to delete @return true - success, false - failure */ - bool deleteField(char *object_name,char *field_id); + bool deleteRecord(char *object_name,char *record_id); /** Salesforce.com API invocation HTTP response code to aid in debugging error conditions @@ -471,16 +471,16 @@ protected: // do we have a valid salesforce ID and OAUTH token? - bool haveSalesforceID(bool fetch = true); + bool haveSalesforceToken(bool fetch = true); - // CREATE: a field in Salesforce.com - char *createField(char *object_name,char *json_data,char *output_buffer,int output_buffer_length); + // CREATE: a record in Salesforce.com + char *createRecord(char *object_name,char *json_data,char *output_buffer,int output_buffer_length); - // READ: a specific field in Salesforce.com - char *readField(char *object_name,char *field_id,char *output_buffer,int output_buffer_length); + // READ: a specific record in Salesforce.com + char *readRecord(char *object_name,char *record_id,char *output_buffer,int output_buffer_length); - // UPDATE: a specific field in Salesforce.com - bool updateField(char *object_name,char *field_id,char *json_data); + // UPDATE: a specific record in Salesforce.com + bool updateRecord(char *object_name,char *record_id,char *json_data); // raw invocation of REST calls into Salesforce.com char *invoke(const char *url,char *output_buffer,int output_buffer_length); // defaults to GET @@ -505,7 +505,7 @@ void fillOauthToken(char *token); bool validOauthToken(bool fetch = true); - // get the specified URL from our Salesforce ID + // get the specified URL from our Salesforce Token char *getSalesforceURL(char *key,char *url_buffer,int url_buffer_length); // simple char array replacement (modifies input string!)