Salesforce Case Generator for mbed
SalesForceCaseGenerator.cpp@13:1e06a3a4740d, 2014-09-26 (annotated)
- Committer:
- ansond
- Date:
- Fri Sep 26 04:34:41 2014 +0000
- Revision:
- 13:1e06a3a4740d
- Parent:
- 12:8603f9ff1336
revamped to use new salesforce interface
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:5953127f7c19 | 1 | /* Copyright C2014 ARM, MIT License |
ansond | 0:5953127f7c19 | 2 | * |
ansond | 13:1e06a3a4740d | 3 | * Author: Doug Anson (doug.anson@arm.com) |
ansond | 13:1e06a3a4740d | 4 | * |
ansond | 0:5953127f7c19 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
ansond | 0:5953127f7c19 | 6 | * and associated documentation files the "Software", to deal in the Software without restriction, |
ansond | 0:5953127f7c19 | 7 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
ansond | 0:5953127f7c19 | 8 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
ansond | 0:5953127f7c19 | 9 | * furnished to do so, subject to the following conditions: |
ansond | 0:5953127f7c19 | 10 | * |
ansond | 0:5953127f7c19 | 11 | * The above copyright notice and this permission notice shall be included in all copies or |
ansond | 0:5953127f7c19 | 12 | * substantial portions of the Software. |
ansond | 0:5953127f7c19 | 13 | * |
ansond | 0:5953127f7c19 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
ansond | 0:5953127f7c19 | 15 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ansond | 0:5953127f7c19 | 16 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
ansond | 0:5953127f7c19 | 17 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ansond | 0:5953127f7c19 | 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
ansond | 0:5953127f7c19 | 19 | */ |
ansond | 0:5953127f7c19 | 20 | |
ansond | 0:5953127f7c19 | 21 | #include "SalesForceCaseGenerator.h" |
ansond | 13:1e06a3a4740d | 22 | |
ansond | 13:1e06a3a4740d | 23 | // Logging |
ansond | 13:1e06a3a4740d | 24 | #define LOG(...) { if (this->logger() != NULL) { this->logger()->log(__VA_ARGS__); } } |
ansond | 13:1e06a3a4740d | 25 | #define LOG_CONSOLE(...) { if (this->logger() != NULL) { this->logger()->logConsole(__VA_ARGS__); } } |
ansond | 0:5953127f7c19 | 26 | |
ansond | 0:5953127f7c19 | 27 | // constructor |
ansond | 13:1e06a3a4740d | 28 | SalesForceCaseGenerator::SalesForceCaseGenerator(HTTPClient *http,Logger *logger) : SalesforceInterface(http,logger) { |
ansond | 0:5953127f7c19 | 29 | } |
ansond | 0:5953127f7c19 | 30 | |
ansond | 0:5953127f7c19 | 31 | // destructor |
ansond | 0:5953127f7c19 | 32 | SalesForceCaseGenerator::~SalesForceCaseGenerator() { |
ansond | 0:5953127f7c19 | 33 | } |
ansond | 0:5953127f7c19 | 34 | |
ansond | 13:1e06a3a4740d | 35 | // Create an anonymous Case instance |
ansond | 13:1e06a3a4740d | 36 | bool SalesForceCaseGenerator::createAnonymousCase(char *subject,char *description,char *condition,int temperature,char *latitude,char *longitude) { |
ansond | 0:5953127f7c19 | 37 | bool success = false; |
ansond | 0:5953127f7c19 | 38 | |
ansond | 1:5c876ee72cf0 | 39 | // data buffer and result buffer |
ansond | 13:1e06a3a4740d | 40 | ALLOC_BUFFER(result); |
ansond | 1:5c876ee72cf0 | 41 | |
ansond | 13:1e06a3a4740d | 42 | // set addl header information needed for annonymous acceptance (only) |
ansond | 3:8ad19b204ddf | 43 | char headers[] = "User-Agent: curl/7.33.0\nAccept: */*\n" ; |
ansond | 3:8ad19b204ddf | 44 | |
ansond | 13:1e06a3a4740d | 45 | // build the new case to issue to Salesforce.com |
ansond | 12:8603f9ff1336 | 46 | MbedJSONValue report; |
ansond | 12:8603f9ff1336 | 47 | report["subject"] = subject; |
ansond | 12:8603f9ff1336 | 48 | report["description"] = description; |
ansond | 12:8603f9ff1336 | 49 | report["condition"] = condition; |
ansond | 12:8603f9ff1336 | 50 | report["temperature"] = temperature; |
ansond | 12:8603f9ff1336 | 51 | report["latitude"] = latitude; |
ansond | 12:8603f9ff1336 | 52 | report["longitude"] = longitude; |
ansond | 12:8603f9ff1336 | 53 | |
ansond | 3:8ad19b204ddf | 54 | // covert to the HTTP data types |
ansond | 13:1e06a3a4740d | 55 | ALLOC_BUFFER(data); |
ansond | 12:8603f9ff1336 | 56 | strcpy(data,report.serialize().c_str()); |
ansond | 13:1e06a3a4740d | 57 | LOG_CONSOLE("Case JSON: %s",data); |
ansond | 13:1e06a3a4740d | 58 | |
ansond | 13:1e06a3a4740d | 59 | // prepare the result buffer |
ansond | 5:09d5df94e8ae | 60 | HTTPJson new_case(data,strlen(data)+1); |
ansond | 11:57f6f0f286c0 | 61 | HTTPText http_result(result,MAX_BUFFER_LENGTH); |
ansond | 13:1e06a3a4740d | 62 | |
ansond | 13:1e06a3a4740d | 63 | // set headers |
ansond | 13:1e06a3a4740d | 64 | this->http()->setHeader(headers) ; |
ansond | 13:1e06a3a4740d | 65 | |
ansond | 13:1e06a3a4740d | 66 | // for anonymous case generation, we simply use the raw HTTP interface and POST... |
ansond | 13:1e06a3a4740d | 67 | LOG_CONSOLE("Posting Case..."); |
ansond | 13:1e06a3a4740d | 68 | HTTPResult ret = this->http()->post(DF_CASE_GEN_URL,new_case,&http_result,7500); |
ansond | 13:1e06a3a4740d | 69 | if (ret == HTTP_OK) { |
ansond | 13:1e06a3a4740d | 70 | LOG_CONSOLE("Parsing Reply..."); |
ansond | 3:8ad19b204ddf | 71 | |
ansond | 13:1e06a3a4740d | 72 | // we are looking for { "status": "ok" }... |
ansond | 1:5c876ee72cf0 | 73 | success = this->contains(result,"status","ok"); |
ansond | 13:1e06a3a4740d | 74 | if (success) { |
ansond | 13:1e06a3a4740d | 75 | LOG_CONSOLE("Case post: SUCCESS"); |
ansond | 13:1e06a3a4740d | 76 | } |
ansond | 13:1e06a3a4740d | 77 | else { |
ansond | 13:1e06a3a4740d | 78 | LOG_CONSOLE("Case post: FAILED: status: %d http status: %d error: %s",ret,this->http()->getHTTPResponseCode(),result); |
ansond | 13:1e06a3a4740d | 79 | } |
ansond | 1:5c876ee72cf0 | 80 | } |
ansond | 1:5c876ee72cf0 | 81 | else { |
ansond | 13:1e06a3a4740d | 82 | LOG_CONSOLE("Case post: FAILED: status: %d http status: %d error: %s",ret,this->http()->getHTTPResponseCode(),result); |
ansond | 0:5953127f7c19 | 83 | } |
ansond | 0:5953127f7c19 | 84 | |
ansond | 0:5953127f7c19 | 85 | // return our status |
ansond | 0:5953127f7c19 | 86 | return success; |
ansond | 0:5953127f7c19 | 87 | } |