Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import client libraries, allow to make grpc calls to services #5

Merged
merged 16 commits into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
venv/
.idea
snet_sdk.egg-info/
blockchain/node_modules/
__pycache__
snet_sdk/resources/contracts/abi
snet_sdk/resources/contracts/networks
101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# snet-sdk-python

SingularityNET SDK for Python

## Getting Started

These instructions are for the development and use of the SingularityNET SDK for Python.

### Usage

The SingularityNET SDK allows you to import compiled client libraries for your service or services or choice and make calls to those services programmatically from your application by setting up state channels with the providers of those services and making gRPC calls to the SingularityNET daemons for those services by selecting a channel with sufficient funding and supplying the appropriate metadata for authentication.

Once you have installed the snet-sdk in your current environment and it's in your PYTHONPATH, you should import it and create an instance of the base sdk class:

```python
from snet_sdk import Snet
import config
snet = Snet(private_key=config.private_key)
```

Now, the instance of the sdk can be used to instantiate clients for SingularityNET services. To interact with those services, the sdk needs to be supplied the compiled client libraries and a reference to their path on your file system.

To generate the client libraries, you need the SingularityNET Command Line Interface, or CLI, which you can download from PyPi, see [https://github.com/singnet/snet-cli#installing-with-pip](https://github.com/singnet/snet-cli#installing-with-pip)

Once you have the CLI installed, run the following command:
```bash
snet sdk generate-client-library python <org_id> <service_id>
```

Optionally, you can specify an output path; otherwise it's going to be `./client_libraries/python/<org_id>/<service_id>`

Now, by default the sdk is going to look for those client libraries in the `./grpc/<org_id>/<service_id>` in the directory of the main process of the module it's being imported by.

Once you have the generated client libraries in place, you can create an instance of a SingularityNET service client:
```python
client = snet.client("<org_id>", "<service_id>")
```

The client exposes the following properties and methods:
- All of the modules from the generated client library as `client.grpc.<module_name`. These are temporarily added to your PYTHONPATH and imported at runtime
- Functions to open, fund and extend state channels
- Functions to retrieve the list of state channels between you and the service provider from the blockchain
- Functions to get the updated state for a specific channel from the service provider, signed by yourself
- Functions to generate and sign the required metadata to make gRPC calls to the service daemon

This is an example of how to make a call to a SingularityNET service in the `snet` organization with the `example-service` service_id using the base SDK and client instances created as shown before:
```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)
print(result)
```
If you have no open state channels with the service provider, you can create one by calling the following function:
```python
client.open_channel()
```
By default, this will create a channel with the shortest possible expiration date and the necessary amount to make 100 service calls.
Once an open channel is created and funded, the sdk will automatically find and use a funded, non-expired channel.

For more information about gRPC and how to use it with Python, please see:
- [gRPC Basics - Python](https://grpc.io/docs/tutorials/basic/python.html)
- [gRPC Python’s documentation](https://grpc.io/grpc/python/)

---

## Development

### Installing

#### Prerequisites

* [Python 3.6.5](https://www.python.org/downloads/release/python-365/)
* [Node 8+ w/npm](https://nodejs.org/en/download/)

---

* Clone the git repository
```bash
$ git clone [email protected]:singnet/snet-sdk-python.git
$ cd snet-sdk-python
```

* Install development/test blockchain dependencies
```bash
$ ./scripts/blockchain install
```

* Install the package in development/editable mode
```bash
$ pip install -e .
```

### Versioning

We use [SemVer](http://semver.org/) for versioning. For the versions available, see the
[tags on this repository](https://github.com/singnet/snet-sdk-python/tags).

## License

This project is licensed under the MIT License - see the
[LICENSE](https://github.com/singnet/snet-sdk-python/blob/master/LICENSE) file for details.
6 changes: 6 additions & 0 deletions blockchain/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"singularitynet-platform-contracts": "0.2.6",
"singularitynet-token-contracts": "2.0.1"
}
}
55 changes: 55 additions & 0 deletions scripts/blockchain
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env python3

import os
import pathlib
import shutil
import subprocess
import sys


def main():
assert len(sys.argv) > 1, "please select a target from 'install', 'uninstall'"
target = sys.argv[1]
blockchain_dir = pathlib.Path(__file__).absolute().parent.parent.joinpath("blockchain")
node_modules_dir = blockchain_dir.joinpath("node_modules")
platform_json_src_dir = node_modules_dir.joinpath("singularitynet-platform-contracts")
token_json_src_dir = node_modules_dir.joinpath("singularitynet-token-contracts")
token_contract_name = "SingularityNetToken"
contract_json_dest_dir = pathlib.Path(__file__).absolute().parent.parent.joinpath("snet_sdk", "resources", "contracts")
abi_contract_names = ["Registry", "MultiPartyEscrow"]
networks_contract_names = ["Registry", "MultiPartyEscrow"]

npm_location = shutil.which('npm')
if not npm_location:
raise Exception("This script requires 'npm' to be installed and in your PATH")

if target == "install":
shutil.rmtree(contract_json_dest_dir)

subprocess.call([npm_location, "install"], cwd=blockchain_dir)

os.makedirs(contract_json_dest_dir.joinpath("abi"), exist_ok=True)
os.makedirs(contract_json_dest_dir.joinpath("networks"), exist_ok=True)

for contract_name in abi_contract_names:
shutil.copy(platform_json_src_dir.joinpath("abi", "{}.json".format(contract_name)),
contract_json_dest_dir.joinpath("abi", "{}.json".format(contract_name)))
for contract_name in networks_contract_names:
shutil.copy(platform_json_src_dir.joinpath("networks", "{}.json".format(contract_name)),
contract_json_dest_dir.joinpath("networks", "{}.json".format(contract_name)))

shutil.copy(token_json_src_dir.joinpath("abi", "{}.json".format(token_contract_name)),
contract_json_dest_dir.joinpath("abi", "{}.json".format(token_contract_name)))
shutil.copy(token_json_src_dir.joinpath("networks", "{}.json".format(token_contract_name)),
contract_json_dest_dir.joinpath("networks", "{}.json".format(token_contract_name)))
elif target == "uninstall":
try:
shutil.rmtree(node_modules_dir)
shutil.rmtree(contract_json_dest_dir.joinpath("abi"))
shutil.rmtree(contract_json_dest_dir.joinpath("networks"))
except FileNotFoundError:
pass


if __name__ == "__main__":
main()
23 changes: 23 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from setuptools import setup, find_packages
import re

with open('snet_sdk/__init__.py', 'rt', encoding='utf8') as f:
version = re.search(r'__version__ = "(.*?)"', f.read()).group(1)

setup(
name='snet_sdk',
version=version,
packages=find_packages(),
url='https://github.com/singnet/snet-sdk-python',
license='MIT',
author='SingularityNET Foundation',
author_email='[email protected]',
description='SingularityNET Python SDK',
install_requires=[
'grpcio-tools==1.17.1',
'ecdsa==0.13',
'web3==4.2.1',
'ipfsapi==0.4.2.post1',
'rfc3986==1.1.0'
]
)
Loading