Salesforce.com interface to directly access Salesforce.com
Dependencies: HTTPClient-SSL MbedJSONValue
Dependents: df-2014-salesforce-hrm-k64f
Fork of SalesforceInterface by
Revision 21:e5a4471a46fb, committed 2014-09-26
- Comitter:
- ansond
- Date:
- Fri Sep 26 03:42:20 2014 +0000
- Parent:
- 20:0d6abaf6d7c5
- Child:
- 22:3363752cd523
- Commit message:
- logging updates
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 Thu Sep 25 05:35:05 2014 +0000 +++ b/SalesforceInterface.cpp Fri Sep 26 03:42:20 2014 +0000 @@ -43,9 +43,31 @@ #include "HTTPMap.h" #include "HTTPJson.h" + // Logging + #define LOG(...) { if (this->logger() != NULL) { this->logger()->log(__VA_ARGS__); } } + #define LOG_CONSOLE(...) { if (this->logger() != NULL) { this->logger()->logConsole(__VA_ARGS__); } } + // default constructor - SalesforceInterface::SalesforceInterface(ErrorHandler *logger,HTTPClient *http) { + SalesforceInterface::SalesforceInterface(HTTPClient *http,RawSerial *pc) { + Logger *logger = NULL; + if (pc != NULL) { + logger = new Logger(pc,NULL); + this->init(http,logger,true); + } + else { + this->init(http,NULL,false); + } + } + + // alternative constructor + SalesforceInterface::SalesforceInterface(HTTPClient *http,Logger *logger) { + this->init(http,logger,false); + } + + // initialize + void SalesforceInterface::init(HTTPClient *http,Logger *logger,bool logger_internal) { this->m_logger = logger; + this->m_logger_internal = logger_internal; this->m_http = http; this->m_username = NULL; this->m_password = NULL; @@ -63,6 +85,7 @@ // destructor SalesforceInterface::~SalesforceInterface() { + if (this->m_logger_internal == true && this->m_logger != NULL) delete this->m_logger; } // set credentials @@ -89,7 +112,7 @@ } // convenience accessors - ErrorHandler *SalesforceInterface::logger() { return this->m_logger; } + Logger *SalesforceInterface::logger() { return this->m_logger; } HTTPClient *SalesforceInterface::http() { return this->m_http; } OauthToken *SalesforceInterface::oauth() { return &this->m_oauth_token; } bool SalesforceInterface::haveCreds() { return this->m_have_creds; } @@ -132,7 +155,7 @@ DEBUG("valid OAUTH token acquired."); return; } - DEBUG("error: invalid or null OAUTH token fill attempt."); + LOG_CONSOLE("error: invalid or null OAUTH token fill attempt."); } // is our OAUTH token valid? @@ -152,7 +175,7 @@ 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 Token found... fetching..."); + LOG("Fetching Salesforce Token..."); this->getSalesforceToken(); return this->haveSalesforceToken(false); } @@ -184,14 +207,14 @@ } else { // unable to get the token (reset for sanity) - this->logger()->log("error in acquiring OAUTH token http_code=%d status=%d",this->httpResponseCode(),this->httpStatus()); + LOG_CONSOLE("error in acquiring OAUTH token http_code=%d status=%d",this->httpResponseCode(),this->httpStatus()); this->resetOauthToken(); } } // else report that we dont have a token else { - this->logger()->log("No OAUTH token found (fetch=false)."); + LOG_CONSOLE("No OAUTH token found (fetch=false)."); } } @@ -226,11 +249,11 @@ // check the result and return the token if (this->httpStatus() == HTTP_OK || this->httpResponseCode() == 200) return output_buffer; - this->logger()->log("acquire oauth FAILED. URL: %s http_code=%d status=%d",SF_OAUTH_TOKEN_URL,this->httpResponseCode(),this->httpStatus()); + LOG_CONSOLE("acquire oauth FAILED. URL: %s http_code=%d status=%d",SF_OAUTH_TOKEN_URL,this->httpResponseCode(),this->httpStatus()); } else { // no credentials - this->logger()->log("no/incomplete salesforce.com credentials provided. Unable to acquire OAUTH2 token..."); + LOG_CONSOLE("no/incomplete salesforce.com credentials provided. Unable to acquire OAUTH2 token..."); } return NULL; } @@ -244,12 +267,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 Token: status=%d httpCode=%d",this->httpStatus(),this->httpResponseCode()); + if (this->httpStatus() != HTTP_OK) LOG_CONSOLE("Unable to get Salesforce Token: status=%d httpCode=%d",this->httpStatus(),this->httpResponseCode()); return id; } else { // unable to get token - no OAUTH token - this->logger()->log("Unable to get Salesforce Token: no valid OAUTH token."); + LOG_CONSOLE("Unable to get Salesforce Token: no valid OAUTH token."); } return NULL; } @@ -282,12 +305,12 @@ } else { // unable to find the query URL... - this->logger()->log("query: error - unable to find query URL in salesforce token..."); + LOG_CONSOLE("query: error - unable to find query URL in salesforce token..."); } } else { // dont have a valid salesforce token - this->logger()->log("query: error - no valid salesforce token was found..."); + LOG_CONSOLE("query: error - no valid salesforce token was found..."); } return NULL; } @@ -360,12 +383,12 @@ } else { // dont have a valid salesforce token - this->logger()->log("createRecord: error - no valid salesforce token was found..."); + LOG_CONSOLE("createRecord: error - no valid salesforce token was found..."); } } else { // invalid or NULL parameters - this->logger()->log("createRecord: error - invalid or NULL parameters..."); + LOG_CONSOLE("createRecord: error - invalid or NULL parameters..."); } this->m_http_response_code = SF_GEN_ERR_HTTP_CODE; return NULL; @@ -406,12 +429,12 @@ } else { // dont have a valid salesforce token - this->logger()->log("readRecord: error - no valid salesforce token was found..."); + LOG_CONSOLE("readRecord: error - no valid salesforce token was found..."); } } else { // invalid or NULL parameters - this->logger()->log("readRecord: error - invalid or NULL parameters..."); + LOG_CONSOLE("readRecord: error - invalid or NULL parameters..."); } this->m_http_response_code = SF_GEN_ERR_HTTP_CODE; return NULL; @@ -462,12 +485,12 @@ } else { // dont have a valid salesforce token - this->logger()->log("updateRecord: error - no valid salesforce token was found..."); + LOG_CONSOLE("updateRecord: error - no valid salesforce token was found..."); } } else { // invalid or NULL parameters - this->logger()->log("updateRecord: error - invalid or NULL parameters..."); + LOG_CONSOLE("updateRecord: error - invalid or NULL parameters..."); } this->m_http_response_code = SF_GEN_ERR_HTTP_CODE; return false; @@ -524,12 +547,12 @@ } else { // dont have a valid salesforce token - this->logger()->log("upsertRecord: error - no valid salesforce token was found..."); + LOG_CONSOLE("upsertRecord: error - no valid salesforce token was found..."); } } else { // invalid or NULL parameters - this->logger()->log("upsertRecord: error - invalid or NULL parameters..."); + LOG_CONSOLE("upsertRecord: error - invalid or NULL parameters..."); } this->m_http_response_code = SF_GEN_ERR_HTTP_CODE; return false; @@ -559,7 +582,7 @@ str_url += record_id; // DEBUG - DEBUG("deleteRecord: URL: %s",str_url.c_str()); + LOG_CONSOLE("deleteRecord: URL: %s",str_url.c_str()); // now invoke with DELETE ALLOC_SML_BUFFER(output_buffer); @@ -578,12 +601,12 @@ } else { // dont have a valid salesforce token - this->logger()->log("deleteRecord: error - no valid salesforce token was found..."); + LOG_CONSOLE("deleteRecord: error - no valid salesforce token was found..."); } } else { // invalid or NULL parameters - this->logger()->log("deleteRecord: error - invalid or NULL parameters..."); + LOG_CONSOLE("deleteRecord: error - invalid or NULL parameters..."); } this->m_http_response_code = SF_GEN_ERR_HTTP_CODE; return false; @@ -605,7 +628,7 @@ break; default: // wrong verb for this call interface... - this->logger()->log("invoke: invalid call: must be either GET or DELETE verb if only output buffer is provided"); + LOG_CONSOLE("invoke: invalid call: must be either GET or DELETE verb if only output buffer is provided"); break; } return response; @@ -670,7 +693,7 @@ } else { // no input buffer! - this->logger()->log("invoke: ERROR HTTP(POST) requested but no input data provided... returning NULL"); + LOG_CONSOLE("invoke: ERROR HTTP(POST) requested but no input data provided... returning NULL"); } break; case PUT: @@ -690,23 +713,23 @@ } else { // no input buffer! - this->logger()->log("invoke: ERROR HTTP(PUT) requested but no input data provided... returning NULL"); + LOG_CONSOLE("invoke: ERROR HTTP(PUT) requested but no input data provided... returning NULL"); } break; default: // invalid HTTP verb - this->logger()->log("invoke: ERROR invalid HTTP verb (%d) provided... returning NULL",verb); + LOG_CONSOLE("invoke: ERROR invalid HTTP verb (%d) provided... returning NULL",verb); break; } } else { // no OAUTH Token - this->logger()->log("unable to acquire OAUTH token for credentials provided. Unable to invoke API..."); + LOG_CONSOLE("unable to acquire OAUTH token for credentials provided. Unable to invoke API..."); } } else { // no credentials - this->logger()->log("no/incomplete salesforce.com credentials provided. Unable to invoke API..."); + LOG_CONSOLE("no/incomplete salesforce.com credentials provided. Unable to invoke API..."); } // process any return results that we have @@ -726,14 +749,14 @@ } else if (this->httpResponseCodeInRange(300) /* REDIRECT */) { // error - got a redirect but have no URL - this->logger()->log("invoke error: received redirect but no URL..."); + LOG_CONSOLE("invoke error: received redirect but no URL..."); this->m_http_status = HTTP_ERROR; } } // return the response in the output buffer if (this->httpStatus() == HTTP_OK || this->httpStatus() == HTTP_REDIRECT) return output_buffer; - else this->logger()->log("invocation failed with HTTP error code=%d status=%d",this->httpResponseCode(),this->httpStatus()); + else LOG_CONSOLE("invocation failed with HTTP error code=%d status=%d",this->httpResponseCode(),this->httpStatus()); return NULL; }
--- a/SalesforceInterface.h Thu Sep 25 05:35:05 2014 +0000 +++ b/SalesforceInterface.h Fri Sep 26 03:42:20 2014 +0000 @@ -21,22 +21,15 @@ #ifndef _SALESFORCE_INTERFACE_H_ #define _SALESFORCE_INTERFACE_H_ - // ErrorHandler - #include "ErrorHandler.h" + // Logger + #include "Logger.h" // SSL-based HTTP support #include "HTTPClient.h" // JSON parsing support #include "MbedJSONValue.h" - - // verbose debugging - #if ENABLE_DEBUG_LOGGING - #define DEBUG(...) { this->logger()->logConsole(__VA_ARGS__); } - #else - #define DEBUG(...) - #endif - + // convenience macros #define DEFINE_BUFFER(x) char x[MAX_BUFFER_LENGTH+1] #define RESET_BUFFER(x) memset(x,0,MAX_BUFFER_LENGTH+1) @@ -52,6 +45,13 @@ #define SALESFORCE_API_VERSION "28.0" #endif + // verbose debugging within SalesforceInterface + #if ENABLE_DEBUG_LOGGING + #define DEBUG(...) { LOG_CONSOLE(__VA_ARGS__); } + #else + #define DEBUG(...) { ; } + #endif + // HTTP Verbs typedef enum { GET, @@ -89,7 +89,7 @@ * @code #include "Definitions.h" // definitions including platform specifics... - #include "ErrorHandler.h" + #include "Logger.h" // include salesforce.com credentials #include "sf_creds.h" @@ -119,7 +119,7 @@ // *************** Test Cases ************************ - void Test_getSalesforceToken(ErrorHandler *logger,SalesforceInterface *sf) { + void Test_getSalesforceToken(Logger *logger,SalesforceInterface *sf) { logger->log("\r\n\r\nGetting Salesforce Token..."); logger->turnLEDPurple(); @@ -132,7 +132,7 @@ logger->turnLEDGreen(); } - void Test_query(ErrorHandler *logger,SalesforceInterface *sf,char *query_str) { + void Test_query(Logger *logger,SalesforceInterface *sf,char *query_str) { logger->log("\r\n\r\nExecuting test query: %s",query_str); logger->turnLEDPurple(); if (query_str != NULL && strlen(query_str) > 0) { @@ -147,7 +147,7 @@ logger->turnLEDGreen(); } - void Test_create(ErrorHandler *logger,SalesforceInterface *sf) { + void Test_create(Logger *logger,SalesforceInterface *sf) { logger->log("\r\n\r\nExecuting create()"); logger->turnLEDPurple(); @@ -177,7 +177,7 @@ logger->turnLEDGreen(); } - void Test_read(ErrorHandler *logger,SalesforceInterface *sf) { + void Test_read(Logger *logger,SalesforceInterface *sf) { logger->log("\r\n\r\nExecuting read()"); logger->turnLEDPurple(); @@ -202,7 +202,7 @@ logger->turnLEDGreen(); } - void Test_create_external_id(ErrorHandler *logger,SalesforceInterface *sf) { + void Test_create_external_id(Logger *logger,SalesforceInterface *sf) { logger->log("\r\n\r\nExecuting create(ExternalID)"); logger->turnLEDPurple(); @@ -232,7 +232,7 @@ logger->turnLEDGreen(); } - void Test_read_by_external_id_and_value(ErrorHandler *logger,SalesforceInterface *sf) { + void Test_read_by_external_id_and_value(Logger *logger,SalesforceInterface *sf) { logger->log("\r\n\r\nExecuting read(externalID)..."); logger->turnLEDPurple(); @@ -257,7 +257,7 @@ logger->turnLEDGreen(); } - void Test_update(ErrorHandler *logger,SalesforceInterface *sf) { + void Test_update(Logger *logger,SalesforceInterface *sf) { logger->log("\r\n\r\nExecuting update()"); logger->turnLEDPurple(); @@ -283,7 +283,7 @@ logger->turnLEDGreen(); } - void Test_upsert_external_id(ErrorHandler *logger,SalesforceInterface *sf) { + void Test_upsert_external_id(Logger *logger,SalesforceInterface *sf) { logger->log("\r\n\r\nExecuting upsert(ExternalID)"); logger->turnLEDPurple(); @@ -309,7 +309,7 @@ logger->turnLEDGreen(); } - void Test_delete(ErrorHandler *logger,SalesforceInterface *sf) { + void Test_delete(Logger *logger,SalesforceInterface *sf) { logger->log("\r\n\r\nExecuting delete()"); logger->turnLEDPurple(); @@ -332,7 +332,7 @@ logger->turnLEDGreen(); } - void Test_reset_auth(ErrorHandler *logger,SalesforceInterface *sf) { + void Test_reset_auth(Logger *logger,SalesforceInterface *sf) { logger->log("\r\n\r\nForcing API to reset OAUTH token and Salesforce Token..."); logger->turnLEDPurple(); sf->resetSalesforceToken(); @@ -345,7 +345,7 @@ void mainTask(void const *v) { // create our object instances - ErrorHandler logger(&pc,NULL); + Logger logger(&pc,NULL); SalesforceInterface *sf = NULL; // announce @@ -365,7 +365,7 @@ // allocate the Salesforce.com interface logger.log("Allocating Saleforce.com interface..."); - sf = new SalesforceInterface(&logger,&http); + sf = new SalesforceInterface(&http,&logger); // set our Salesforce.com credentials sf->setCredentials(username,password,client_id,client_secret); @@ -449,7 +449,8 @@ */ class SalesforceInterface { private: - ErrorHandler *m_logger; + Logger *m_logger; + bool m_logger_internal; HTTPClient *m_http; char *m_username; char *m_password; @@ -467,10 +468,17 @@ public: /** Default constructor - @param logger ErrorHandler instance @param http HTTPClient instance + @param pc optional RawSerial for debugging output. If NULL, there will be no debugging output */ - SalesforceInterface(ErrorHandler *logger,HTTPClient *http); + SalesforceInterface(HTTPClient *http,RawSerial *pc = NULL); + + /** + Alternative constructor + @param http HTTPClient instance + @param logger optional Logger instance (See Logger for more info). If NULL, there will be no debugging output + */ + SalesforceInterface(HTTPClient *http,Logger *logger = NULL); /** Default destructor @@ -613,7 +621,7 @@ char *getOauthToken(char *output_buffer,int output_buffer_length); // convenience accessors - ErrorHandler *logger(); + Logger *logger(); HTTPClient *http(); OauthToken *oauth(); HTTPResult httpStatus(); @@ -638,6 +646,9 @@ // min() method int min(int value1,int value2); + + // initialize + void init(HTTPClient *http,Logger *logger,bool logger_internal); }; #endif // _SALESFORCE_INTERFACE_H_ \ No newline at end of file