def query_more(self, next_records_identifier, identifier_is_url=False, **kwargs): """Retrieves more results from a query that returned more results than the batch maximum. Returns a dict decoded from the Salesforce response JSON payload. Arguments: * next_records_identifier -- either the Id of the next Salesforce object in the result, or a URL to the next record in the result. * identifier_is_url -- True if `next_records_identifier` should be treated as a URL, False if `next_records_identifer` should be treated as an Id. """ if identifier_is_url: # Don't use `self.base_url` here because the full URI is provided url = ('https://{instance}{next_record_url}' .format(instance=self.sf_instance, next_record_url=next_records_identifier)) else: url = self.base_url + 'query/{next_record_id}' url = url.format(next_record_id=next_records_identifier) result = self.request.get(url, headers=self.headers, **kwargs) if result.status_code != 200: _exception_handler(result) return result.json(object_pairs_hook=OrderedDict)
def test_generic_error_code(self): """Test an error code that is otherwise not caught""" self.mockresult.status_code = 500 with self.assertRaises(SalesforceGeneralError) as cm: _exception_handler(self.mockresult) self.assertEqual(str(cm.exception), ("Error Code 500. Response content" ": Example Content"))
def query_more(self, next_records_identifier, identifier_is_url=False, **kwargs): """Retrieves more results from a query that returned more results than the batch maximum. Returns a dict decoded from the Salesforce response JSON payload. Arguments: * next_records_identifier -- either the Id of the next Salesforce object in the result, or a URL to the next record in the result. * identifier_is_url -- True if `next_records_identifier` should be treated as a URL, False if `next_records_identifer` should be treated as an Id. """ if identifier_is_url: # Don't use `self.base_url` here because the full URI is provided url = ('https://{instance}{next_record_url}'.format( instance=self.sf_instance, next_record_url=next_records_identifier)) else: url = self.base_url + 'query/{next_record_id}' url = url.format(next_record_id=next_records_identifier) result = self.request.get(url, headers=self.headers, **kwargs) if result.status_code != 200: _exception_handler(result) return result.json(object_pairs_hook=OrderedDict)
def test_generic_error_code(self): """Test an error code that is otherwise not caught""" self.mockresult.status_code = 500 with self.assertRaises(SalesforceGeneralError) as cm: _exception_handler(self.mockresult) self.assertEqual(str(cm.exception), ('Error Code 500. Response content' ': Example Content'))
def test_deprecated_exception_handler(self): """Test calling deprecated exception handler""" self.mockresult.status_code = 404 resource_name = 'SpecialContacts' with self.assertRaises(SalesforceResourceNotFound) as exception: _exception_handler(result=self.mockresult, name=resource_name) self.assertEqual(exception.exception.status, 404) self.assertEqual(exception.exception.resource_name, resource_name)
def test_resource_not_found(self): """Test resource not found (404 code)""" self.mockresult.status_code = 404 with self.assertRaises(SalesforceResourceNotFound) as cm: _exception_handler(self.mockresult, 'SpecialContacts') self.assertEqual(str(cm.exception), ('Resource SpecialContacts Not' ' Found. Response content: Example Content'))
def test_resource_not_found(self): """Test resource not found (404 code)""" self.mockresult.status_code = 404 with self.assertRaises(SalesforceResourceNotFound) as cm: _exception_handler(self.mockresult, 'SpecialContacts') self.assertEqual(str(cm.exception), ( 'Resource SpecialContacts Not' ' Found. Response content: Example Content'))
def test_request_refused(self): """Test a refused request (403 code)""" self.mockresult.status_code = 403 with self.assertRaises(SalesforceRefusedRequest) as cm: _exception_handler(self.mockresult) self.assertEqual(str(cm.exception), ( 'Request refused for ' 'http://www.example.com/. Response content: Example Content'))
def test_expired_session(self): """Test an expired session (401 code)""" self.mockresult.status_code = 401 with self.assertRaises(SalesforceExpiredSession) as cm: _exception_handler(self.mockresult) self.assertEqual(str(cm.exception), ( 'Expired session for ' 'http://www.example.com/. Response content: Example Content'))
def test_malformed_request(self): """Test a malformed request (400 code)""" self.mockresult.status_code = 400 with self.assertRaises(SalesforceMalformedRequest) as cm: _exception_handler(self.mockresult) self.assertEqual(str(cm.exception), ( 'Malformed request ' 'http://www.example.com/. Response content: Example Content'))
def test_multiple_records_returned(self): """Test multiple records returned (a 300 code)""" self.mockresult.status_code = 300 with self.assertRaises(SalesforceMoreThanOneRecord) as cm: _exception_handler(self.mockresult) self.assertEqual(str(cm.exception), ( 'More than one record for ' 'http://www.example.com/. Response content: Example Content'))
def _call_salesforce(self, method, url, **kwargs): """Utility method for performing HTTP call to Salesforce. Returns a `requests.result` object. """ headers = { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + self.session_id, 'X-PrettyPrint': '1' } result = self.request.request(method, url, headers=headers, **kwargs) if result.status_code >= 300: _exception_handler(result, self.name) return result
def query(self, query, **kwargs): """Return the result of a Salesforce SOQL query as a dict decoded from the Salesforce response JSON payload. Arguments: * query -- the SOQL query to send to Salesforce, e.g. `SELECT Id FROM Lead WHERE Email = "*****@*****.**"` """ url = self.base_url + 'query/' params = {'q': query} # `requests` will correctly encode the query string passed as `params` result = self.request.get(url, headers=self.headers, params=params, **kwargs) if result.status_code != 200: _exception_handler(result) return result.json(object_pairs_hook=OrderedDict)