A client library for accessing data through the `SparkWorks Rest
API <https://api.sparkworks.net/swagger-ui.html>`__.

SparkWorks Rest API Python Client
=================================

This Python package is automatically generated by the `Swagger
Codegen <https://github.com/swagger-api/swagger-codegen>`__ project:

-  API version: v2.0
-  Build package: io.swagger.codegen.languages.PythonClientCodegen For
   more information, please visit http://www.sparkworks.net/

Requirements.
-------------

Python 2.7 and 3.4+

Installation & Usage
--------------------

pip install
~~~~~~~~~~~

.. code:: sh

    pip install sparkworksrest

(you may need to run ``pip`` with root permission:
``sudo pip install sparkworksrest``)

Then import the package:

.. code:: python

    import sparkworks_client 

Setting up the access to the SparkWorks Rest API
------------------------------------------------

.. code:: python


    import sparkworks_client

    # Configure OAuth2 access token for authorization: oauth2
    configuration = sparkworks_client.Configuration("username", "password", "client_id", "client_secret")
    configuration.connect()

    # create an instance of the API class
    api_client = sparkworks_client.ApiClient(configuration)

    #initialize the clients needed
    unit_api_instance = sparkworks_client.UnitApi(api_client)
    group_api_instance = sparkworks_client.GroupApi(api_client)
    phenomenon_api_instance = sparkworks_client.PhenomenonApi(api_client)
    gateway_api_instance = sparkworks_client.GatewayApi(api_client)
    resource_api_instance = sparkworks_client.ResourceApi(api_client)
    command_api_instance = sparkworks_client.CommandApi(api_client)
    registry_api_instance = sparkworks_client.RegistryApi(api_client)
    data_api_instance = sparkworks_client.DataApi(api_client)
    participatorySensing_api_instance = sparkworks_client.ParticipatorySensingApi(api_client)

Examples
--------

Example 1: List Resources and Access Resource Data
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: python

    import time

    import sparkworks_client

    # Configure OAuth2 access token for authorization: oauth2
    configuration = sparkworks_client.Configuration("username", "password", "client_id", "client_secret")
    configuration.connect()
    print("access_token: " + configuration.access_token)

    # create an instance of the API class

    api_client = sparkworks_client.ApiClient(configuration)

    resource_api_instance = sparkworks_client.ResourceApi(api_client)
    unit_api_instance = sparkworks_client.UnitApi(api_client)
    phenomenon_api_instance = sparkworks_client.PhenomenonApi(api_client)
    data_api_instance = sparkworks_client.DataApi(api_client)

    # list all Resources
    resources = resource_api_instance.get_resources()

    print "====resource===="
    # retrieve resource by uuid
    resource = resource_api_instance.get_resource_by_uuid(resources[0].uuid)
    print resource

    print "====phenomenon===="
    phenomenon = phenomenon_api_instance.get_phenomenon_by_uuid(resource.phenomenon_uuid)
    print phenomenon

    print "====unit===="
    unit = unit_api_instance.get_unit_by_uuid(resource.unit_uuid)
    print unit

    time_from = (int(round(time.time())) - 5 * 60 * 60) * 1000
    time_to = int(round(time.time())) * 1000

    print "====data 5min===="
    # retrieve data - granularity 5min
    data = data_api_instance.query_time_range({
        "queries": [
            {
                "from": time_from,
                "to": time_to,
                "granularity": "5min",
                "resourceUuid": resource.uuid
            }
        ]
    })

    for d in data.results.values():
        for datum in d.data:
            print "5min", datum

    print "====data hour===="
    # retrieve data - granularity hour
    data = data_api_instance.query_time_range({
        "queries": [
            {
                "from": time_from,
                "to": time_to,
                "granularity": "hour",
                "resourceUuid": resource.uuid
            }
        ]
    })

    for d in data.results.values():
        for datum in d.data:
            print "hour", datum

Example 2: List Groups and Group Resources
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: python

    import sparkworks_client

    # Configure OAuth2 access token for authorization: oauth2
    configuration = sparkworks_client.Configuration("username", "password", "client_id", "client_secret")
    configuration.connect()
    print("access_token: " + configuration.access_token)

    # create an instance of the API class

    api_client = sparkworks_client.ApiClient(configuration)

    resource_api_instance = sparkworks_client.ResourceApi(api_client)
    group_api_instance = sparkworks_client.GroupApi(api_client)

    print "====groups===="
    # list all groups
    groups = group_api_instance.get_all_groups()
    for group in groups:
        print group

    print "====group[0].resources===="
    # retrieve resources by group uuid
    resources = group_api_instance.get_group_resources(groups[0].uuid)
    for resource in resources:
        print resource
