Salesforce.com interface to directly access Salesforce.com
Dependencies: HTTPClient-SSL MbedJSONValue
Dependents: df-2014-salesforce-hrm-k64f
Fork of SalesforceInterface by
SalesforceInterface.cpp@7:97ea5ef906f7, 2014-09-21 (annotated)
- Committer:
- ansond
- Date:
- Sun Sep 21 07:08:51 2014 +0000
- Revision:
- 7:97ea5ef906f7
- Parent:
- 1:a7dca096e47d
- 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 | // Tuneables |
ansond | 0:518b1ca956fc | 20 | #define SF_OAUTH_TOKEN_URL "https://login.salesforce.com/services/oauth2/token" |
ansond | 0:518b1ca956fc | 21 | #define SF_OAUTH_REQUEST_BODY "grant_type=password&client_id=%s&client_secret=%s&username=%s&password=%s" |
ansond | 0:518b1ca956fc | 22 | #define SF_HTTP_AUTH_HEADER "Authorization: Bearer %s" |
ansond | 0:518b1ca956fc | 23 | |
ansond | 0:518b1ca956fc | 24 | // include class definition |
ansond | 0:518b1ca956fc | 25 | #include "SalesforceInterface.h" |
ansond | 0:518b1ca956fc | 26 | |
ansond | 0:518b1ca956fc | 27 | // Supported DataTypes for HTTPClient |
ansond | 0:518b1ca956fc | 28 | #include "HTTPMap.h" |
ansond | 0:518b1ca956fc | 29 | #include "HTTPJson.h" |
ansond | 0:518b1ca956fc | 30 | |
ansond | 0:518b1ca956fc | 31 | // default constructor |
ansond | 0:518b1ca956fc | 32 | SalesforceInterface::SalesforceInterface(ErrorHandler *logger,HTTPClient *http) { |
ansond | 0:518b1ca956fc | 33 | this->m_logger = logger; |
ansond | 0:518b1ca956fc | 34 | this->m_http = http; |
ansond | 0:518b1ca956fc | 35 | this->m_username = NULL; |
ansond | 0:518b1ca956fc | 36 | this->m_password = NULL; |
ansond | 0:518b1ca956fc | 37 | this->m_client_id = NULL; |
ansond | 0:518b1ca956fc | 38 | this->m_client_secret = NULL; |
ansond | 0:518b1ca956fc | 39 | this->m_have_creds = false; |
ansond | 7:97ea5ef906f7 | 40 | this->m_http_status = HTTP_OK; |
ansond | 7:97ea5ef906f7 | 41 | this->m_http_response_code = -1; |
ansond | 7:97ea5ef906f7 | 42 | RESET_BUFFER(this->m_http_redirection_url); |
ansond | 7:97ea5ef906f7 | 43 | this->resetOauthToken(); |
ansond | 0:518b1ca956fc | 44 | } |
ansond | 0:518b1ca956fc | 45 | |
ansond | 0:518b1ca956fc | 46 | // destructor |
ansond | 0:518b1ca956fc | 47 | SalesforceInterface::~SalesforceInterface() { |
ansond | 0:518b1ca956fc | 48 | } |
ansond | 0:518b1ca956fc | 49 | |
ansond | 0:518b1ca956fc | 50 | // set credentials |
ansond | 0:518b1ca956fc | 51 | void SalesforceInterface::setCredentials(char *username,char *password,char *client_id,char *client_secret) { |
ansond | 0:518b1ca956fc | 52 | this->m_username = NULL; |
ansond | 0:518b1ca956fc | 53 | this->m_password = NULL; |
ansond | 0:518b1ca956fc | 54 | this->m_client_id = NULL; |
ansond | 0:518b1ca956fc | 55 | this->m_client_secret = NULL; |
ansond | 0:518b1ca956fc | 56 | this->m_have_creds = false; |
ansond | 0:518b1ca956fc | 57 | |
ansond | 0:518b1ca956fc | 58 | if (username != NULL) { |
ansond | 0:518b1ca956fc | 59 | this->m_username = username; |
ansond | 0:518b1ca956fc | 60 | if (password != NULL) { |
ansond | 0:518b1ca956fc | 61 | this->m_password = password; |
ansond | 0:518b1ca956fc | 62 | if (client_id != NULL) { |
ansond | 0:518b1ca956fc | 63 | this->m_client_id = client_id; |
ansond | 0:518b1ca956fc | 64 | if (client_secret != NULL) { |
ansond | 0:518b1ca956fc | 65 | this->m_client_secret = client_secret; |
ansond | 0:518b1ca956fc | 66 | this->m_have_creds = true; |
ansond | 0:518b1ca956fc | 67 | } |
ansond | 0:518b1ca956fc | 68 | } |
ansond | 0:518b1ca956fc | 69 | } |
ansond | 0:518b1ca956fc | 70 | } |
ansond | 0:518b1ca956fc | 71 | } |
ansond | 0:518b1ca956fc | 72 | |
ansond | 0:518b1ca956fc | 73 | // convenience accessors |
ansond | 0:518b1ca956fc | 74 | ErrorHandler *SalesforceInterface::logger() { return this->m_logger; } |
ansond | 0:518b1ca956fc | 75 | HTTPClient *SalesforceInterface::http() { return this->m_http; } |
ansond | 7:97ea5ef906f7 | 76 | OauthToken *SalesforceInterface::oauth() { return &this->m_oauth_token; } |
ansond | 0:518b1ca956fc | 77 | bool SalesforceInterface::haveCreds() { return this->m_have_creds; } |
ansond | 7:97ea5ef906f7 | 78 | HTTPResult SalesforceInterface::httpStatus() { return this->m_http_status; } |
ansond | 7:97ea5ef906f7 | 79 | int SalesforceInterface::httpResponseCode() { return this->m_http_response_code; } |
ansond | 7:97ea5ef906f7 | 80 | |
ansond | 7:97ea5ef906f7 | 81 | // reset our oauth token |
ansond | 7:97ea5ef906f7 | 82 | void SalesforceInterface::resetOauthToken() { |
ansond | 7:97ea5ef906f7 | 83 | //DEBUG("resetting OAUTH token..."); |
ansond | 7:97ea5ef906f7 | 84 | this->m_oauth_token.valid = false; |
ansond | 7:97ea5ef906f7 | 85 | this->m_oauth_token.id = ""; |
ansond | 7:97ea5ef906f7 | 86 | this->m_oauth_token.issued_at = ""; |
ansond | 7:97ea5ef906f7 | 87 | this->m_oauth_token.token_type = ""; |
ansond | 7:97ea5ef906f7 | 88 | this->m_oauth_token.instance_url = ""; |
ansond | 7:97ea5ef906f7 | 89 | this->m_oauth_token.signature = ""; |
ansond | 7:97ea5ef906f7 | 90 | this->m_oauth_token.access_token = ""; |
ansond | 7:97ea5ef906f7 | 91 | } |
ansond | 7:97ea5ef906f7 | 92 | |
ansond | 7:97ea5ef906f7 | 93 | // fill our oauth token |
ansond | 7:97ea5ef906f7 | 94 | void SalesforceInterface::fillOauthToken(char *token) { |
ansond | 7:97ea5ef906f7 | 95 | if (token != NULL && strlen(token) > 0) { |
ansond | 7:97ea5ef906f7 | 96 | // parse JSON |
ansond | 7:97ea5ef906f7 | 97 | MbedJSONValue parsed_token; |
ansond | 7:97ea5ef906f7 | 98 | parse(parsed_token,token); |
ansond | 7:97ea5ef906f7 | 99 | |
ansond | 7:97ea5ef906f7 | 100 | // fill our OAUTH token |
ansond | 7:97ea5ef906f7 | 101 | this->m_oauth_token.id = parsed_token["id"].get<std::string>(); |
ansond | 7:97ea5ef906f7 | 102 | this->m_oauth_token.issued_at = parsed_token["issued_at"].get<std::string>(); |
ansond | 7:97ea5ef906f7 | 103 | this->m_oauth_token.token_type = parsed_token["token_type"].get<std::string>(); |
ansond | 7:97ea5ef906f7 | 104 | this->m_oauth_token.instance_url = parsed_token["instance_url"].get<std::string>(); |
ansond | 7:97ea5ef906f7 | 105 | this->m_oauth_token.signature = parsed_token["signature"].get<std::string>(); |
ansond | 7:97ea5ef906f7 | 106 | this->m_oauth_token.access_token = parsed_token["access_token"].get<std::string>(); |
ansond | 7:97ea5ef906f7 | 107 | |
ansond | 7:97ea5ef906f7 | 108 | // we have an OAUTH token now |
ansond | 7:97ea5ef906f7 | 109 | this->m_oauth_token.valid = true; |
ansond | 7:97ea5ef906f7 | 110 | DEBUG("valid OAUTH token acquired."); |
ansond | 7:97ea5ef906f7 | 111 | return; |
ansond | 7:97ea5ef906f7 | 112 | } |
ansond | 7:97ea5ef906f7 | 113 | DEBUG("error: invalid or null OAUTH token fill attempt."); |
ansond | 7:97ea5ef906f7 | 114 | } |
ansond | 7:97ea5ef906f7 | 115 | |
ansond | 7:97ea5ef906f7 | 116 | // is our OAUTH token valid? |
ansond | 7:97ea5ef906f7 | 117 | bool SalesforceInterface::validOauthToken() { |
ansond | 7:97ea5ef906f7 | 118 | // make sure we have a valid OAUTH Token |
ansond | 7:97ea5ef906f7 | 119 | this->checkAndGetOauthToken(); |
ansond | 7:97ea5ef906f7 | 120 | |
ansond | 7:97ea5ef906f7 | 121 | // TODO: we currently only return fill status. Later we may want to check dates too... |
ansond | 7:97ea5ef906f7 | 122 | return this->m_oauth_token.valid; |
ansond | 7:97ea5ef906f7 | 123 | } |
ansond | 7:97ea5ef906f7 | 124 | |
ansond | 7:97ea5ef906f7 | 125 | // check and get our OAUTH token |
ansond | 7:97ea5ef906f7 | 126 | void SalesforceInterface::checkAndGetOauthToken() { |
ansond | 7:97ea5ef906f7 | 127 | DEBUG("checking for valid OAUTH token..."); |
ansond | 7:97ea5ef906f7 | 128 | if (this->m_oauth_token.valid == false) { |
ansond | 7:97ea5ef906f7 | 129 | // re-initialize token |
ansond | 7:97ea5ef906f7 | 130 | this->resetOauthToken(); |
ansond | 7:97ea5ef906f7 | 131 | |
ansond | 7:97ea5ef906f7 | 132 | // get our Token |
ansond | 7:97ea5ef906f7 | 133 | ALLOC_BUFFER(output_buffer); |
ansond | 7:97ea5ef906f7 | 134 | char *token = this->getOauthToken(output_buffer,MAX_BUFFER_LENGTH); |
ansond | 7:97ea5ef906f7 | 135 | |
ansond | 7:97ea5ef906f7 | 136 | // fill |
ansond | 7:97ea5ef906f7 | 137 | this->fillOauthToken(token); |
ansond | 7:97ea5ef906f7 | 138 | return; |
ansond | 7:97ea5ef906f7 | 139 | } |
ansond | 7:97ea5ef906f7 | 140 | DEBUG("valid OAUTH token found."); |
ansond | 7:97ea5ef906f7 | 141 | } |
ansond | 0:518b1ca956fc | 142 | |
ansond | 0:518b1ca956fc | 143 | // |
ansond | 0:518b1ca956fc | 144 | // get OAUTH2 Token - taken from here: |
ansond | 0:518b1ca956fc | 145 | // https://developer.salesforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com#Obtaining_a_Token_in_an_Autonomous_Client_.28Username_and_Password_Flow.29 |
ansond | 0:518b1ca956fc | 146 | // |
ansond | 0:518b1ca956fc | 147 | char *SalesforceInterface::getOauthToken(char *output_buffer,int output_buffer_length) { |
ansond | 1:a7dca096e47d | 148 | if (this->haveCreds()) { |
ansond | 0:518b1ca956fc | 149 | // construct the OAUTH2 Token request body |
ansond | 0:518b1ca956fc | 150 | HTTPMap input; |
ansond | 0:518b1ca956fc | 151 | |
ansond | 0:518b1ca956fc | 152 | // |
ansond | 0:518b1ca956fc | 153 | // FORMAT: Taken from URL above method signature: |
ansond | 0:518b1ca956fc | 154 | // |
ansond | 0:518b1ca956fc | 155 | // grant_type=password&client_id=<your_client_id>&client_secret=<your_client_secret>&username=<your_username>&password=<your_password> |
ansond | 0:518b1ca956fc | 156 | // |
ansond | 0:518b1ca956fc | 157 | // ContentType: application/x-www-form-urlencoded |
ansond | 0:518b1ca956fc | 158 | // |
ansond | 0:518b1ca956fc | 159 | input.put("grant_type","password"); |
ansond | 0:518b1ca956fc | 160 | input.put("client_id",this->m_client_id); |
ansond | 0:518b1ca956fc | 161 | input.put("client_secret",this->m_client_secret); |
ansond | 0:518b1ca956fc | 162 | input.put("username",this->m_username); |
ansond | 0:518b1ca956fc | 163 | input.put("password",this->m_password); |
ansond | 1:a7dca096e47d | 164 | |
ansond | 0:518b1ca956fc | 165 | // prepare the output buffer |
ansond | 0:518b1ca956fc | 166 | HTTPText output(output_buffer,output_buffer_length); |
ansond | 0:518b1ca956fc | 167 | |
ansond | 0:518b1ca956fc | 168 | // HTTP POST call to gett he token |
ansond | 7:97ea5ef906f7 | 169 | DEBUG("Getting OAUTH Token..."); |
ansond | 7:97ea5ef906f7 | 170 | this->m_http_status = this->http()->post(SF_OAUTH_TOKEN_URL,input,&output); |
ansond | 7:97ea5ef906f7 | 171 | |
ansond | 0:518b1ca956fc | 172 | // check the result and return the token |
ansond | 7:97ea5ef906f7 | 173 | if (this->m_http_status == HTTP_OK) return output_buffer; |
ansond | 7:97ea5ef906f7 | 174 | this->logger()->log("oauth invocation failed. URL: %s",SF_OAUTH_TOKEN_URL); |
ansond | 0:518b1ca956fc | 175 | } |
ansond | 0:518b1ca956fc | 176 | else { |
ansond | 0:518b1ca956fc | 177 | // no credentials |
ansond | 0:518b1ca956fc | 178 | this->logger()->log("no/incomplete salesforce.com credentials provided. Unable to acquire OAUTH2 token..."); |
ansond | 0:518b1ca956fc | 179 | } |
ansond | 0:518b1ca956fc | 180 | return NULL; |
ansond | 0:518b1ca956fc | 181 | } |
ansond | 0:518b1ca956fc | 182 | |
ansond | 7:97ea5ef906f7 | 183 | // Salesforce.com: Get our ID |
ansond | 7:97ea5ef906f7 | 184 | char *SalesforceInterface::getSalesforceID(char *output_buffer,int output_buffer_length) { |
ansond | 7:97ea5ef906f7 | 185 | // proceed only if we have a valid OAUTH Token |
ansond | 7:97ea5ef906f7 | 186 | if (this->validOauthToken() == true) { |
ansond | 7:97ea5ef906f7 | 187 | // pull the ID from salesforce |
ansond | 7:97ea5ef906f7 | 188 | char *id = this->invoke(this->oauth()->id.c_str(),output_buffer,output_buffer_length); |
ansond | 7:97ea5ef906f7 | 189 | |
ansond | 7:97ea5ef906f7 | 190 | // log any error status and return what we have... |
ansond | 7:97ea5ef906f7 | 191 | if (this->httpStatus() != HTTP_OK) this->logger()->log("Unable to get Salesforce ID: status=%d httpCode=%d",this->httpStatus(),this->httpResponseCode()); |
ansond | 7:97ea5ef906f7 | 192 | return id; |
ansond | 7:97ea5ef906f7 | 193 | } |
ansond | 7:97ea5ef906f7 | 194 | else { |
ansond | 7:97ea5ef906f7 | 195 | // unable to get ID - no OAUTH token |
ansond | 7:97ea5ef906f7 | 196 | this->logger()->log("Unable to get Salesforce ID: no valid OAUTH token."); |
ansond | 7:97ea5ef906f7 | 197 | } |
ansond | 7:97ea5ef906f7 | 198 | return NULL; |
ansond | 7:97ea5ef906f7 | 199 | } |
ansond | 7:97ea5ef906f7 | 200 | |
ansond | 0:518b1ca956fc | 201 | // Salesforce.com Invoke: defaults to GET |
ansond | 7:97ea5ef906f7 | 202 | char *SalesforceInterface::invoke(const char *url,char *output_buffer,int output_buffer_len) { |
ansond | 0:518b1ca956fc | 203 | return this->invoke(url,NUM_TYPES,NULL,0,output_buffer,output_buffer_len,GET); |
ansond | 0:518b1ca956fc | 204 | } |
ansond | 0:518b1ca956fc | 205 | |
ansond | 0:518b1ca956fc | 206 | // Salesforce.com Invoke: defaults to POST with JSON input data type |
ansond | 7:97ea5ef906f7 | 207 | char *SalesforceInterface::invoke(const char *url,const char *input_data,const int input_data_len,char *output_buffer,int output_buffer_len) { |
ansond | 0:518b1ca956fc | 208 | return this->invoke(url,JSON,input_data,input_data_len,output_buffer,output_buffer_len); |
ansond | 0:518b1ca956fc | 209 | } |
ansond | 0:518b1ca956fc | 210 | |
ansond | 0:518b1ca956fc | 211 | // Salesforce.com Invoke: defaults to POST with variable input data type |
ansond | 7:97ea5ef906f7 | 212 | char *SalesforceInterface::invoke(const char *url,const InputDataTypes input_type,const char *input_data,const int input_data_len,char *output_buffer,int output_buffer_len) { |
ansond | 0:518b1ca956fc | 213 | return this->invoke(url,input_type,input_data,input_data_len,output_buffer,output_buffer_len,POST); |
ansond | 0:518b1ca956fc | 214 | } |
ansond | 0:518b1ca956fc | 215 | |
ansond | 0:518b1ca956fc | 216 | // Salesforce.com Invoke: full fidelity method |
ansond | 7:97ea5ef906f7 | 217 | char *SalesforceInterface::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) { |
ansond | 7:97ea5ef906f7 | 218 | // initialize our invocation status and response code |
ansond | 7:97ea5ef906f7 | 219 | this->m_http_response_code = -1; |
ansond | 7:97ea5ef906f7 | 220 | this->m_http_status = HTTP_ERROR; |
ansond | 7:97ea5ef906f7 | 221 | |
ansond | 7:97ea5ef906f7 | 222 | // param check: make sure that we at least have an output buffer and URL |
ansond | 7:97ea5ef906f7 | 223 | if (url != NULL && strlen(url) > 0 && output_buffer != NULL && output_buffer_len > 0) { |
ansond | 7:97ea5ef906f7 | 224 | // proceed only if we have a valid OAUTH Token |
ansond | 7:97ea5ef906f7 | 225 | if (this->validOauthToken() == true) { |
ansond | 7:97ea5ef906f7 | 226 | // use OAUTH headers |
ansond | 7:97ea5ef906f7 | 227 | this->http()->oauthToken(this->oauth()->access_token.c_str()); |
ansond | 0:518b1ca956fc | 228 | |
ansond | 7:97ea5ef906f7 | 229 | // reset the redirection url buffer in case we get a redirect... |
ansond | 7:97ea5ef906f7 | 230 | RESET_BUFFER(this->m_http_redirection_url); |
ansond | 7:97ea5ef906f7 | 231 | this->http()->setLocationBuf((char *)this->m_http_redirection_url,MAX_BUFFER_LENGTH); |
ansond | 7:97ea5ef906f7 | 232 | |
ansond | 0:518b1ca956fc | 233 | // create our output/response buffer |
ansond | 0:518b1ca956fc | 234 | HTTPText output(output_buffer,output_buffer_len); |
ansond | 0:518b1ca956fc | 235 | |
ansond | 0:518b1ca956fc | 236 | // now make the HTTP(S) request |
ansond | 0:518b1ca956fc | 237 | switch(verb) { |
ansond | 0:518b1ca956fc | 238 | case GET: |
ansond | 7:97ea5ef906f7 | 239 | DEBUG("invoking(GET) URL: %s...",url); |
ansond | 7:97ea5ef906f7 | 240 | this->m_http_status = this->http()->get(url,&output); |
ansond | 7:97ea5ef906f7 | 241 | this->m_http_response_code = this->http()->getHTTPResponseCode(); |
ansond | 0:518b1ca956fc | 242 | break; |
ansond | 0:518b1ca956fc | 243 | case DELETE: |
ansond | 7:97ea5ef906f7 | 244 | DEBUG("invoking(DEL) URL: %s...",url); |
ansond | 7:97ea5ef906f7 | 245 | this->m_http_status = this->http()->del(url,&output); |
ansond | 7:97ea5ef906f7 | 246 | this->m_http_response_code = this->http()->getHTTPResponseCode(); |
ansond | 0:518b1ca956fc | 247 | break; |
ansond | 0:518b1ca956fc | 248 | case POST: |
ansond | 0:518b1ca956fc | 249 | if (input_data != NULL && input_data_len > 0) { |
ansond | 0:518b1ca956fc | 250 | if (input_type == JSON) { |
ansond | 7:97ea5ef906f7 | 251 | DEBUG("invoking(POST-JSON) URL: %s...",url); |
ansond | 7:97ea5ef906f7 | 252 | HTTPJson input_json((char *)input_data,(int)input_data_len); |
ansond | 7:97ea5ef906f7 | 253 | this->m_http_status = this->http()->post(url,input_json,&output); |
ansond | 7:97ea5ef906f7 | 254 | this->m_http_response_code = this->http()->getHTTPResponseCode(); |
ansond | 0:518b1ca956fc | 255 | } |
ansond | 0:518b1ca956fc | 256 | else { |
ansond | 7:97ea5ef906f7 | 257 | DEBUG("invoking(POST-TEXT) URL: %s...",url); |
ansond | 7:97ea5ef906f7 | 258 | HTTPText input_text((char *)input_data,(int)input_data_len); |
ansond | 7:97ea5ef906f7 | 259 | this->m_http_status = this->http()->post(url,input_text,&output); |
ansond | 7:97ea5ef906f7 | 260 | this->m_http_response_code = this->http()->getHTTPResponseCode(); |
ansond | 0:518b1ca956fc | 261 | } |
ansond | 0:518b1ca956fc | 262 | } |
ansond | 0:518b1ca956fc | 263 | else { |
ansond | 0:518b1ca956fc | 264 | // no input buffer! |
ansond | 0:518b1ca956fc | 265 | this->logger()->log("invoke: ERROR HTTP(POST) requested but no input data provided... returning NULL"); |
ansond | 0:518b1ca956fc | 266 | } |
ansond | 0:518b1ca956fc | 267 | break; |
ansond | 0:518b1ca956fc | 268 | case PUT: |
ansond | 0:518b1ca956fc | 269 | if (input_data != NULL && input_data_len > 0) { |
ansond | 0:518b1ca956fc | 270 | if (input_type == JSON) { |
ansond | 7:97ea5ef906f7 | 271 | DEBUG("invoking(PUT-JSON) URL: %s...",url); |
ansond | 7:97ea5ef906f7 | 272 | HTTPJson input_json((char *)input_data,(int)input_data_len); |
ansond | 7:97ea5ef906f7 | 273 | this->m_http_status = this->http()->put(url,input_json,&output); |
ansond | 7:97ea5ef906f7 | 274 | this->m_http_response_code = this->http()->getHTTPResponseCode(); |
ansond | 0:518b1ca956fc | 275 | } |
ansond | 0:518b1ca956fc | 276 | else { |
ansond | 7:97ea5ef906f7 | 277 | DEBUG("invoking(PUT-TEXT) URL: %s...",url); |
ansond | 7:97ea5ef906f7 | 278 | HTTPText input_text((char *)input_data,(int)input_data_len); |
ansond | 7:97ea5ef906f7 | 279 | this->m_http_status = this->http()->put(url,input_text,&output); |
ansond | 7:97ea5ef906f7 | 280 | this->m_http_response_code = this->http()->getHTTPResponseCode(); |
ansond | 0:518b1ca956fc | 281 | } |
ansond | 0:518b1ca956fc | 282 | } |
ansond | 0:518b1ca956fc | 283 | else { |
ansond | 0:518b1ca956fc | 284 | // no input buffer! |
ansond | 0:518b1ca956fc | 285 | this->logger()->log("invoke: ERROR HTTP(PUT) requested but no input data provided... returning NULL"); |
ansond | 0:518b1ca956fc | 286 | } |
ansond | 0:518b1ca956fc | 287 | break; |
ansond | 0:518b1ca956fc | 288 | default: |
ansond | 0:518b1ca956fc | 289 | // invalid HTTP verb |
ansond | 0:518b1ca956fc | 290 | this->logger()->log("invoke: ERROR invalid HTTP verb (%d) provided... returning NULL",verb); |
ansond | 0:518b1ca956fc | 291 | break; |
ansond | 0:518b1ca956fc | 292 | } |
ansond | 0:518b1ca956fc | 293 | } |
ansond | 7:97ea5ef906f7 | 294 | else { |
ansond | 7:97ea5ef906f7 | 295 | // no OAUTH Token |
ansond | 7:97ea5ef906f7 | 296 | this->logger()->log("unable to acquire OAUTH token for credentials provided. Unable to invoke API..."); |
ansond | 7:97ea5ef906f7 | 297 | } |
ansond | 0:518b1ca956fc | 298 | } |
ansond | 0:518b1ca956fc | 299 | else { |
ansond | 0:518b1ca956fc | 300 | // no credentials |
ansond | 0:518b1ca956fc | 301 | this->logger()->log("no/incomplete salesforce.com credentials provided. Unable to invoke API..."); |
ansond | 0:518b1ca956fc | 302 | } |
ansond | 0:518b1ca956fc | 303 | |
ansond | 7:97ea5ef906f7 | 304 | // process any return results that we have |
ansond | 7:97ea5ef906f7 | 305 | if (this->httpStatus() == HTTP_OK || this->httpStatus() == HTTP_REDIRECT) { |
ansond | 7:97ea5ef906f7 | 306 | // do we have any redirections? |
ansond | 7:97ea5ef906f7 | 307 | if (this->httpResponseCode() == 302 /* REDIRECT */ && strlen(this->m_http_redirection_url) > 0) { |
ansond | 7:97ea5ef906f7 | 308 | // we have a redirect - so reset the output buffer |
ansond | 7:97ea5ef906f7 | 309 | memset(output_buffer,0,output_buffer_len); |
ansond | 7:97ea5ef906f7 | 310 | |
ansond | 7:97ea5ef906f7 | 311 | // we have to make a copy of the redirection URL - this is because the subsequent invoke() will wipe our current one clean |
ansond | 7:97ea5ef906f7 | 312 | ALLOC_BUFFER(redirect_url); |
ansond | 7:97ea5ef906f7 | 313 | strcpy(redirect_url,this->m_http_redirection_url); |
ansond | 7:97ea5ef906f7 | 314 | |
ansond | 7:97ea5ef906f7 | 315 | // repeat with the redirection URL |
ansond | 7:97ea5ef906f7 | 316 | DEBUG("invoke: redirecting to: %s",redirect_url); |
ansond | 7:97ea5ef906f7 | 317 | return this->invoke((const char *)redirect_url,input_type,input_data,input_data_len,output_buffer,output_buffer_len,verb); |
ansond | 7:97ea5ef906f7 | 318 | } |
ansond | 7:97ea5ef906f7 | 319 | else if (this->httpResponseCode() == 302 /* REDIRECT */) { |
ansond | 7:97ea5ef906f7 | 320 | // error - got a redirect but have no URL |
ansond | 7:97ea5ef906f7 | 321 | this->logger()->log("invoke error: received redirect but no URL..."); |
ansond | 7:97ea5ef906f7 | 322 | this->m_http_status = HTTP_ERROR; |
ansond | 7:97ea5ef906f7 | 323 | } |
ansond | 7:97ea5ef906f7 | 324 | } |
ansond | 7:97ea5ef906f7 | 325 | |
ansond | 0:518b1ca956fc | 326 | // return the response in the output buffer |
ansond | 7:97ea5ef906f7 | 327 | if (this->httpStatus() == HTTP_OK) return output_buffer; |
ansond | 7:97ea5ef906f7 | 328 | else this->logger()->log("invocation failed with HTTP error code=%d status=%d",this->httpResponseCode(),this->httpStatus()); |
ansond | 0:518b1ca956fc | 329 | return NULL; |
ansond | 0:518b1ca956fc | 330 | } |
ansond | 0:518b1ca956fc | 331 |