def query(self, query_string):
        query_url = self.url_resources.get_full_resource_url(
            self.auth.instance_url,
            ResourcesName.get_resource_name("query"))

        params = {'q': query_string}
        return self.get(query_url, params)
    def search(self, search_string):
        search_url = self.url_resources.get_full_resource_url(
            self.auth.instance_url, ResourcesName.get_resource_name("search"))

        params = {'q': search_string}

        return self.get(search_url, params)
    def query(self, query_string):
        query_url = self.url_resources.get_full_resource_url(
            self.auth.instance_url,
            ResourcesName.get_resource_name("query"))

        params = {'q': query_string}
        return self.get(query_url, params)
    def delete(self, record_id):
        delete_url = '{0}/{1}'.format(
            self.url_resources.get_resource_sobject_url(
                self.auth.instance_url,
                ResourcesName.get_resource_name("sobject"), self.__name),
            record_id)

        return self.__send_request('DELETE', delete_url)
    def search(self, search_string):
        search_url = self.url_resources.get_full_resource_url(
            self.auth.instance_url,
            ResourcesName.get_resource_name("search"))

        params = {'q': search_string}

        return self.get(search_url, params)
    def get(self, url=None, params=None):
        get_url = self.url_resources.get_resource_sobject_url(
            self.auth.instance_url, ResourcesName.get_resource_name("sobject"),
            self.__name)

        if url is not None:
            get_url += url

        return self.__send_request('GET', get_url, params=params)
    def post(self, data, record_id=None):
        post_url = self.url_resources.get_resource_sobject_url(
            self.auth.instance_url, ResourcesName.get_resource_name("sobject"),
            self.__name)

        if record_id is not None:
            post_url += '/' + record_id

        return self.__send_request('POST', post_url, data=json.dumps(data))
    def delete(self, record_id):
        delete_url = '{0}/{1}'.format(
            self.url_resources.get_resource_sobject_url(
                self.auth.instance_url,
                ResourcesName.get_resource_name("sobject"),
                self.__name),
            record_id)

        return self.__send_request('DELETE',
                                   delete_url)
    def query_more(self, url):
        query_url = self.url_resources.get_full_resource_url(
            self.auth.instance_url, ResourcesName.get_resource_name("query"))

        if url.startswith(query_url, len(self.auth.instance_url)):
            get_url = '{0}/{1}'.format(self.auth.instance_url, url)
        else:
            if url[0] == '/':
                get_url = '{0}{1}'.format(self.auth.instance_url, url)
            else:
                get_url = '{0}/{1}'.format(query_url, url)
        return self.get(get_url)
    def query_more(self, url):
        query_url = self.url_resources.get_full_resource_url(
            self.auth.instance_url,
            ResourcesName.get_resource_name("query"))

        if url.startswith(query_url,
                          len(self.auth.instance_url)):
            get_url = '{0}/{1}'.format(self.auth.instance_url, url)
        else:
            get_url = '{0}/{1}'.format(query_url, url)

        return self.get(get_url)
    def post(self, data, record_id=None):
        post_url = self.url_resources.get_resource_sobject_url(
            self.auth.instance_url,
            ResourcesName.get_resource_name("sobject"),
            self.__name)

        if record_id is not None:
            post_url += '/' + record_id

        return self.__send_request('POST',
                                   post_url,
                                   data=json.dumps(data))
    def get(self, url=None, params=None):
        get_url = self.url_resources.get_resource_sobject_url(
            self.auth.instance_url,
            ResourcesName.get_resource_name("sobject"),
            self.__name)

        if url is not None:
            get_url += url

        return self.__send_request('GET',
                                   get_url,
                                   params=params)
    def get(self, url=None, params=None):
        get_url = self.url_resources.get_resource_sobject_url(
            self.auth.instance_url, ResourcesName.get_resource_name("sobject"),
            self.__name)

        if url is not None:
            # Case of direct use of RestSObject and url is a resource_id
            if not (url.startswith('http://')
                    or url.startswith('https://')) and url[0] != '/':
                url = '/' + url
            get_url += url

        return self.__send_request('GET', get_url, params=params)
    def query_more(self, url):
        query_url = self.url_resources.get_full_resource_url(
            self.auth.instance_url, ResourcesName.get_resource_name("query"))

        query_key = url.split("/")[5]

        if query_key != None:
            get_url = '{0}{1}'.format(query_url, query_key)
        elif url.startswith(query_url, len(self.auth.instance_url)):
            get_url = '{0}/{1}'.format(self.auth.instance_url, url)
        else:
            get_url = '{0}/{1}'.format(query_url, url)

        return self.get(get_url)
    def get(self, url=None, params=None):
        get_url = self.url_resources.get_resource_sobject_url(
            self.auth.instance_url,
            ResourcesName.get_resource_name("sobject"),
            self.__name)

        if url is not None:
            # Case of direct use of RestSObject and url is a resource_id
            if not (url.startswith('http://') or url.startswith('https://')) and url[0] != '/':
                url = '/' + url
            get_url += url

        return self.__send_request('GET',
                                   get_url,
                                   params=params)
    def update(self, data):
        if not isinstance(data, list):
            raise TypeError("'update' require a parameter type 'list'")

        record_id = data[0]
        records = data[1]

        update_url = '{0}/{1}'.format(
            self.url_resources.get_resource_sobject_url(
                self.auth.instance_url,
                ResourcesName.get_resource_name("sobject"), self.__name),
            record_id)

        return self.__send_request('PATCH',
                                   update_url,
                                   data=json.dumps(records))
    def update(self, data):
        if not isinstance(data, list):
            raise TypeError("'update' require a parameter type 'list'")

        record_id = data[0]
        records = data[1]

        update_url = '{0}/{1}'.format(
            self.url_resources.get_resource_sobject_url(
                self.auth.instance_url,
                ResourcesName.get_resource_name("sobject"),
                self.__name),
            record_id)

        return self.__send_request('PATCH',
                                   update_url,
                                   data=json.dumps(records))
    def query_all(self, query_string):
        query_url = self.url_resources.get_full_resource_url(
            self.auth.instance_url,
            ResourcesName.get_resource_name("queryAll"))

        params = {'q': query_string}
        resp = self.get(query_url, params)

        def do_query_all(response):
            if response['done'] or not response['nextRecordsUrl']:
                return response
            else:
                result = self.query_more(response['nextRecordsUrl'])

                response['done'] = result['done']
                response['nextRecordsUrl'] = result.get('nextRecordsUrl')
                response['totalSize'] += result['totalSize']
                response['records'].extend(result['records'])
                return do_query_all(response)

        return do_query_all(resp)
    def query_all(self, query_string):
        query_url = self.url_resources.get_full_resource_url(
            self.auth.instance_url,
            ResourcesName.get_resource_name("queryAll"))

        params = {'q': query_string}
        resp = self.get(query_url, params)

        def do_query_all(response):
            if response['done']:
                return response
            else:
                result = self.query_more(response['nextRecordsUrl'])

                response['done'] = result['done']
                response['totalSize'] += result['totalSize']
                response['records'].extend(result['records'])

                return do_query_all(response)

        return do_query_all(resp)
    def query_limits(self):
        limits_url = self.url_resources.get_full_resource_url(
            self.auth.instance_url, ResourcesName.get_resource_name("limits"))

        return self.get(limits_url)
    def query_limits(self):
        limits_url = self.url_resources.get_full_resource_url(
            self.auth.instance_url,
            ResourcesName.get_resource_name("limits"))

        return self.get(limits_url)