Interface for invoking Salesforce.com REST calls over SSL with OAUTH authentication. This interface is designed to simplify the interaction between mbed devices and salesforce.com web services.
Dependencies: HTTPClient-SSL MbedJSONValue
Diff: SalesforceInterface.cpp
- Revision:
- 21:e5a4471a46fb
- Parent:
- 20:0d6abaf6d7c5
- Child:
- 22:3363752cd523
--- 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; }