How to use the pyravendb.commands.raven_commands.RavenCommand function in pyravendb

To help you get started, we’ve selected a few pyravendb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ravendb / ravendb-python-client / pyravendb / commands / raven_commands.py View on Github external
def create_request(self, server_node):
        if self._database_name is None:
            self.url = "{0}/info/tcp?tag={1}".format(server_node.url, self._tag)
        else:
            self.url = "{0}/databases/{1}/info/tcp?tag={2}".format(server_node.url, self._database_name, self._tag)

        self.requested_node = server_node

    def set_response(self, response):
        if response is None:
            raise exceptions.ErrorResponseException("Invalid response")

        return response.json()


class QueryStreamCommand(RavenCommand):
    def __init__(self, index_query):
        super(QueryStreamCommand, self).__init__(method="POST", is_read_request=True, use_stream=True)
        if not index_query:
            raise ValueError("index_query cannot be None")

        if index_query.wait_for_non_stale_results:
            raise exceptions.NotSupportedException("Since stream() does not wait for indexing (by design), "
                                                   "streaming query with wait_for_non_stale_results is not supported.")

        self._index_query = index_query

    def create_request(self, server_node):
        self.url = "{0}/databases/{1}/streams/queries".format(server_node.url, server_node.database)
        self.data = self._index_query.to_json()

    def set_response(self, response):
github ravendb / ravendb-python-client / pyravendb / commands / raven_commands.py View on Github external
class GetClusterTopologyCommand(RavenCommand):
    def __init__(self):
        super(GetClusterTopologyCommand, self).__init__(method="GET", is_read_request=True)

    def create_request(self, server_node):
        self.url = "{0}/cluster/topology".format(server_node.url)

    def set_response(self, response):
        if response.status_code == 200:
            return response.json()
        if response.status_code == 400:
            log.debug(response.json()["Error"])
        return None


class GetOperationStateCommand(RavenCommand):
    def __init__(self, operation_id, is_server_store_operation=False):
        super(GetOperationStateCommand, self).__init__(method="GET")
        self.operation_id = operation_id
        self.is_server_store_operation = is_server_store_operation

    def create_request(self, server_node):
        self.url = "{0}/databases/{1}/operations/state?id={2}".format(server_node.url, server_node.database,
                                                                      self.operation_id) if not self.is_server_store_operation \
            else "{0}/operations/state?id={2}".format(server_node.url, self.operation_id)

    def set_response(self, response):
        try:
            response = response.json()
        except ValueError:
            raise response.raise_for_status()
        return response
github ravendb / ravendb-python-client / pyravendb / raven_operations / server_operations.py View on Github external
return GetDatabaseRecordCommand(self._database_name)


# --------------------CertificateOperation------------------------------

class GetCertificateOperation(ServerOperation):
    def __init__(self, start, page_size):

        super(GetCertificateOperation, self).__init__()
        self._start = start
        self._page_size = page_size

    def get_command(self, conventions):
        return self._GetCertificateCommand(self._start, self._page_size)

    class _GetCertificateCommand(RavenCommand):
        def __init__(self, start, page_size):
            super(GetCertificateOperation._GetCertificateCommand, self).__init__(method="GET")
            self._start = start
            self._page_size = page_size

        def create_request(self, server_node):
            self.url = "{0}/admin/certificates?start={1}&pageSize={2}".format(server_node.url, self._start,
                                                                              self._page_size)

        def set_response(self, response):
            if response is None:
                raise ValueError("response is invalid.")
            data = {}
            try:
                response = response.json()["Results"]
                if len(response) > 1:
github ravendb / ravendb-python-client / pyravendb / commands / raven_commands.py View on Github external
if self._index_entries_only:
            self.url += "&debug=entries"

        self.data = self._index_query.to_json()

    def set_response(self, response):
        if response is None:
            return

        response = response.json()
        if "Error" in response:
            raise exceptions.ErrorResponseException(response["Error"])
        return response


class GetStatisticsCommand(RavenCommand):
    def __init__(self, debug_tag=None):
        super(GetStatisticsCommand, self).__init__(method="GET")
        self.debug_tag = debug_tag

    def create_request(self, server_node):
        self.url = "{0}/databases/{1}/stats".format(server_node.url, server_node.database)
        if self.debug_tag:
            self.url += "?" + self.debug_tag

    def set_response(self, response):
        if response and response.status_code == 200:
            return response.json()
        return None


class GetTopologyCommand(RavenCommand):
github ravendb / ravendb-python-client / pyravendb / commands / raven_commands.py View on Github external
if response is None:
            raise exceptions.ErrorResponseException("Invalid response")
        if response.status_code != 200:
            try:
                json_response = response.json()
                if "Error" in json_response:
                    raise Exception(json_response["Error"])
                return response
            except ValueError:
                raise response.raise_for_status()
        return response


# ------------------------SubscriptionCommands----------------------

class CreateSubscriptionCommand(RavenCommand):
    def __init__(self, options):
        """
        @param SubscriptionCreationOptions options: Subscription options
        """

        super(CreateSubscriptionCommand, self).__init__(method="PUT")
        self._options = options

    def create_request(self, server_node):
        self.url = "{0}/databases/{1}/subscriptions".format(server_node.url, server_node.database)
        self.data = self._options.to_json()

    def set_response(self, response):
        try:
            response = response.json()
            if "Error" in response:
github ravendb / ravendb-python-client / pyravendb / commands / raven_commands.py View on Github external
self.timeout = self._query.wait_for_non_stale_results_timeout + timedelta(seconds=10)

    def create_request(self, server_node):
        if self._query.facet_setup_doc and len(self._query.facets) > 0:
            raise exceptions.InvalidOperationException("You cannot specify both 'facet_setup_doc' and 'facets'.")

        self.url = "{0}/databases/{1}/queries?op=facets&query-hash={2}".format(server_node.url, server_node.database,
                                                                               self._query.get_query_hash())
        self.data = self._query.to_json()

    def set_response(self, response):
        if response.status_code == 200:
            return response.json()


class MultiGetCommand(RavenCommand):
    def __init__(self, requests):
        """
        @param requests: The requests for the server
        :type list of dict
        dict keys:
            url = Request url (relative).
            headers =  Request headers.
            query = Query information e.g. "?pageStart=10&pageSize=20".
            data = The data for the requests body
            method = The requests method e.g.
        """
        super(MultiGetCommand, self).__init__(method="POST")
        self._requests = requests
        self._base_url = None

    def create_request(self, server_node):
github ravendb / ravendb-python-client / pyravendb / raven_operations / server_operations.py View on Github external
def set_response(self, response):
            pass


class DeleteCertificateOperation(ServerOperation):
    def __init__(self, thumbprint):
        if thumbprint is None:
            raise ValueError("certificate cannot be None")
        super(DeleteCertificateOperation, self).__init__()
        self._thumbprint = thumbprint

    def get_command(self, conventions):
        return self._DeleteCertificateCommand(self._thumbprint)

    class _DeleteCertificateCommand(RavenCommand):
        def __init__(self, thumbprint):
            if thumbprint is None:
                raise ValueError("certificate cannot be None")
            super(DeleteCertificateOperation._DeleteCertificateCommand, self).__init__(method="DELETE")
            self._thumbprint = thumbprint

        def create_request(self, server_node):
            self.url = "{0}/admin/certificates?thumbprint={1}".format(server_node.url,
                                                                      Utils.quote_key(self._thumbprint))

        def set_response(self, response):
            pass
github ravendb / ravendb-python-client / pyravendb / commands / raven_commands.py View on Github external
"Content": request.get("data", None), "Headers": headers})

        self.data = {"Requests": commands}
        self.url = "{0}/multi_get".format(self._base_url)

    def set_response(self, response):
        try:
            response = response.json()
            if "Error" in response:
                raise exceptions.ErrorResponseException(response["Error"])
            return response["Results"]
        except:
            raise exceptions.ErrorResponseException("Invalid response")


class GetDatabaseRecordCommand(RavenCommand):
    def __init__(self, database_name):
        super(GetDatabaseRecordCommand, self).__init__(method="GET")
        self._database_name = database_name

    def create_request(self, server_node):
        self.url = "{0}/admin/databases?name={1}".format(server_node.url, self._database_name)

    def set_response(self, response):
        try:
            response = response.json()
            if "Error" in response:
                raise exceptions.ErrorResponseException(response["Error"])
            return response["Topology"]
        except:
            raise response.raise_for_status()
github ravendb / ravendb-python-client / pyravendb / commands / raven_commands.py View on Github external
def set_response(self, response):
        if response is None:
            return None
        try:
            response = response.json()
            if "Error" in response:
                raise exceptions.ErrorResponseException(response["Error"])
        except ValueError:
            raise exceptions.ErrorResponseException(
                "Failed to load document from the database please check the connection to the server")

        return response


class DeleteDocumentCommand(RavenCommand):
    def __init__(self, key, change_vector=None):
        super(DeleteDocumentCommand, self).__init__(method="DELETE")
        self.key = key
        self.change_vector = change_vector

    def create_request(self, server_node):
        if self.key is None:
            raise ValueError("None Key is not valid")
        if not isinstance(self.key, str):
            raise ValueError("key must be {0}".format(type("")))

        if self.change_vector is not None:
            self.headers = {"If-Match": "\"{0}\"".format(self.change_vector)}

        self.url = "{0}/databases/{1}/docs?id={2}".format(server_node.url, server_node.database,
                                                          Utils.quote_key(self.key))
github ravendb / ravendb-python-client / pyravendb / commands / raven_commands.py View on Github external
self.is_server_store_operation = is_server_store_operation

    def create_request(self, server_node):
        self.url = "{0}/databases/{1}/operations/state?id={2}".format(server_node.url, server_node.database,
                                                                      self.operation_id) if not self.is_server_store_operation \
            else "{0}/operations/state?id={2}".format(server_node.url, self.operation_id)

    def set_response(self, response):
        try:
            response = response.json()
        except ValueError:
            raise response.raise_for_status()
        return response


class PutAttachmentCommand(RavenCommand):
    def __init__(self, document_id, name, stream, content_type, change_vector):
        """
        @param document_id: The id of the document
        @param name: Name of the attachment
        @param stream: The attachment as bytes (ex.open("file_path", "rb"))
        @param content_type: The type of the attachment (ex.image/png)
        @param change_vector: The change vector of the document
        """

        if not document_id:
            raise ValueError("Invalid document_id")
        if not name:
            raise ValueError("Invalid name")

        super(PutAttachmentCommand, self).__init__(method="PUT")
        self._document_id = document_id