Salesforce.com interface to directly access Salesforce.com

Dependencies:   HTTPClient-SSL MbedJSONValue

Dependents:   df-2014-salesforce-hrm-k64f

Fork of SalesforceInterface by Doug Anson

Committer:
ansond
Date:
Mon Sep 22 04:28:02 2014 +0000
Revision:
8:47db53cd5884
Parent:
7:97ea5ef906f7
Child:
9:a254fcd904be
fixes to enable query to work

Who changed what in which revision?

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