Salesforce.com interface to directly access Salesforce.com
Dependencies: HTTPClient-SSL MbedJSONValue
Dependents: df-2014-salesforce-hrm-k64f
Fork of SalesforceInterface by
SalesforceInterface.h@9:a254fcd904be, 2014-09-23 (annotated)
- Committer:
- ansond
- Date:
- Tue Sep 23 05:40:18 2014 +0000
- Revision:
- 9:a254fcd904be
- Parent:
- 8:47db53cd5884
- Child:
- 10:845ea6d00b65
updates for create,read,update,delete methods
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:518b1ca956fc | 1 | /* Copyright C2014 ARM, MIT License |
ansond | 0:518b1ca956fc | 2 | * |
ansond | 9:a254fcd904be | 3 | * Author: Doug Anson (doug.anson@arm.com) |
ansond | 9:a254fcd904be | 4 | * |
ansond | 0:518b1ca956fc | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
ansond | 0:518b1ca956fc | 6 | * and associated documentation files the "Software", to deal in the Software without restriction, |
ansond | 0:518b1ca956fc | 7 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
ansond | 0:518b1ca956fc | 8 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
ansond | 0:518b1ca956fc | 9 | * furnished to do so, subject to the following conditions: |
ansond | 0:518b1ca956fc | 10 | * |
ansond | 0:518b1ca956fc | 11 | * The above copyright notice and this permission notice shall be included in all copies or |
ansond | 0:518b1ca956fc | 12 | * substantial portions of the Software. |
ansond | 0:518b1ca956fc | 13 | * |
ansond | 0:518b1ca956fc | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
ansond | 0:518b1ca956fc | 15 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ansond | 0:518b1ca956fc | 16 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
ansond | 0:518b1ca956fc | 17 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ansond | 0:518b1ca956fc | 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
ansond | 0:518b1ca956fc | 19 | */ |
ansond | 0:518b1ca956fc | 20 | |
ansond | 0:518b1ca956fc | 21 | #ifndef _SALESFORCE_INTERFACE_H_ |
ansond | 0:518b1ca956fc | 22 | #define _SALESFORCE_INTERFACE_H_ |
ansond | 0:518b1ca956fc | 23 | |
ansond | 0:518b1ca956fc | 24 | // ErrorHandler |
ansond | 0:518b1ca956fc | 25 | #include "ErrorHandler.h" |
ansond | 0:518b1ca956fc | 26 | |
ansond | 0:518b1ca956fc | 27 | // SSL-based HTTP support |
ansond | 0:518b1ca956fc | 28 | #include "HTTPClient.h" |
ansond | 0:518b1ca956fc | 29 | |
ansond | 0:518b1ca956fc | 30 | // JSON parsing support |
ansond | 0:518b1ca956fc | 31 | #include "MbedJSONValue.h" |
ansond | 0:518b1ca956fc | 32 | |
ansond | 0:518b1ca956fc | 33 | // verbose debugging |
ansond | 0:518b1ca956fc | 34 | #if ENABLE_DEBUG_LOGGING |
ansond | 8:47db53cd5884 | 35 | #define DEBUG(...) { this->logger()->logConsole(__VA_ARGS__); } |
ansond | 0:518b1ca956fc | 36 | #else |
ansond | 0:518b1ca956fc | 37 | #define DEBUG(...) |
ansond | 0:518b1ca956fc | 38 | #endif |
ansond | 7:97ea5ef906f7 | 39 | |
ansond | 7:97ea5ef906f7 | 40 | // convenience macros |
ansond | 8:47db53cd5884 | 41 | #define DEFINE_BUFFER(x) char x[MAX_BUFFER_LENGTH+1] |
ansond | 8:47db53cd5884 | 42 | #define RESET_BUFFER(x) memset(x,0,MAX_BUFFER_LENGTH+1) |
ansond | 8:47db53cd5884 | 43 | #define ALLOC_BUFFER(x) DEFINE_BUFFER(x);RESET_BUFFER(x) |
ansond | 8:47db53cd5884 | 44 | |
ansond | 8:47db53cd5884 | 45 | #define DEFINE_SML_BUFFER(x) char x[MAX_SMALL_BUFFER_LENGTH+1] |
ansond | 8:47db53cd5884 | 46 | #define RESET_SML_BUFFER(x) memset(x,0,MAX_SMALL_BUFFER_LENGTH+1) |
ansond | 8:47db53cd5884 | 47 | #define ALLOC_SML_BUFFER(x) DEFINE_SML_BUFFER(x);RESET_SML_BUFFER(x) |
ansond | 8:47db53cd5884 | 48 | |
ansond | 8:47db53cd5884 | 49 | // Default salesforce API version (must be in XX.Y format and must be a string) |
ansond | 8:47db53cd5884 | 50 | #define SALESFORCE_API_VERSION_LENGTH 10 |
ansond | 8:47db53cd5884 | 51 | #ifndef SALESFORCE_API_VERSION |
ansond | 8:47db53cd5884 | 52 | #define SALESFORCE_API_VERSION "28.0" |
ansond | 8:47db53cd5884 | 53 | #endif |
ansond | 0:518b1ca956fc | 54 | |
ansond | 0:518b1ca956fc | 55 | // HTTP Verbs |
ansond | 0:518b1ca956fc | 56 | typedef enum { |
ansond | 0:518b1ca956fc | 57 | GET, |
ansond | 0:518b1ca956fc | 58 | PUT, |
ansond | 0:518b1ca956fc | 59 | POST, |
ansond | 0:518b1ca956fc | 60 | DELETE, |
ansond | 0:518b1ca956fc | 61 | NUM_VERBS |
ansond | 0:518b1ca956fc | 62 | } HttpVerb; |
ansond | 0:518b1ca956fc | 63 | |
ansond | 0:518b1ca956fc | 64 | // Supported input data types for PUT and POST (Defined by HTTPClient-SSL/data support...) |
ansond | 0:518b1ca956fc | 65 | typedef enum { |
ansond | 0:518b1ca956fc | 66 | JSON, // ContentType: application/json |
ansond | 0:518b1ca956fc | 67 | PLAIN_TEXT, // ContentType: plain/text |
ansond | 0:518b1ca956fc | 68 | FORM_MAPPED, // ContentType: application/x-www-form-urlencoded |
ansond | 0:518b1ca956fc | 69 | NUM_TYPES |
ansond | 0:518b1ca956fc | 70 | } InputDataTypes; |
ansond | 0:518b1ca956fc | 71 | |
ansond | 7:97ea5ef906f7 | 72 | // OAUTH structure |
ansond | 7:97ea5ef906f7 | 73 | typedef struct { |
ansond | 7:97ea5ef906f7 | 74 | bool valid; |
ansond | 7:97ea5ef906f7 | 75 | string id; |
ansond | 7:97ea5ef906f7 | 76 | string issued_at; |
ansond | 7:97ea5ef906f7 | 77 | string token_type; |
ansond | 7:97ea5ef906f7 | 78 | string instance_url; |
ansond | 7:97ea5ef906f7 | 79 | string signature; |
ansond | 7:97ea5ef906f7 | 80 | string access_token; |
ansond | 7:97ea5ef906f7 | 81 | } OauthToken; |
ansond | 7:97ea5ef906f7 | 82 | |
ansond | 9:a254fcd904be | 83 | // SalesforceInterface provides an API into the REST-based Salesforce.com Force APIs |
ansond | 0:518b1ca956fc | 84 | class SalesforceInterface { |
ansond | 0:518b1ca956fc | 85 | private: |
ansond | 0:518b1ca956fc | 86 | ErrorHandler *m_logger; |
ansond | 0:518b1ca956fc | 87 | HTTPClient *m_http; |
ansond | 0:518b1ca956fc | 88 | char *m_username; |
ansond | 0:518b1ca956fc | 89 | char *m_password; |
ansond | 0:518b1ca956fc | 90 | char *m_client_id; |
ansond | 0:518b1ca956fc | 91 | char *m_client_secret; |
ansond | 0:518b1ca956fc | 92 | bool m_have_creds; |
ansond | 7:97ea5ef906f7 | 93 | OauthToken m_oauth_token; |
ansond | 7:97ea5ef906f7 | 94 | HTTPResult m_http_status; |
ansond | 7:97ea5ef906f7 | 95 | int m_http_response_code; |
ansond | 7:97ea5ef906f7 | 96 | char m_http_redirection_url[MAX_BUFFER_LENGTH+1]; |
ansond | 8:47db53cd5884 | 97 | char m_salesforce_id[MAX_BUFFER_LENGTH+1]; |
ansond | 8:47db53cd5884 | 98 | char m_salesforce_api[SALESFORCE_API_VERSION_LENGTH]; |
ansond | 0:518b1ca956fc | 99 | |
ansond | 0:518b1ca956fc | 100 | public: |
ansond | 0:518b1ca956fc | 101 | // construction/destruction |
ansond | 0:518b1ca956fc | 102 | SalesforceInterface(ErrorHandler *logger,HTTPClient *http); |
ansond | 0:518b1ca956fc | 103 | virtual ~SalesforceInterface(); |
ansond | 0:518b1ca956fc | 104 | |
ansond | 0:518b1ca956fc | 105 | // set Salesforce.com credentials |
ansond | 0:518b1ca956fc | 106 | void setCredentials(char *username,char *password,char *client_id,char *client_secret); |
ansond | 0:518b1ca956fc | 107 | |
ansond | 7:97ea5ef906f7 | 108 | // get our ID structure from Salesforce |
ansond | 9:a254fcd904be | 109 | char *getSalesforceID(bool fetch = true); |
ansond | 9:a254fcd904be | 110 | |
ansond | 9:a254fcd904be | 111 | // force the interface to re-acquire the OAUTH token and salesforce ID |
ansond | 9:a254fcd904be | 112 | void resetSalesforceID(); |
ansond | 8:47db53cd5884 | 113 | |
ansond | 8:47db53cd5884 | 114 | // set/get our salesforce API version |
ansond | 8:47db53cd5884 | 115 | void setSalesforceAPIVersion(int version); |
ansond | 8:47db53cd5884 | 116 | void setSalesforceAPIVersion(char *version); |
ansond | 8:47db53cd5884 | 117 | char *getSalesforceAPIVersion(); |
ansond | 8:47db53cd5884 | 118 | |
ansond | 9:a254fcd904be | 119 | // SQL QUERY: into Salesforce.com |
ansond | 8:47db53cd5884 | 120 | char *query(char *query_str,char *output_buffer,int output_buffer_length); |
ansond | 9:a254fcd904be | 121 | |
ansond | 9:a254fcd904be | 122 | // CREATE: a field in Salesforce.com |
ansond | 9:a254fcd904be | 123 | MbedJSONValue createField(char *object_name,MbedJSONValue &field); |
ansond | 9:a254fcd904be | 124 | |
ansond | 9:a254fcd904be | 125 | // READ: a specific field in Salesforce.com |
ansond | 9:a254fcd904be | 126 | MbedJSONValue readField(char *object_name,char *object_id); |
ansond | 9:a254fcd904be | 127 | |
ansond | 9:a254fcd904be | 128 | // UPDATE: a specific field in Salesforce.com |
ansond | 9:a254fcd904be | 129 | bool updateField(char *object_name,char *object_id,MbedJSONValue &field); |
ansond | 9:a254fcd904be | 130 | |
ansond | 9:a254fcd904be | 131 | // DELETE: a specific field in Salesforce.com |
ansond | 9:a254fcd904be | 132 | bool deleteField(char *object_name,char *object_id); |
ansond | 9:a254fcd904be | 133 | |
ansond | 9:a254fcd904be | 134 | // HTTP Error code access |
ansond | 9:a254fcd904be | 135 | int httpResponseCode(); |
ansond | 9:a254fcd904be | 136 | |
ansond | 9:a254fcd904be | 137 | protected: |
ansond | 9:a254fcd904be | 138 | // do we have a valid salesforce ID and OAUTH token? |
ansond | 9:a254fcd904be | 139 | bool haveSalesforceID(bool fetch = true); |
ansond | 9:a254fcd904be | 140 | |
ansond | 9:a254fcd904be | 141 | // CREATE: a field in Salesforce.com |
ansond | 9:a254fcd904be | 142 | char *createField(char *object_name,char *json_data,char *output_buffer,int output_buffer_length); |
ansond | 9:a254fcd904be | 143 | |
ansond | 9:a254fcd904be | 144 | // READ: a specific field in Salesforce.com |
ansond | 9:a254fcd904be | 145 | char *readField(char *object_name,char *object_id,char *output_buffer,int output_buffer_length); |
ansond | 9:a254fcd904be | 146 | |
ansond | 9:a254fcd904be | 147 | // UPDATE: a specific field in Salesforce.com |
ansond | 9:a254fcd904be | 148 | bool updateField(char *object_name,char *object_id,char *json_data); |
ansond | 9:a254fcd904be | 149 | |
ansond | 8:47db53cd5884 | 150 | // raw invocation of REST calls into Salesforce.com |
ansond | 9:a254fcd904be | 151 | char *invoke(const char *url,char *output_buffer,int output_buffer_length); // defaults to GET |
ansond | 9:a254fcd904be | 152 | char *invoke(const char *url,char *output_buffer,int output_buffer_length,HttpVerb verb); // GET or DELETE with simple output |
ansond | 9:a254fcd904be | 153 | char *invoke(const char *url,const char *input_data,const int input_data_len,char *output_buffer,int output_buffer_length); // defaults to POST with JSON input data type |
ansond | 9:a254fcd904be | 154 | char *invoke(const char *url,const InputDataTypes input_type,const char *input_data,const int input_data_len,char *output_buffer,int output_buffer_length); // defaults to POST with variable input data type |
ansond | 9:a254fcd904be | 155 | char *invoke(const char *url,const InputDataTypes input_type,const char *input_data,const int input_data_len,char *output_buffer,int output_buffer_length,const HttpVerb verb); // full fidelity method |
ansond | 9:a254fcd904be | 156 | |
ansond | 0:518b1ca956fc | 157 | // get our OAUTH Token |
ansond | 9:a254fcd904be | 158 | void checkAndGetOauthToken(bool fetch = true); |
ansond | 0:518b1ca956fc | 159 | char *getOauthToken(char *output_buffer,int output_buffer_length); |
ansond | 8:47db53cd5884 | 160 | |
ansond | 0:518b1ca956fc | 161 | // convenience accessors |
ansond | 0:518b1ca956fc | 162 | ErrorHandler *logger(); |
ansond | 0:518b1ca956fc | 163 | HTTPClient *http(); |
ansond | 7:97ea5ef906f7 | 164 | OauthToken *oauth(); |
ansond | 7:97ea5ef906f7 | 165 | HTTPResult httpStatus(); |
ansond | 7:97ea5ef906f7 | 166 | |
ansond | 7:97ea5ef906f7 | 167 | // internal checkers |
ansond | 0:518b1ca956fc | 168 | bool haveCreds(); |
ansond | 7:97ea5ef906f7 | 169 | void resetOauthToken(); |
ansond | 7:97ea5ef906f7 | 170 | void fillOauthToken(char *token); |
ansond | 9:a254fcd904be | 171 | bool validOauthToken(bool fetch = true); |
ansond | 8:47db53cd5884 | 172 | |
ansond | 8:47db53cd5884 | 173 | // get the specified URL from our Salesforce ID |
ansond | 8:47db53cd5884 | 174 | char *getSalesforceURL(char *key,char *url_buffer,int url_buffer_length); |
ansond | 9:a254fcd904be | 175 | |
ansond | 8:47db53cd5884 | 176 | // simple char array replacement (modifies input string!) |
ansond | 8:47db53cd5884 | 177 | void replace(char *str,char orig_char,char new_char); |
ansond | 8:47db53cd5884 | 178 | |
ansond | 8:47db53cd5884 | 179 | // needed to replace substrings within std::string |
ansond | 8:47db53cd5884 | 180 | void replace(string& line, string& oldString, string& newString); |
ansond | 0:518b1ca956fc | 181 | }; |
ansond | 0:518b1ca956fc | 182 | |
ansond | 0:518b1ca956fc | 183 | #endif // _SALESFORCE_INTERFACE_H_ |