Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions pydgraph/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,58 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Dgraph python client."""

import random

from pydgraph import txn, util
from pydgraph.meta import VERSION
from pydgraph.proto import api_pb2 as api

__author__ = 'Mohit Ranka <mohitranka@gmail.com>'
__maintainer__ = 'Garvit Pahal <garvit@dgraph.io>'
__maintainer__ = 'Martin Martinez Rivera <martinmr@dgraph.io>'
__version__ = VERSION
__status__ = 'development'


class DgraphClient(object):
"""Creates a new Client for interacting with the Dgraph store.

The client can be backed by multiple connections (to the same server, or
multiple servers in a cluster).
"""

def __init__(self, *clients):
if len(clients) == 0:
if not clients:
raise ValueError('No clients provided in DgraphClient constructor')

self._clients = clients[:]
self._lin_read = api.LinRead()

def alter(self, op, timeout=None, metadata=None, credentials=None):
return self.any_client().alter(op, timeout=timeout, metadata=metadata, credentials=credentials)

def query(self, q, variables=None, timeout=None, metadata=None, credentials=None):
return self.txn().query(q, variables=variables, timeout=timeout, metadata=metadata, credentials=credentials)
def alter(self, operation, timeout=None, metadata=None, credentials=None):
"""Runs a modification via this client."""
return self.any_client().alter(operation, timeout=timeout,
metadata=metadata,
credentials=credentials)

def query(self, query, variables=None, timeout=None, metadata=None,
credentials=None):
"""Runs a query via this client."""
return self.txn().query(query, variables=variables, timeout=timeout,
metadata=metadata, credentials=credentials)

def txn(self):
"""Creates a transaction."""
return txn.Txn(self)

def set_lin_read(self, ctx):
"""Sets linread map in ctx to the one from this instace."""
ctx.lin_read.MergeFrom(self._lin_read)

def merge_lin_reads(self, src):
"""Merges linread map in this instance with src."""
util.merge_lin_reads(self._lin_read, src)

def any_client(self):
"""Returns a random client."""
return random.choice(self._clients)
48 changes: 33 additions & 15 deletions pydgraph/client_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,59 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Stub for RPC request."""

import grpc

from pydgraph.meta import VERSION
from pydgraph.proto import api_pb2_grpc as api_grpc

__author__ = 'Garvit Pahal <garvit@dgraph.io>'
__maintainer__ = 'Garvit Pahal <garvit@dgraph.io>'
__maintainer__ = 'Martin Martinez Rivera <martinmr@dgraph.io>'
__version__ = VERSION
__status__ = 'development'


class DgraphClientStub(object):
"""Stub for the Dgraph grpc client."""

def __init__(self, addr='localhost:9080', credentials=None, options=None):
if credentials is None:
self.channel = grpc.insecure_channel(addr, options)
else:
self.channel = grpc.secure_channel(addr, credentials, options)
self.channel = grpc.secure_channel(addr, credentials, options)

self.stub = api_grpc.DgraphStub(self.channel)

def alter(self, op, timeout=None, metadata=None, credentials=None):
return self.stub.Alter(op, timeout=timeout, metadata=metadata, credentials=credentials)
def alter(self, operation, timeout=None, metadata=None, credentials=None):
"""Runs alter operation."""
return self.stub.Alter(operation, timeout=timeout, metadata=metadata,
credentials=credentials)

def query(self, req, timeout=None, metadata=None, credentials=None):
return self.stub.Query(req, timeout=timeout, metadata=metadata, credentials=credentials)

def mutate(self, mu, timeout=None, metadata=None, credentials=None):
return self.stub.Mutate(mu, timeout=timeout, metadata=metadata, credentials=credentials)

def commit_or_abort(self, ctx, timeout=None, metadata=None, credentials=None):
return self.stub.CommitOrAbort(ctx, timeout=timeout, metadata=metadata, credentials=credentials)

def check_version(self, check, timeout=None, metadata=None, credentials=None):
return self.stub.CheckVersion(check, timeout=timeout, metadata=metadata, credentials=credentials)
"""Runs query operation."""
return self.stub.Query(req, timeout=timeout, metadata=metadata,
credentials=credentials)

def mutate(self, mutation, timeout=None, metadata=None, credentials=None):
"""Runs mutate operation."""
return self.stub.Mutate(mutation, timeout=timeout, metadata=metadata,
credentials=credentials)

def commit_or_abort(self, ctx, timeout=None, metadata=None,
credentials=None):
"""Runs commit or abort operation."""
return self.stub.CommitOrAbort(ctx, timeout=timeout, metadata=metadata,
credentials=credentials)

def check_version(self, check, timeout=None, metadata=None,
credentials=None):
"""Returns the version of the Dgraph instance."""
return self.stub.CheckVersion(check, timeout=timeout,
metadata=metadata,
credentials=credentials)

def close(self):
"""Deletes channel and stub."""
del self.channel
del self.stub
9 changes: 7 additions & 2 deletions pydgraph/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Errors thrown by the Dgraph client."""

from pydgraph.meta import VERSION

__author__ = 'Garvit Pahal <garvit@dgraph.io>'
__maintainer__ = 'Garvit Pahal <garvit@dgraph.io>'
__maintainer__ = 'Martin Martinez Rivera <martinmr@dgraph.io>'
__version__ = VERSION
__status__ = 'development'


class AbortedError(Exception):
"""Error thrown by aborted transactions."""

def __init__(self):
super(AbortedError, self).__init__('Transaction has been aborted. Please retry')
super(AbortedError, self).__init__(
'Transaction has been aborted. Please retry')
2 changes: 2 additions & 0 deletions pydgraph/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Metadata about this package."""

VERSION = '1.0.0'
Loading