Salesforce Case Generator for mbed
Diff: SalesForceCaseGenerator.cpp
- Revision:
- 13:1e06a3a4740d
- Parent:
- 12:8603f9ff1336
diff -r 8603f9ff1336 -r 1e06a3a4740d SalesForceCaseGenerator.cpp --- a/SalesForceCaseGenerator.cpp Wed Sep 24 18:52:45 2014 +0000 +++ b/SalesForceCaseGenerator.cpp Fri Sep 26 04:34:41 2014 +0000 @@ -1,5 +1,7 @@ /* Copyright C2014 ARM, MIT License * + * Author: Doug Anson (doug.anson@arm.com) + * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software * and associated documentation files the "Software", to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, publish, distribute, @@ -16,32 +18,31 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - #include "Definitions.h" #include "SalesForceCaseGenerator.h" - #include "MbedJSONValue.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__); } } // constructor - SalesForceCaseGenerator::SalesForceCaseGenerator(ErrorHandler *logger,void *transport) { - this->m_logger = logger; - this->m_http = (HTTPClient *)transport; + SalesForceCaseGenerator::SalesForceCaseGenerator(HTTPClient *http,Logger *logger) : SalesforceInterface(http,logger) { } // destructor SalesForceCaseGenerator::~SalesForceCaseGenerator() { } - // create a case through APEX - bool SalesForceCaseGenerator::createCase(char *subject,char *description,char *condition,int temperature,char *latitude,char *longitude) { + // Create an anonymous Case instance + bool SalesForceCaseGenerator::createAnonymousCase(char *subject,char *description,char *condition,int temperature,char *latitude,char *longitude) { bool success = false; // data buffer and result buffer - char result[MAX_BUFFER_LENGTH+1]; - memset(result,0,MAX_BUFFER_LENGTH+1); + ALLOC_BUFFER(result); - // set addl header information + // set addl header information needed for annonymous acceptance (only) char headers[] = "User-Agent: curl/7.33.0\nAccept: */*\n" ; - // build the new Salesforce.com report + // build the new case to issue to Salesforce.com MbedJSONValue report; report["subject"] = subject; report["description"] = description; @@ -51,48 +52,36 @@ report["longitude"] = longitude; // covert to the HTTP data types - char data[MAX_BUFFER_LENGTH+1]; - memset(data,0,MAX_BUFFER_LENGTH+1); + ALLOC_BUFFER(data); strcpy(data,report.serialize().c_str()); - this->m_logger->log("Case JSON: %s",data); + LOG_CONSOLE("Case JSON: %s",data); + + // prepare the result buffer HTTPJson new_case(data,strlen(data)+1); HTTPText http_result(result,MAX_BUFFER_LENGTH); + + // set headers + this->http()->setHeader(headers) ; + + // for anonymous case generation, we simply use the raw HTTP interface and POST... + LOG_CONSOLE("Posting Case..."); + HTTPResult ret = this->http()->post(DF_CASE_GEN_URL,new_case,&http_result,7500); + if (ret == HTTP_OK) { + LOG_CONSOLE("Parsing Reply..."); - this->m_http->setHeader(headers) ; - this->m_http->setSSLversion(1) ; /* TLSv1.0 */ - - // POST the case and check the response - this->m_logger->log("Posting new Case..."); - this->m_logger->blinkTransportTxLED(); - int ret = this->m_http->post(DF_CASE_GEN_URL,new_case,&http_result,7500); - if (!ret) { - this->m_logger->log("Parsing Reply..."); + // we are looking for { "status": "ok" }... success = this->contains(result,"status","ok"); - this->m_logger->blinkTransportRxLED(); - if (success) - this->m_logger->log("Case generation SUCCESS"); - else - this->m_logger->log("Case generation FAILED: %s",result); + if (success) { + LOG_CONSOLE("Case post: SUCCESS"); + } + else { + LOG_CONSOLE("Case post: FAILED: status: %d http status: %d error: %s",ret,this->http()->getHTTPResponseCode(),result); + } } else { - this->m_logger->log("Failed to post case (%d) (%s)",ret,result); + LOG_CONSOLE("Case post: FAILED: status: %d http status: %d error: %s",ret,this->http()->getHTTPResponseCode(),result); } // return our status return success; - } - - // look for a specific key value pair in a json message - bool SalesForceCaseGenerator::contains(char *json,char *key,char *value) { - bool has_it = false; - - if (json != NULL && key != NULL && value != NULL) { - // parse the json and look for a specific <key,value> pair... - MbedJSONValue parsed; - parse(parsed,json); - char *answer = (char *)parsed[key].get<std::string>().c_str(); - if (strcmp(answer,value) == 0) has_it = true; - } - - return has_it; } \ No newline at end of file