/
pogiApi-cli.py
165 lines (151 loc) · 5.24 KB
/
pogiApi-cli.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Prerequisite: Python Requests Library - https://docs.python-requests.org/
# - to install: https://docs.python-requests.org/en/master/user/install/#install
import json
import sys
from random import randint
# Import requests library
try:
import requests
except Exception as ex:
print("You need to install python's request library to connect to PogiAPI.")
print("Install instruction is in https://requests.readthedocs.io/en/master/user/install/#install.")
sys.exit()
# Required Variables
pogi_url = 'https://sandbox.simplyrfid.com/api' # Enter your Pogi server URL here (default: https://sandbox.simplyrfid.com/api)
user_id = '' # Enter your Pogi user ID here
user_pwd = '' # Enter your Pogi password here
# Wrapper class to easily find API calls
class PogiApi:
@staticmethod
def _postRequest(apiRequest):
apiResponse = requests.post(pogi_url, json=apiRequest)
if apiResponse.status_code == 200:
return json.loads(apiResponse.text)
else:
return None
@staticmethod
def getToken(user_id, user_pwd):
print('Signing in as:', user_id)
apiResponse = PogiApi._postRequest({
"op": "token-get",
"userId": user_id,
"password": user_pwd
})
if apiResponse is None:
print("There was a problem connecting to the server")
return None
elif "token" not in apiResponse:
print(apiResponse["message"])
return None
else:
print('Log in (via token-get) succesful. User:', user_id)
return apiResponse["token"]
@staticmethod
def postIdCurrent(token):
apiResponse = PogiApi._postRequest({
"op": "id-current",
"token": token
})
return apiResponse
@staticmethod
def postIdHistory(token):
apiResponse = PogiApi._postRequest({
"op": "id-history",
"token": token
})
return apiResponse
@staticmethod
def postIdGet(token, tag):
apiResponse = PogiApi._postRequest({
"op": "id-get",
"token": token,
"tag": tag
})
return apiResponse
@staticmethod
def postIdDelete(token, tag):
apiResponse = PogiApi._postRequest({
"op": "id-delete",
"token": token,
"tag": tag
})
return apiResponse
@staticmethod
def postIdAdd(token):
payload = {
"addHistory": "true",
"op": "id-add",
"token": token,
"name": "Test Tag",
"external_id": "408189-0000000" + str(randint(100, 999)),
"tagId": "2FAA000000000000000" + str(randint(10000, 99999))
}
apiResponse = PogiApi._postRequest(payload)
return apiResponse
@staticmethod
def postIdUpdate(token, tag, field, value):
payload = {
"addHistory": "true",
"addMissing": "false",
"op": "id-update",
"tagId": tag,
"token": token,
field: value
}
apiResponse = PogiApi._postRequest(payload)
return apiResponse
# Ask for action
def inputAction():
print("\n==================================================")
print("1. Fetch items (id-current)")
print("2. Fetch item history (id-history)")
print("3. Fetch item details (id-get)")
print("4. Add item (id-add)")
print("5. Update item (id-update)")
print("6. Delete item (id-delete)")
print("0. Exit")
op = input("Enter op: (0-6): ")
return op
def displayResponse(apiResponse):
fmtjson = json.dumps(apiResponse, sort_keys=True, indent=2)
print(fmtjson)
# =========================================
# Main routine
# =========================================
if __name__ == "__main__":
if not user_id or not user_pwd:
print('Missing username and password. Please update the following variables in pogiApi-cli.py:')
print('1. user_id')
print('2. user_pwd')
print('These information should be available on your welcome email.')
sys.exit()
token = PogiApi.getToken(user_id, user_pwd)
if not token:
sys.exit()
while True:
action = inputAction()
if action == "1":
apiResponse = PogiApi.postIdCurrent(token)
displayResponse(apiResponse)
elif action == "2":
apiResponse = PogiApi.postIdHistory(token)
displayResponse(apiResponse)
elif action == "3":
tag = input("Enter Tag/RFID: ")
apiResponse = PogiApi.postIdGet(token, tag)
displayResponse(apiResponse)
elif action == "4":
apiResponse = PogiApi.postIdAdd(token)
displayResponse(apiResponse)
elif action == "5":
tag = input("Enter Tag/RFID: ")
field = input("Enter field: ")
newValue = input("Enter new value: ")
apiResponse = PogiApi.postIdUpdate(token, tag, field, newValue)
displayResponse(apiResponse)
elif action == "6":
tag = input("Enter Tag/RFID: ")
apiResponse = PogiApi.postIdDelete(token, tag)
displayResponse(apiResponse)
elif action == "0":
break