-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtsObjects.py
130 lines (116 loc) · 4.56 KB
/
tsObjects.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'''
Copyright 2022 Verizon
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
from marshmallow import Schema, fields
import datetime
import requests
from requests_toolbelt.utils import dump
import json
import sys
import pprint
class tsVariables:
def __init__(self):
self.myKey = "<your_thingspace_access_key>"
self.mySecret = "<your_thingspace_access_secret_key>"
self.uwsUsername = "<your_thingspace_username>"
self.uwsPassword = "<your_thingspace_password>"
self.tsBearer = ""
self.uwsToken = ""
#Account number
self.account = "<your_verizon_thingspace_accountnumber>"
# requests library does not pull local adapter proxy settings; must set and pass in each post, get, etc. when on a corp network
#self.httpProxy = {}
#self.httpProxy["http"] = "proxy.address"
#self.httpProxy["https"] = "proxy.address"
#self.billCycle = 2
def genDefaultHeader(self):
myHeader = {}
myHeader["Accept"] = "application/json"
myHeader["Content-Type"] = "application/json"
myHeader["Authorization"] = "Bearer " + self.tsBearer
if len(self.uwsToken) > 0:
myHeader["VZ-M2M-Token"] = self.uwsToken
return myHeader
class coarseRequestDevice:
def __init__(self):
self.id = ""
self.kind = ""
self.mdn = ""
class coarseRequest:
def __init__(self, keys):
self.accountName = keys.account
self.accuracyMode = "0"
self.cacheMode = "2"
self.deviceList = list()
def addDevice(self, id, kind, mdn):
device = coarseRequestDevice()
device.id = id
device.kind = kind
device.mdn = mdn
self.deviceList.append(device.__dict__)
# https://thingspace.verizon.com/resources/documentation/location/API_Reference/Get_Device_Locations_synchronous/
def getLocations(self, myKeys):
myUrl = "https://thingspace.verizon.com/api/loc/v1/locations"
try:
response = requests.post(url = myUrl, headers = myKeys.genDefaultHeader(), data = json.dumps(self.__dict__, sort_keys=True), proxies = myKeys.httpProxy)
if response.status_code == requests.codes.ok:
responseJson = json.loads(response.text)
positionSchema = LocationSchema(many=True)
try:
positionCollection = positionSchema.dump(responseJson)
except ValidationError as e:
print(e.messages)
print(e.valid_data)
sys.exit()
except TypeError as e:
print(e)
sys.exit()
else:
print(response.content.decode("UTF-8"))
sys.exit()
except requests.exceptions.RequestException as e:
raise SystemExit(e)
return positionCollection
class subscriptionStatusRequest:
def __init__(self, keys):
self.accountName = keys.account
self.balance = 0
# https://thingspace.verizon.com/resources/documentation/location/API_Reference/Get_Subscription_Status/
def getStatus(self, myKeys):
myUrl = "https://thingspace.verizon.com/api/loc/v1/subscriptions/" + self.accountName
try:
response = requests.get(url = myUrl, headers = myKeys.genDefaultHeader(), proxies = myKeys.httpProxy)
if response.status_code == requests.codes.ok:
responseJson = json.loads(response.text)
statusSchema = SubscriptionStatusSchema()
try:
subscriptionStatus = statusSchema.dump(responseJson)
except ValidationError as e:
print(e.messages)
print(e.valid_data)
sys.exit()
except TypeError as e:
print(e)
sys.exit()
self.balance = int(responseJson["maxAllowance"]) - int(responseJson["usedAllowance"])
else:
print(response.content.decode("UTF-8"))
sys.exit()
except requests.exceptions.RequestException as e:
raise SystemExit(e)
return subscriptionStatus
PdSchema = Schema.from_dict({'time': fields.Str(), 'utcoffset': fields.Str(),
'x': fields.Str(), 'y': fields.Str(), 'radius': fields.Str(), 'qos': fields.Boolean()})
ErrorSchema = Schema.from_dict({'time': fields.Str(), 'type': fields.Str(), 'info': fields.Str(), 'utcoffset': fields.Str()})
LocationSchema = Schema.from_dict({'msid': fields.Str(), 'pd': fields.Nested(PdSchema()),
'error': fields.Nested(ErrorSchema())})
SubscriptionStatusSchema = Schema.from_dict({'accountName': fields.Str(), 'locType': fields.Str(), 'maxAllowance': fields.Str(), 'usedAllowance': fields.Str(), 'purchaseTime': fields.Str()})