Querying portia - Data fetching with Python

Making HTTP requests using Python - Checking credentials

  • ### Unsucessfull request

In [10]:
# Library for HTTP requests
import requests

# Portia service URL for token authorization checking
url = "http://io.portia.supe.solutions/api/v1/accesstoken/check"

# Makes the request
response = requests.get(url)

# Shows response
if response.status_code == 200:
    print("Success accessing Portia Service - Status Code: {0}\n{1}".format(response.status_code, response.text))
else:
    print("Couldn't access Portia service - Status Code: {0}".format(response.status_code))


Couldn't access Portia service - Status Code: 401
  • ### Sucessfull request

In [11]:
# Library for HTTP requests
import requests

# Portia service URL for token authorization checking
url = "http://io.portia.supe.solutions/api/v1/accesstoken/check"

# Setting the header with a token for successful authorization
header = {"Authorization": "Bearer bdb6e780b43011e7af0b67cba486057b"}

# Makes the request
response = requests.get(url, headers=header)

# Shows response
if response.status_code == 200:
    print("Success accessing Portia Service - Status Code: {0}\n{1}".format(response.status_code, response.text))
else:
    print("Couldn't access Portia service - Status Code: {0}".format(response.status_code))


Success accessing Portia Service - Status Code: 200
{"user":"teste","isLoggedIn":true}

Obtaining data from a specific time frame

Now that we have learned how to authenticate with the service, let's see how to get the data


In [50]:
import requests      # Library for HTTP requests
import time as epoch # Library for timing functions
import json          # Library for JSON usage

# Example for getting the last 5 minutes of data
fiveMinutes = 1000 * 60 * 5
toTimestamp = int(epoch.time()) * 1000 # The time lib only gives us the UTC time as seconds since January 1, 1970, so, we multiply by 1000 to get the milliseconds
fromTimestamp = toTimestamp - fiveMinutes

# Portia service URL for specific time frame
url = "http://io.portia.supe.solutions/api/v1/device/HytTDwUp-j8yrsh8e/port/2/sensor/1"

# Adding the calculated timestamps as GET parameters
url += "?from_timestamp={0}&?to_timestamp={1}".format(fromTimestamp, toTimestamp) # If no parameters, the service default response is for the last 24 hours

# Setting the header with a token for successful authorization
header = {"Authorization": "Bearer bdb6e780b43011e7af0b67cba486057b"}

# Makes the request
response = requests.get(url, headers=header)

# Shows response
if response.status_code == 200:
    # Parses dimensions
    dimensions = json.loads(response.text)
    
    print("Success! For each received dimension:")
    for dimension in dimensions:
        print("Accessing dimension package:")
        print("\tDimension Code: {0}".format(dimension["dimension_code"]))
        print("\tUnity Code: {0}".format(dimension["dimension_unity_code"]))
        print("\tThing Code: {0}".format(dimension["dimension_thing_code"]))
        print("\tDimension Value: {0}".format(dimension["dimension_value"]))
        print("\tServer Timestamp: {0}\n".format(dimension["server_timestamp"]))
        
else:
    print("Couldn't access Portia service - Status Code: {0}".format(response.status_code))


Success! For each received dimension:
Accessing dimension package:
	Dimension Code: 1
	Unity Code: 1
	Thing Code: 1
	Dimension Value: 22.5
	Server Timestamp: 1508778623757

Accessing dimension package:
	Dimension Code: 1
	Unity Code: 1
	Thing Code: 1
	Dimension Value: 22.5
	Server Timestamp: 1508778562317

Accessing dimension package:
	Dimension Code: 1
	Unity Code: 1
	Thing Code: 1
	Dimension Value: 22.5
	Server Timestamp: 1508778502766

Accessing dimension package:
	Dimension Code: 1
	Unity Code: 1
	Thing Code: 1
	Dimension Value: 22.4
	Server Timestamp: 1508778442152

Accessing dimension package:
	Dimension Code: 1
	Unity Code: 1
	Thing Code: 1
	Dimension Value: 22.4
	Server Timestamp: 1508778382528

Obtaining the latest data

For the next example, we are requesting only the last data sent by the equipments

  • ### Last dimension

In [56]:
import requests      # Library for HTTP requests
import json          # Library for JSON usage

# Portia service URL for getting the latest data
url = "http://io.portia.supe.solutions/api/v1/device/HytTDwUp-j8yrsh8e/port/2/sensor/1/last"

# Setting the header with a token for successful authorization
header = {"Authorization": "Bearer bdb6e780b43011e7af0b67cba486057b"}

# Makes the request
response = requests.get(url, headers=header)

# Shows response
if response.status_code == 200:
    # Parses dimension
    dimension = json.loads(response.text)[0]
    print("Success! Accessing dimension package:")
    print("\tDimension Code: {0}".format(dimension["dimension_code"]))
    print("\tUnity Code: {0}".format(dimension["dimension_unity_code"]))
    print("\tThing Code: {0}".format(dimension["dimension_thing_code"]))
    print("\tDimension Value: {0}".format(dimension["dimension_value"]))
    print("\tServer Timestamp: {0}\n".format(dimension["server_timestamp"]))
        
else:
    print("Couldn't access Portia service - Status Code: {0}".format(response.status_code))


Success! Accessing dimension package:
	Dimension Code: 1
	Unity Code: 1
	Thing Code: 1
	Dimension Value: 22.6
	Server Timestamp: 1508779043681

  • ### Last three dimensions

In [59]:
import requests      # Library for HTTP requests
import json          # Library for JSON usage

# Portia service URL for getting the latest data
url = "http://io.portia.supe.solutions/api/v1/device/HytTDwUp-j8yrsh8e/port/2/sensor/1/last"

# Adding GET parameter for specifying that we want the last 3 dimension packages
url += "?limit={0}".format(3)

# Setting the header with a token for successful authorization
header = {"Authorization": "Bearer bdb6e780b43011e7af0b67cba486057b"}

# Makes the request
response = requests.get(url, headers=header)

# Shows response
if response.status_code == 200:
    # Parses dimensions
    dimensions = json.loads(response.text)
    
    print("Success! For each received dimension:")
    for dimension in dimensions:
        print("Accessing dimension package:")
        print("\tDimension Code: {0}".format(dimension["dimension_code"]))
        print("\tUnity Code: {0}".format(dimension["dimension_unity_code"]))
        print("\tThing Code: {0}".format(dimension["dimension_thing_code"]))
        print("\tDimension Value: {0}".format(dimension["dimension_value"]))
        print("\tServer Timestamp: {0}\n".format(dimension["server_timestamp"]))
        
else:
    print("Couldn't access Portia service - Status Code: {0}".format(response.status_code))


Success! For each received dimension:
Accessing dimension package:
	Dimension Code: 1
	Unity Code: 1
	Thing Code: 1
	Dimension Value: 22.5
	Server Timestamp: 1508779284186

Accessing dimension package:
	Dimension Code: 1
	Unity Code: 1
	Thing Code: 1
	Dimension Value: 22.5
	Server Timestamp: 1508779224094

Accessing dimension package:
	Dimension Code: 1
	Unity Code: 1
	Thing Code: 1
	Dimension Value: 22.6
	Server Timestamp: 1508779163051