forked from peterchauyw/iotdm-pyclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_example.py
81 lines (66 loc) · 3.87 KB
/
http_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File to demonstate operation of Python API using HTTP protocol to talk to host
# To operate run IoTDM server on localhost as described at:
# https://wiki.opendaylight.org/view/IoTDM:Main#Getting_started_for_users
# Contents of this file is based on:
# https://wiki.opendaylight.org/view/IoTDM:PythonAPI
# December 2016
# Author: Iain Sharp
import iotdm_api
from onem2m_xml_protocols.ae import *
from onem2m_xml_protocols.container import *
from onem2m_xml_protocols.contentinstance import *
api = iotdm_api.Iotdm_api()
# Build the root CSE base of a tree by Restconf:
(responseCode, responseString) = api.restConf('http://localhost', 'InCSE1', 'admin', 'admin')
print("Result - Code: " + str(responseCode) + " String: " + responseString)
# Creation of an AE named "myAE" under the CSE base "InCSE1".
AE = ae()
AE.set_api("Nk836-t071-fc022")
AE.set_rr(True)
AE.set_rn("myAE")
payload = AE.to_JSON()
(responseCode, responseString) = api.create("http://localhost:8282/InCSE1", 2, payload, origin="AE-ID", requestID="12345")
print("Result - Code: " + str(responseCode) + " String: " + responseString)
# Creation of a Container named "myContainer" with a maximum number of bytes of 32, under "myAE".
container = cnt()
container.set_rn("myContainer")
container.set_mbs(32)
payload = container.to_JSON()
(responseCode, responseString) = api.create("http://localhost:8282/InCSE1/myAE", 3, payload, origin="AE-ID", requestID="12345")
print("Result - Code: " + str(responseCode) + " String: " + responseString)
# Creation of a Container named "mySubContainer" with a maximum number of instances of 5, under "myContainer".
container = cnt()
container.set_rn("mySubContainer")
container.set_mni(5)
payload = container.to_JSON()
(responseCode, responseString) = api.create("http://localhost:8282/InCSE1/myAE/myContainer", 3, payload, origin="AE-ID", requestID="12345")
print("Result - Code: " + str(responseCode) + " String: " + responseString)
# Creation of a Content Instance named "myContentInstance" under "myContainer", with "hello" as its content.
con_instance = cin()
con_instance.set_con("hello")
con_instance.set_rn("myContentInstance")
payload = con_instance.to_JSON()
(responseCode, responseString) = api.create("http://localhost:8282/InCSE1/myAE/myContainer", 4, payload, origin="AE-ID", requestID="12345")
print("Result - Code: " + str(responseCode) + " String: " + responseString)
# Creation of a Content Instance named "myOtherContentInstance" under "mySubContainer", with "world" as its content.
con_instance = cin()
con_instance.set_con("world")
con_instance.set_rn("myOtherContentInstance")
payload = con_instance.to_JSON()
(responseCode, responseString) = api.create("http://localhost:8282/InCSE1/myAE/myContainer/mySubContainer", 4, payload, origin="AE-ID", requestID="12345")
print("Result - Code: " + str(responseCode) + " String: " + responseString)
# Update of the Container "myContainer", with its attribute "Ontology Reference" being set to "http://info"
container = cnt()
container.set_or("http://info")
payload = container.to_JSON()
(responseCode, responseString) = api.update("http://localhost:8282/InCSE1/myAE/myContainer", payload, origin="AE-ID", requestID="12345")
print("Result - Code: " + str(responseCode) + " String: " + responseString)
# Update of the Container "mySubContainer", with its attribute "label" being set to "submarine"
container = cnt()
container.set_lbl(["submarine"])
payload = container.to_JSON()
(responseCode, responseString) = api.update("http://localhost:8282/InCSE1/myAE/myContainer/mySubContainer", payload, origin="AE-ID", requestID="12345")
print("Result - Code: " + str(responseCode) + " String: " + responseString)
# Deletion of the ContentInstance "myContentInstance"
(responseCode, responseString) = api.delete("http://localhost:8282/InCSE1/myAE/myContainer/myContentInstance", origin="AE-ID", requestID="12345")
print("Result - Code: " + str(responseCode) + " String: " + responseString)