Salesforce Case Generator for mbed

Committer:
ansond
Date:
Wed Aug 27 16:52:58 2014 +0000
Revision:
2:670837d3e248
Parent:
1:5c876ee72cf0
Child:
3:8ad19b204ddf
updates - working now

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:5953127f7c19 1 /* Copyright C2014 ARM, MIT License
ansond 0:5953127f7c19 2 *
ansond 0:5953127f7c19 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ansond 0:5953127f7c19 4 * and associated documentation files the "Software", to deal in the Software without restriction,
ansond 0:5953127f7c19 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ansond 0:5953127f7c19 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ansond 0:5953127f7c19 7 * furnished to do so, subject to the following conditions:
ansond 0:5953127f7c19 8 *
ansond 0:5953127f7c19 9 * The above copyright notice and this permission notice shall be included in all copies or
ansond 0:5953127f7c19 10 * substantial portions of the Software.
ansond 0:5953127f7c19 11 *
ansond 0:5953127f7c19 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ansond 0:5953127f7c19 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ansond 0:5953127f7c19 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ansond 0:5953127f7c19 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ansond 0:5953127f7c19 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ansond 0:5953127f7c19 17 */
ansond 0:5953127f7c19 18
ansond 0:5953127f7c19 19 #include "Definitions.h"
ansond 0:5953127f7c19 20 #include "SalesForceCaseGenerator.h"
ansond 0:5953127f7c19 21 #include "MbedJSONValue.h"
ansond 0:5953127f7c19 22
ansond 0:5953127f7c19 23 // constructor
ansond 0:5953127f7c19 24 SalesForceCaseGenerator::SalesForceCaseGenerator(Logger *logger) {
ansond 0:5953127f7c19 25 this->m_logger = logger;
ansond 0:5953127f7c19 26 }
ansond 0:5953127f7c19 27
ansond 0:5953127f7c19 28 // destructor
ansond 0:5953127f7c19 29 SalesForceCaseGenerator::~SalesForceCaseGenerator() {
ansond 0:5953127f7c19 30 }
ansond 0:5953127f7c19 31
ansond 0:5953127f7c19 32 // create a case through APEX
ansond 0:5953127f7c19 33 bool SalesForceCaseGenerator::createCase(char *subject,char *description) {
ansond 0:5953127f7c19 34 bool success = false;
ansond 0:5953127f7c19 35
ansond 1:5c876ee72cf0 36 // data buffer and result buffer
ansond 1:5c876ee72cf0 37 char data[BUFFER_LENGTH+1];
ansond 1:5c876ee72cf0 38 char result[BUFFER_LENGTH+1];
ansond 1:5c876ee72cf0 39 memset(data,0,BUFFER_LENGTH+1);
ansond 1:5c876ee72cf0 40 memset(result,0,BUFFER_LENGTH+1);
ansond 1:5c876ee72cf0 41
ansond 1:5c876ee72cf0 42 // create the case
ansond 2:670837d3e248 43 sprintf(data,"{\"subject\":\"%s\",\"description\":\"%s\"}", subject, description);
ansond 2:670837d3e248 44
ansond 1:5c876ee72cf0 45 // Create the inbound and outbound buffers
ansond 1:5c876ee72cf0 46 HTTPText http_data(data,strlen(data)+1);
ansond 1:5c876ee72cf0 47 HTTPText http_result(result,BUFFER_LENGTH);
ansond 2:670837d3e248 48
ansond 1:5c876ee72cf0 49 // POST the case and check the response
ansond 2:670837d3e248 50 this->m_logger->log("Sending Request...\r\nlength=%d bytes",strlen(data));
ansond 1:5c876ee72cf0 51 if (this->m_http.post(DF_CASE_GEN_URL,http_data,&http_result) == 0) {
ansond 2:670837d3e248 52 this->m_logger->log("Parsing Reply...");
ansond 1:5c876ee72cf0 53 success = this->contains(result,"status","ok");
ansond 1:5c876ee72cf0 54 if (success)
ansond 1:5c876ee72cf0 55 this->m_logger->log("Case generated successfully");
ansond 1:5c876ee72cf0 56 else
ansond 1:5c876ee72cf0 57 this->m_logger->log("Case generation FAILED: %s",data);
ansond 1:5c876ee72cf0 58 }
ansond 1:5c876ee72cf0 59 else {
ansond 1:5c876ee72cf0 60 this->m_logger->log("Failed to send get request");
ansond 0:5953127f7c19 61 }
ansond 0:5953127f7c19 62
ansond 0:5953127f7c19 63 // return our status
ansond 0:5953127f7c19 64 return success;
ansond 0:5953127f7c19 65 }
ansond 0:5953127f7c19 66
ansond 0:5953127f7c19 67 // look for a specific key value pair in a json message
ansond 0:5953127f7c19 68 bool SalesForceCaseGenerator::contains(char *json,char *key,char *value) {
ansond 0:5953127f7c19 69 bool has_it = false;
ansond 0:5953127f7c19 70
ansond 0:5953127f7c19 71 if (json != NULL && key != NULL && value != NULL) {
ansond 0:5953127f7c19 72 // parse the json and look for a specific <key,value> pair...
ansond 0:5953127f7c19 73 MbedJSONValue parsed;
ansond 0:5953127f7c19 74 parse(parsed,json);
ansond 0:5953127f7c19 75 char *answer = (char *)parsed[key].get<std::string>().c_str();
ansond 0:5953127f7c19 76 if (strcmp(answer,value) == 0) has_it = true;
ansond 0:5953127f7c19 77 }
ansond 0:5953127f7c19 78
ansond 0:5953127f7c19 79 return has_it;
ansond 0:5953127f7c19 80 }