Skip to content

Commit

Permalink
dded client-side interceptor to automatically add metadata to service…
Browse files Browse the repository at this point in the history
… call

Updated README to reflect the change
  • Loading branch information
vforvalerio87 committed Feb 8, 2019
1 parent fb1fc55 commit 1b4cce3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ This is an example of how to make a call to a SingularityNET service in the `sne
```python
stub = client.grpc.example_service_pb2_grpc.CalculatorStub(client.grpc_channel)
request = calculator.grpc.example_service_pb2.Numbers(a=10, b=12)
result = stub.add(request, metadata=calculator.get_service_call_metadata())
result = stub.add(request)
print(result)
```
If you have no open state channels with the service provider, you can create one by calling the following function:
Expand Down
25 changes: 23 additions & 2 deletions snet_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
import ecdsa
import hashlib
import grpc
import collections
import web3
from web3.gas_strategies.rpc import rpc_gas_price_strategy
from eth_account.messages import defunct_hash_message
from rfc3986 import urlparse
import ipfsapi

import snet_sdk.header_manipulator_client_interceptor
import snet_sdk.generic_client_interceptor as generic_client_interceptor

__version__ = "0.0.1"

Expand All @@ -41,6 +42,14 @@ def default(self, obj):
super().default(self, obj)


class _ClientCallDetails(
collections.namedtuple(
'_ClientCallDetails',
('method', 'timeout', 'metadata', 'credentials')),
grpc.ClientCallDetails):
pass


snet_sdk_defaults = {
"libraries_base_path": "grpc",
"eth_rpc_endpoint": "https://kovan.infura.io",
Expand Down Expand Up @@ -345,8 +354,20 @@ def _get_service_call_metadata(channel_id):
# Client exports
client.open_channel = lambda value=default_channel_value, expiration=default_channel_expiration: _client_open_channel(value, expiration)
client.get_service_call_metadata = lambda: _get_service_call_metadata(_channel_id)
client.grpc_channel = grpc_channel
client.grpc = imported_modules

def intercept_call(client_call_details, request_iterator, request_streaming,
response_streaming):
metadata = []
if client_call_details.metadata is not None:
metadata = list(client_call_details.metadata)
metadata.extend(client.get_service_call_metadata())
client_call_details = _ClientCallDetails(
client_call_details.method, client_call_details.timeout, metadata,
client_call_details.credentials)
return client_call_details, request_iterator, None

client.grpc_channel = grpc.intercept_channel(grpc_channel, generic_client_interceptor.create(intercept_call))


return client
46 changes: 0 additions & 46 deletions snet_sdk/header_manipulator_client_interceptor.py

This file was deleted.

0 comments on commit 1b4cce3

Please sign in to comment.