How to use the data provider's data?

To show how a service can use the data from data provider and, we use "Data Example" service as an example. The "Data Example" service is already in our container, as well as our developing tools, After starting the container, it can be found by:

    $ cd ~/service_demo/data_example

The directory looks like:

    \hello_world
    --  client.opt.conf  
    --  deploy.opt.conf  
    --  hello_world.py 
    --  hello_lib.py
    --  hello_lib.pyc 
    --  input.txt
    --  README.md

The `client.opt.conf` and `deploy.opt.conf` are configuration files, which are used to invoke and deploy the service respectively.
`hello_world.py` and `hello_lib.py` is the service code and `input.txt` contains the input string.
`README.md` briefly introduces the services.


## Service Code
Let's first see the `hello_world.py`, which is very simple:

```python

from teex import *
from hello_lib import *

input_string = TEEX_getinput()

output_string = hello_string(input_string)

output_string += "Service Data:"
output_string += TEEX_getdata(0)
output_string += "Task Data:"
output_string += TEEX_getdata(4)

TEEX_return({'message': output_string})
})

The first line imports our teex Python library, which provides interfaces to get service input and return. The second line imports a naive 'hello_lib' library, which implements hello_string function. Then the TEEX_getinput is used to get the user input string. And the TEEX_getdata(index) is used to get the data from data provider. There are six data assigned to this service and the service code only uses the first one and the fifth. "Data Example" service just construct a new string and then use TEE_return to send it back to the user.

Uploading data for "Data Example"

We can use teex-upload to upload data to TEEX TestNet. We use a configuration file to parse the medata of the target data to teex-upload. You can upload the data by typing:

    $ cd ~/service_demo/data_upload_example
    $ teex-upload -c upload.opt.conf 

The upload.opt.conf' contains all the arguments required byteex-upload`, which contains:

{
        "data-file": "data.txt",
        "price": "2",
        "aes-key": "1234567890123456",
        "public-key": "627306090abab3a6e1400e9345bc60c78a8bef57",
        "desc": "data uploader demo"
}

The "data-file" specifies the data, the "price" specifies the how much token needs to be paid to the data provider. The "aes-key" is used to encrypt the data and "public-key" is the wallet address of the data provider. After executing the teex-upload, the data provider can get the data ID.

Deploying "Data Example"

We can use teex-deploy to deploy the "Data Example" service to TEEX TestNet. We use a configuration file to parse all arguments to teex-deploy. You can deploy the service by typing:

    $ cd ~/service_demo/data_example
    $ teex-deploy -c deploy.opt.conf

The deploy.opt.conf contains all the arguments required by teex-deploy, which contains:

{
        "service-files":
        [
                {"filename": "hello_world.py"},
                {"filename": "hello_lib.py"}
        ],
        "entry-file": "hello_world.py",

        "data-list":
        [
                {"data-id": "e8d6e3e7ca1b49bf89fd73f6acbd5ec2"},
                {"data-id": "e8d6e3e7ca1b49bf89fd73f6acbd5ec2"},
                {"data-id": "e8d6e3e7ca1b49bf89fd73f6acbd5ec2"}
        ],

        "price": "2",
        "worker-feerate": "1",
        "private-key": "4E291BA605CED837F1044DE583453881527C9BD4A222DE0B9DFFB9FA72447337",
        "public-key": "7C0556cc73C78115454A219447ab870Ecf42B688",
        "desc": "Data Example"
}

"service-files" specifies the service code, "data-list" specifies the data required by the service, price" indicates how much token need to be paid to invoke the service and "worker-feerate" is the token paid to the worker. We require that the "price" cannot less than 1. "private-key" and "public-key" specify the keys of the service. We have already provided the default key pair in the configuration file, and you can also replace it with your own key pair. After executing the teex-deploy, it will output the service ID of the "Data Example".

Note

Although TEEX TestNet enforces the security of the "private-key", we recommend the user not to use his wallet private key as the service private key.

Invoking "Data Service"

The invocation can be performed with teex-client. We also use a configuration file to parse all arguments to it. A "Data Service" service can be invoked by typing:

    $ cd ~/service_demo/data_example
    $ teex-client -c client.opt.conf

The client.opt.conf contains all the arguments required by teex-client, which contains:

{
        "service_id": "04ebb787188d43a0ab2c7660c788ef5f",
        "data-list":
        [
                {"data-id": "e8d6e3e7ca1b49bf89fd73f6acbd5ec2"},
                {"data-id": "e8d6e3e7ca1b49bf89fd73f6acbd5ec2"},
                {"data-id": "e8d6e3e7ca1b49bf89fd73f6acbd5ec2"}
        ],
        "input_file": "input.txt",
        "payment": "100"
}

The "data_list" specifies data assigned to the task (which is different as data assigned to the service), input_file" specifies the file which contains the input string to the service and "payment" specifies how mush token user wants to pay for this invocation. The payment cannot less than the sum of the price of invoked service and all the data (both the data of service and data of task). "service_id" is the ID of service, which is generated by teex-deploy.

We have already deployed the service on TEEX TestNet and put its service ID in the configuration file, so users can directly use it. User can replace this ID with the service ID generated in Deploying "Data Example".