Ejemplo n.º 1
0
    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_identifier` should be treated as
                               an Id.
        """
        if identifier_is_url:
            # Don't use `self.base_url` here because the full URI is provided
            url = (u'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._call_salesforce('GET', url, **kwargs)

        if result.status_code != 200:
            exception_handler(result)

        return result.json(object_pairs_hook=OrderedDict)
Ejemplo n.º 2
0
    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'))
Ejemplo n.º 3
0
    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'))
Ejemplo n.º 4
0
    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'))
Ejemplo n.º 5
0
    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'))
Ejemplo n.º 6
0
    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'))
Ejemplo n.º 7
0
    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'))
Ejemplo n.º 8
0
    def _call_salesforce(self, method, url, **kwargs):
        """Utility method for performing HTTP call to Salesforce.

        Returns a `requests.result` object.
        """
        result = self.session.request(method,
                                      url,
                                      headers=self.headers,
                                      **kwargs)

        if result.status_code >= 300:
            exception_handler(result)

        sforce_limit_info = result.headers.get('Sforce-Limit-Info')
        if sforce_limit_info:
            self.api_usage = self.parse_api_usage(sforce_limit_info)

        return result
Ejemplo n.º 9
0
    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._call_salesforce('GET', url, params=params, **kwargs)

        if result.status_code != 200:
            exception_handler(result)

        return result.json(object_pairs_hook=OrderedDict)
Ejemplo n.º 10
0
    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'
        }
        additional_headers = kwargs.pop('headers', dict())
        headers.update(additional_headers or dict())
        result = self.session.request(method, url, headers=headers, **kwargs)

        if result.status_code >= 300:
            exception_handler(result, self.name)

        sforce_limit_info = result.headers.get('Sforce-Limit-Info')
        if sforce_limit_info:
            self.api_usage = Salesforce.parse_api_usage(sforce_limit_info)

        return result