Interface for invoking Salesforce.com REST calls over SSL with OAUTH authentication. This interface is designed to simplify the interaction between mbed devices and salesforce.com web services.
Dependencies: HTTPClient-SSL MbedJSONValue
SalesforceInterface.h@7:97ea5ef906f7, 2014-09-21 (annotated)
- Committer:
- ansond
- Date:
- Sun Sep 21 07:08:51 2014 +0000
- Revision:
- 7:97ea5ef906f7
- Parent:
- 0:518b1ca956fc
- Child:
- 8:47db53cd5884
fixes and updates for initial id extraction
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 | 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 | 0:518b1ca956fc | 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 | 7:97ea5ef906f7 | 39 | #define RESET_BUFFER(x) memset(x,0,MAX_BUFFER_LENGTH+1) |
ansond | 7:97ea5ef906f7 | 40 | #define ALLOC_BUFFER(x) char x[MAX_BUFFER_LENGTH+1];RESET_BUFFER(x) |
ansond | 0:518b1ca956fc | 41 | |
ansond | 0:518b1ca956fc | 42 | // HTTP Verbs |
ansond | 0:518b1ca956fc | 43 | typedef enum { |
ansond | 0:518b1ca956fc | 44 | GET, |
ansond | 0:518b1ca956fc | 45 | PUT, |
ansond | 0:518b1ca956fc | 46 | POST, |
ansond | 0:518b1ca956fc | 47 | DELETE, |
ansond | 0:518b1ca956fc | 48 | NUM_VERBS |
ansond | 0:518b1ca956fc | 49 | } HttpVerb; |
ansond | 0:518b1ca956fc | 50 | |
ansond | 0:518b1ca956fc | 51 | // Supported input data types for PUT and POST (Defined by HTTPClient-SSL/data support...) |
ansond | 0:518b1ca956fc | 52 | typedef enum { |
ansond | 0:518b1ca956fc | 53 | JSON, // ContentType: application/json |
ansond | 0:518b1ca956fc | 54 | PLAIN_TEXT, // ContentType: plain/text |
ansond | 0:518b1ca956fc | 55 | FORM_MAPPED, // ContentType: application/x-www-form-urlencoded |
ansond | 0:518b1ca956fc | 56 | NUM_TYPES |
ansond | 0:518b1ca956fc | 57 | } InputDataTypes; |
ansond | 0:518b1ca956fc | 58 | |
ansond | 7:97ea5ef906f7 | 59 | // OAUTH structure |
ansond | 7:97ea5ef906f7 | 60 | typedef struct { |
ansond | 7:97ea5ef906f7 | 61 | bool valid; |
ansond | 7:97ea5ef906f7 | 62 | string id; |
ansond | 7:97ea5ef906f7 | 63 | string issued_at; |
ansond | 7:97ea5ef906f7 | 64 | string token_type; |
ansond | 7:97ea5ef906f7 | 65 | string instance_url; |
ansond | 7:97ea5ef906f7 | 66 | string signature; |
ansond | 7:97ea5ef906f7 | 67 | string access_token; |
ansond | 7:97ea5ef906f7 | 68 | } OauthToken; |
ansond | 7:97ea5ef906f7 | 69 | |
ansond | 0:518b1ca956fc | 70 | // This class provides an interface into the REST-based Salesforce.com APIs |
ansond | 0:518b1ca956fc | 71 | class SalesforceInterface { |
ansond | 0:518b1ca956fc | 72 | private: |
ansond | 0:518b1ca956fc | 73 | ErrorHandler *m_logger; |
ansond | 0:518b1ca956fc | 74 | HTTPClient *m_http; |
ansond | 0:518b1ca956fc | 75 | char *m_username; |
ansond | 0:518b1ca956fc | 76 | char *m_password; |
ansond | 0:518b1ca956fc | 77 | char *m_client_id; |
ansond | 0:518b1ca956fc | 78 | char *m_client_secret; |
ansond | 0:518b1ca956fc | 79 | bool m_have_creds; |
ansond | 7:97ea5ef906f7 | 80 | OauthToken m_oauth_token; |
ansond | 7:97ea5ef906f7 | 81 | HTTPResult m_http_status; |
ansond | 7:97ea5ef906f7 | 82 | int m_http_response_code; |
ansond | 7:97ea5ef906f7 | 83 | char m_http_redirection_url[MAX_BUFFER_LENGTH+1]; |
ansond | 0:518b1ca956fc | 84 | |
ansond | 0:518b1ca956fc | 85 | public: |
ansond | 0:518b1ca956fc | 86 | // construction/destruction |
ansond | 0:518b1ca956fc | 87 | SalesforceInterface(ErrorHandler *logger,HTTPClient *http); |
ansond | 0:518b1ca956fc | 88 | virtual ~SalesforceInterface(); |
ansond | 0:518b1ca956fc | 89 | |
ansond | 0:518b1ca956fc | 90 | // set Salesforce.com credentials |
ansond | 0:518b1ca956fc | 91 | void setCredentials(char *username,char *password,char *client_id,char *client_secret); |
ansond | 0:518b1ca956fc | 92 | |
ansond | 7:97ea5ef906f7 | 93 | // get our ID structure from Salesforce |
ansond | 7:97ea5ef906f7 | 94 | char *getSalesforceID(char *output_buffer,int output_buffer_length); |
ansond | 7:97ea5ef906f7 | 95 | |
ansond | 7:97ea5ef906f7 | 96 | // invoke REST calls into Salesforce.com |
ansond | 7:97ea5ef906f7 | 97 | char *invoke(const char *url,char *output_buffer,int output_buffer_len); // defaults to GET |
ansond | 7:97ea5ef906f7 | 98 | 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 | 7:97ea5ef906f7 | 99 | 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 | 100 | 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 | 7:97ea5ef906f7 | 101 | |
ansond | 7:97ea5ef906f7 | 102 | private: |
ansond | 0:518b1ca956fc | 103 | // get our OAUTH Token |
ansond | 7:97ea5ef906f7 | 104 | void checkAndGetOauthToken(); |
ansond | 0:518b1ca956fc | 105 | char *getOauthToken(char *output_buffer,int output_buffer_length); |
ansond | 0:518b1ca956fc | 106 | |
ansond | 0:518b1ca956fc | 107 | // convenience accessors |
ansond | 0:518b1ca956fc | 108 | ErrorHandler *logger(); |
ansond | 0:518b1ca956fc | 109 | HTTPClient *http(); |
ansond | 7:97ea5ef906f7 | 110 | OauthToken *oauth(); |
ansond | 7:97ea5ef906f7 | 111 | HTTPResult httpStatus(); |
ansond | 7:97ea5ef906f7 | 112 | int httpResponseCode(); |
ansond | 7:97ea5ef906f7 | 113 | |
ansond | 7:97ea5ef906f7 | 114 | // internal checkers |
ansond | 0:518b1ca956fc | 115 | bool haveCreds(); |
ansond | 7:97ea5ef906f7 | 116 | void resetOauthToken(); |
ansond | 7:97ea5ef906f7 | 117 | void fillOauthToken(char *token); |
ansond | 7:97ea5ef906f7 | 118 | bool validOauthToken(); |
ansond | 0:518b1ca956fc | 119 | }; |
ansond | 0:518b1ca956fc | 120 | |
ansond | 0:518b1ca956fc | 121 | #endif // _SALESFORCE_INTERFACE_H_ |