Skip to content

Commit

Permalink
Merge pull request #30 from singnet/development
Browse files Browse the repository at this point in the history
Separate SDK
  • Loading branch information
sassless authored Apr 26, 2024
2 parents 1848b1e + 08639c3 commit 19badda
Show file tree
Hide file tree
Showing 51 changed files with 465 additions and 2,535 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
.env
.venv
env/
venv/
.idea
snet_sdk.egg-info/
snet.sdk.egg-info/
blockchain/node_modules/
__pycache__
snet_sdk/resources/contracts/abi
snet_sdk/resources/contracts/networks
.pyi
7 changes: 4 additions & 3 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include snet_sdk/resources/proto/*
include snet_sdk/resources/contracts/abi/*
include snet_sdk/resources/contracts/networks/*
recursive-exclude * .git
recursive-exclude * node_modules
recursive-exclude * __pycache__
recursive-exclude * *.py[co]
228 changes: 48 additions & 180 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@

# snet-sdk-python

SingularityNET SDK for Python


## Package

The package is published in PyPI at the following link:

|Package |Description |
|----------------------------------------------|---------------------------------------------------------------------|
|[snet-sdk](https://pypi.org/project/snet.sdk/)|Integrate SingularityNET services seamlessly into Python applications|

## Getting Started

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

### Core concepts
The SingularityNET SDK allows you to make calls to SingularityNET services programmatically from your application.
To communicate between clients and services, SingularityNET uses [gRPC](https://grpc.io/).
To handle payment of services, SingularityNET uses [Ethereum state channels](https://dev.singularitynet.io/docs/concepts/multi-party-escrow/).

The SingularityNET SDK allows you to make calls to SingularityNET services programmatically from your application.
To communicate between clients and services, SingularityNET uses [gRPC](https://grpc.io/).
To handle payment of services, SingularityNET uses [Ethereum state channels](https://dev.singularitynet.io/docs/concepts/multi-party-escrow/).
The SingularityNET SDK abstracts and manages state channels with service providers on behalf of the user and handles authentication with the SingularityNET services.

### Usage

To call a SingularityNET service, the user must be able to deposit funds (AGI tokens) to the [Multi-Party Escrow](https://dev.singularitynet.io/docs/concepts/multi-party-escrow/) Smart Contract.
To deposit these tokens or do any other transaction on the Ethereum blockchain, the user must possess an Ethereum identity with available Ether.

To call a SingularityNET service, the user must be able to deposit funds (AGI tokens) to the [Multi-Party Escrow](https://dev.singularitynet.io/docs/concepts/multi-party-escrow/) Smart Contract.
To deposit these tokens or do any other transaction on the Ethereum blockchain, the user must possess an Ethereum identity with available Ether.

To interact with SingularityNET services, you must compile the appropriate client libraries for that service.
To generate the client libraries to use in your application, 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/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/<registry_address>/<org_id>/<service_id>`.
You should move or copy these generated files to the root of your project.

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
Expand All @@ -41,158 +36,47 @@ snet_sdk = sdk.SnetSDK(config)

The `config` parameter must be a Python dictionary.
See [test_sdk_client.py.sample](https://github.com/singnet/snet-cli/blob/master/packages/sdk/testcases/functional_tests/test_sdk_client.py) for a sample configuration file.

##### Free call configuration
If you want to use free call you need to add below mwntioned attributes in config file.

After executing this code, you should have client libraries created for this service. They are located in the following path: ~/.snet/org_id/service_id/python/

Note: Currently you can only save files to ~/.snet/

##### Free call configuration

If you want to use free call you need to add below mwntioned attributes in config file.
```
"free_call_auth_token-bin":"f2548d27ffd319b9c05918eeac15ebab934e5cfcd68e1ec3db2b92765",
"free-call-token-expiry-block":172800,
"email":"[email protected]"
```
You can download this config for a given service from [Dapp]([https://beta.singularitynet.io/)

Now, the instance of the sdk can be used to create service client instances. To create a service client instance, it needs to be supplied with the client libraries that you compiled before.
Specifically, it needs the `Stub` object of the service you want to use from the compiled `_pb2_grpc.py` file of the client library.
Continuing from the previous code this is an example using `example-service` from the `snet` organization:
You can download this config for a given service from [Dapp]([https://beta.singularitynet.io/)

Now, the instance of the sdk can be used to create service client instances.
Continuing from the previous code this is an example using `Exampleservicee` from the `26072b8b6a0e448180f8c0e702ab6d2f` organization:

```python
import example_service_pb2_grpc

org_id = "snet"
service_id = "example-service"
org_id = "26072b8b6a0e448180f8c0e702ab6d2f"
service_id = "Exampleservice"
group_name="default_group"

service_client = snet_sdk.create_service_client(org_id, service_id,example_service_pb2_grpc.CalculatorStub,group_name)
```

The generated `service_client` instance can be used to call the methods exposed by the service.
To call these methods, a request object must be provided. Specifically, you should pick the appropriate request message type that is referenced in the stub object.
Continuing from the previous code this is an example using `example-service` from the `snet` organization:

```python
import example_service_pb2

request = example_service_pb2.Numbers(a=20, b=3)

result = service_client.service.mul(request)
print("Performing 20 * 3: {}".format(result)) # Performing 20 * 3: value: 60.0
```

You can get this code example at [https://github.com/singnet/snet-code-examples/tree/python_client/python/client](https://github.com/singnet/snet-code-examples/tree/python_client/python/client)

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.7](https://www.python.org/downloads/release/python-377/)
* [Node 8+ w/npm](https://nodejs.org/en/download/)

---

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

* 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-cli/tags).

## License

This project is licensed under the MIT License - see the
[LICENSE](https://github.com/singnet/snet-cli/blob/master/snet_sdk/LICENSE) file for details.
# snet-sdk-python

SingularityNET SDK for Python

## Getting Started

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

### Core concepts

The SingularityNET SDK allows you to make calls to SingularityNET services programmatically from your application.
To communicate between clients and services, SingularityNET uses [gRPC](https://grpc.io/).
To handle payment of services, SingularityNET uses [Ethereum state channels](https://dev.singularitynet.io/docs/concepts/multi-party-escrow/).
The SingularityNET SDK abstracts and manages state channels with service providers on behalf of the user and handles authentication with the SingularityNET services.

### Usage

To call a SingularityNET service, the user must be able to deposit funds (AGI tokens) to the [Multi-Party Escrow](https://dev.singularitynet.io/docs/concepts/multi-party-escrow/) Smart Contract.
To deposit these tokens or do any other transaction on the Ethereum blockchain, the user must possess an Ethereum identity with available Ether.


To interact with SingularityNET services, you must compile the appropriate client libraries for that service.
To generate the client libraries to use in your application, 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/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>
service_client = snet_sdk.create_service_client(org_id, service_id, group_name)
```

Optionally, you can specify an output path; otherwise it's going to be `./client_libraries/python/<registry_address>/<org_id>/<service_id>`.
You should move or copy these generated files to the root of your project.

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 import sdk
from config import config
snet_sdk = sdk.SnetSDK(config)
```
Note that `org_id` and `service_id` must be passed to `config`.

The `config` parameter must be a Python dictionary.
See [config.py.sample](https://github.com/singnet/snet-code-examples/blob/master/python/client/config.py.sample) for a sample configuration file.

Now, the instance of the sdk can be used to create service client instances. To create a service client instance, it needs to be supplied with the client libraries that you compiled before.
Specifically, it needs the `Stub` object of the service you want to use from the compiled `_pb2_grpc.py` file of the client library.
Continuing from the previous code this is an example using `example-service` from the `snet` organization:
The generated service_client instance can be used to call methods provided by the service.
To call these methods, you need to use the call_rpc method, passing into it the names of the method and data object, as well as the data itself (What specific data needs to be passed can be seen in the .proto file).
Continuing from the previous code, this is an example using example-service from the snet organization:

```python
import example_service_pb2_grpc

org_id = "snet"
service_id = "example-service"

service_client = sdk.create_service_client(org_id, service_id, example_service_pb2_grpc.CalculatorStub)
result = service_client.call_rpc("mul", "Numbers", a=20, b=3)
print(f"Performing 20 * 3: {result}") # Performing 20 * 3: value: 60.0
```

The generated `service_client` instance can be used to call the methods exposed by the service.
To call these methods, a request object must be provided. Specifically, you should pick the appropriate request message type that is referenced in the stub object.
Continuing from the previous code this is an example using `example-service` from the `snet` organization:

```python
import example_service_pb2

request = example_service_pb2.Numbers(a=20, b=3)

result = service_client.service.mul(request)
print("Performing 20 * 3: {}".format(result)) # Performing 20 * 3: value: 60.0
```

You can get this code example at [https://github.com/singnet/snet-code-examples/tree/python_client/python/client](https://github.com/singnet/snet-code-examples/tree/python_client/python/client)

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/)
Expand All @@ -201,47 +85,31 @@ For more information about gRPC and how to use it with Python, please see:

## Development

### Prerequisites

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

`Note!` don't use Python 3.8.2
#### Prerequisites

---
* [Python 3.10](https://www.python.org/downloads/release/python-31012/)

### Installing
---

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

* Install the required dependencies
```bash
$ cd snet-cli/packages/sdk
$ pip install -r requirements.txt
$ cd ../snet_cli
$ pip install -r requirements.txt
$ cd ../
$ pip install -r requirements.txt

```
* Install development/test blockchain dependencies

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

* Blockchain configuration
If required, you can view/edit the snet-cli configuration of ipfs, eth_rpc in `~/.snet/config ` for various networks.

### Versioning

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

## License

This project is licensed under the MIT License - see the
[LICENSE](https://github.com/singnet/snet-cli/blob/master/snet_sdk/LICENSE) file for details.
[LICENSE](https://github.com/singnet/snet-sdk-python/blob/master/LICENSE) file for details.
56 changes: 0 additions & 56 deletions common_dependencies.py

This file was deleted.

23 changes: 21 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
--index-url https://pypi.python.org/simple
-e .
protobuf==4.21.6
grpcio-tools==1.59.0
wheel==0.41.2
jsonrpcclient==4.0.3
eth-hash==0.5.2
rlp==3.0.0
eth-rlp==0.3.0
web3==6.11.1
mnemonic==0.20
pycoin==0.92.20230326
pyyaml==6.0.1
ipfshttpclient==0.4.13.2
rfc3986==2.0.0
pymultihash==0.8.2
base58==2.1.1
argcomplete==3.1.2
grpcio-health-checking==1.59.0
jsonschema==4.0.0
eth-account==0.9.0
snet-cli==2.1.2
snet.contracts==0.1.1
Loading

0 comments on commit 19badda

Please sign in to comment.