-
Notifications
You must be signed in to change notification settings - Fork 1
/
evrythng.py
executable file
·846 lines (662 loc) · 18.8 KB
/
evrythng.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
#!/usr/bin/python
# EVRYTHNG API Python Wrapper v0.92 - Vlad Trifa
# Engine 1.17
# Import all basic libs needed
import simplejson as json
import httplib, urllib
import csv
import logging
# Import some tools to measure execution time
import time
import corestats
# Set to 1 to force HTTPS
SECURE=1
# Which API Endpoint
hostname="api.evrythng.com"
# The Various API Keys
apiKey="YOUR_OPERATOR_KEY_HERE"
appId="YOUR_APP_ID"
userId="YOUR_USER_ID"
inApp=0
logLevel=0
# FIXME: add other log levels here - 0 is debug, 1 is info, 2 is warning, 3 is errors
def setLogLevel(level):
global logLevel
if level:
logging.basicConfig(level=logging.INFO,
format='%(levelname)-8s: %(message)s'
)
logging.info("Simple Log mode")
else:
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)-8s %(filename)s:%(lineno)-4d: %(message)s',
datefmt='%m-%d %H:%M',
)
logging.info("Full log mode")
logLevel=level
### This is to setup the environments / app / user contexts
def setDomain(domain):
global hostname
hostname = domain
logging.info("API Endpoint: " + hostname)
def setOperator(key):
global apiKey
apiKey = key
logging.info("Scope: OPERATOR key: " + apiKey)
headProducts()
# FIXME: add testing right here if works, otherwise return error
def setApp(key,id):
global apiKey,appId
apiKey = key
appId = id
logging.info("Scope: APP id "+ appId +" (APP API KEY: " + apiKey +")")
inApp=1
headProducts()
# FIXME: add testing right here if works, otherwise return error
def setUser(key,id):
global apiKey,userId
apiKey = key
userId = id
logging.info("Scope: USER id "+ userId +" (APP API KEY: " + apiKey +")")
inApp=1
# FIXME: add testing right here if works, otherwise return error
# Print the response of the API
def printResponse(response):
logging.info(response['headers'])
# pretty print response body, if any
if response['body'] != '':
logging.info("Response Content Body: \n"+ json.dumps(json.loads(response['body']), indent=4))
else:
logging.info("No response body.")
# Validates if the API response is the one expected
def validateResponse(response,validCode,errorCode):
# Check if successful
if response[0]['status'] != validCode:
logging.error(errorCode + ". Status code: " + str(response[0]['status']))
logging.error(response[0]['headers'])
logging.error(response[0]['body'])
# Sends a request to the EVRYTHNG Engine
def sendRequest(method, url, body='', headers=''):
global hostname
# By default for all calls
headers['Authorization'] = apiKey
# Use HTTP or HTTPs Connection
if SECURE:
port=443
else:
port=80
# We create a connection
conn = httplib.HTTPSConnection(
host=hostname,
port=port
)
json_body=json.dumps(body)
# Build the HTTP request with the body, headers & co
conn.request(
method=method,
url='%s' % url,
body=json_body,
headers=headers
)
# Send the request
logging.info("-> ### %s %s:%s%s " % (method,hostname,port,url))
logging.info("-> Headers: " + json.dumps(headers))
logging.info("-> Payload: " + json_body)
# Send the request and time it
start = time.time()
full_response = conn.getresponse()
rt = float("%.2f" % ((time.time() - start)*1000)) # Response Time RT (milliseconds)
# Parse the response
response={} # Parse the HTTP response
response['body']=full_response.read()
response['headers']=full_response.getheaders()
response['status']=full_response.status
response['reason']=full_response.reason
conn.close()
# And confirm
logging.info("<- ### %s %s ###" % (response['status'],response['reason']))
logging.info("<- ### RT %0.5f [ms] ###" % rt)
if logLevel==0:
printResponse(response)
return [response, rt]
#---- Implementation of a few endpoints in the engine
def headThngs():
headers = {"Accept": "application/json"}
response = sendRequest(
method="HEAD",
url="/thngs",
headers=headers
)
validateResponse(response,200,"Problem HEAD /thngs")
return response
# GET the list of all THNGS for a user
def getAllThngs(scope=''):
headers = {"Accept": "application/json"}
if scope=='all':
scopedUrl="/thngs?app=all"
elif scope=='':
scopedUrl="/thngs"
else:
scopedUrl="/thngs/?app=%s" % scope
response = sendRequest(
method="GET",
url=scopedUrl,
headers=headers
)
return response
# Gets list of thngs using a filter (filter should be human readable)
def getThngs(filterString):
headers = {"Accept": "application/json"}
response = sendRequest(
method="GET",
url="/thngs/?filter=%s" % urllib.quote_plus(filterString),
headers=headers
)
validateResponse(response,200,"Problem GET /thngs")
return response
# POST a new THNG
def createThng(thngDocument,scope=''):
headers = {"Content-Type": "application/json"}
if scope=='all':
scopedUrl="/thngs?app=all"
elif scope=='':
scopedUrl="/thngs"
else:
scopedUrl="/thngs/?app=%s" % scope
response = sendRequest(
method="POST",
url=scopedUrl,
body=thngDocument,
headers=headers
)
validateResponse(response,201,"Problem POST /thngs")
return response
# GET a THNG
def getThng(thngID):
headers = {"Accept": "application/json"}
response = sendRequest(
method="GET",
url="/thngs/%s" % thngID,
headers=headers
)
validateResponse(response,200,"Problem GET /thngs")
return response
# DELETE a THNG
def deleteThng(thngID):
headers = {"Accept": "application/json"}
response = sendRequest(
method="DELETE",
url="/thngs/%s" % thngID,
headers=headers
)
validateResponse(response,200,"Problem DELETE /products/{id}")
return response
# UPDATE a THNG
def updateThng(thngID,propDocument):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="PUT",
url="/thngs/%s" % thngID,
body=thngDocument,
headers=headers
)
validateResponse(response,200,"Problem PUT /thngs/{id}")
return response
def headProducts():
headers = {"Accept": "application/json"}
response = sendRequest(
method="HEAD",
url="/products",
headers=headers
)
validateResponse(response,200,"Problem HEAD /products")
return response
# POST a new PRODUCT
def createProduct(productDocument,scope='all'):
headers = {"Content-Type": "application/json"}
if scope=='all':
scopedUrl="/products?app=all"
elif scope=='':
scopedUrl="/products"
else:
scopedUrl="/products/?app=%s" % scope
response = sendRequest(
method="POST",
url=scopedUrl,
body=productDocument,
headers=headers
)
validateResponse(response,201,"Problem POST /products")
return response
# GET the list of all THNGS for a user
def getProducts(scope=''):
headers = {"Accept": "application/json"}
if scope=='all':
scopedUrl="/products?app=all"
elif scope=='':
scopedUrl="/products"
else:
scopedUrl="/products/?app=%s" % scope
response = sendRequest(
method="GET",
url=scopedUrl,
headers=headers
)
validateResponse(response,200,"Problem GET /products")
return response
# GET a PRODUCT
def getProduct(productID):
headers = {}
response = sendRequest(
method="GET",
url="/products/%s" % productID,
headers=headers
)
return response
# DELETE a PRODUCT
def deleteProduct(productID):
headers = {}
response = sendRequest(
method="DELETE",
url="/products/%s" % productID,
headers=headers
)
validateResponse(response,200,"Problem DELETE /products")
return response
# UPDATE a PRODUCT
def updateProduct(productID,productDocument):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="PUT",
url="/thngs/%s" % productID,
body=productDocument,
headers=headers
)
return response
# UPDATE PROPERTIES of an entity, by default a THNG
def updateProperties(entityID,propertyDocument,entityPath='/thngs'):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="PUT",
url="%s/%s/properties" % (entityPath,entityID),
body=propertyDocument,
headers=headers
)
return response
# GET LIST of PROPERTIES of a THNG
# Returns array of form ...
def getProperties(entityID,entityPath='/thngs'):
response = sendRequest(
method="GET",
url="%s/%s/properties" % (entityPath,entityID),
headers=headers
)
return response
# GET HISTORY of a single PROPERTY of a THNG
# Returns array of form ...
def getProperty(entityID,propertyID,entityPath='/thngs'):
headers = {"Accept": "application/json"}
response = sendRequest(
method="GET",
url="%s/%s/properties%s" % (entityPath,entityID,propertyID),
headers=headers
)
return response
# GET list of LOCATIONS of a THNG
# Location *must* be an object: location={'latitude': 'some', 'longitude': 'stuff', 'timestamp': 234234}
def getLocations(thngID):
call_url="/thngs/%s/location" % thngID
# if from !=''
# call_url += "?from=%s" % from
# if to !=''
# call_url += "to=%s" % from
response = sendRequest(
method="GET",
url=call_url,
headers=headers
)
return response
# UPDATE LOCATION of a THNG
# Location *must* be an object: location={'latitude': 'some', 'longitude': 'stuff', 'timestamp': 234234}
def updateLocation(thngID,location):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="PUT",
url="/thngs/%s/location" % thngID,
body=location,
headers=headers
)
return response
# CREATE COLLECTION
def createCollection(collDocument):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="POST",
url="/collections",
body=collDocument,
headers=headers
)
validateResponse(response,201,"Problem POST /collections")
return response
# UPDATE COLLECTION
def updateCollection(collId,collDocument):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="PUT",
url="/collections/%s" % collId,
body=collDocument,
headers=headers
)
return response
# GET COLLECTION
def getCollection(collId,headers=''):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="GET",
url="/collections/%s" % collId,
headers=headers
)
validateResponse(response,200,"Problem GET /collections")
return response
# DELETE COLLECTION
def deleteCollection(collId,headers=''):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="DELETE",
url="/collections/%s" % collId,
headers=headers
)
validateResponse(response,200,"Problem DELETE /collections")
return response
# ADD THNGS to COLLECTION
def addToCollection(collID,thngList):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="PUT",
url="/collections/%s/thngs" % collID,
body=thngList,
headers=headers
)
validateResponse(response,200,"Problem PUT /collections/")
return response
# POST a redirection
def createRedirection(thngID,redirectionDocument):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="POST",
url="/thngs/%s/redirector" % thngID,
body=redirectionDocument,
headers=headers
)
validateResponse(response,201,"Problem POST /redirector")
return response
# DELETE a redirection
def deleteRedirection(thngID):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="DELETE",
url="/thngs/%s/redirector" % thngID,
headers=headers
)
validateResponse(response,200,"Problem DELETE /redirector")
return response
# GET the QR code
def getQr(thngID,size,format):
headers = {"Accept": format}
response = sendRequest(
method="GET",
url="/thngs/%s/redirector/qr?h=%s&w=%s" % (thngID,size,size),
headers=headers
)
validateResponse(response,200,"Problem GET /redirector/qr")
return response
# GET the QR code
def getQrTemplated(shortID,size,format,template):
# The endpoint to generate QR codes with templates
headers = {"Accept": format}
response = sendRequest(
method="GET",
url="/redirections/%s.qr?h=%s&w=%s&tpl=%s" % (shortID,size,size,template),
headers=headers,
body='',
domain="tn.gg"
)
validateResponse(response,200,"Problem GET /redirector/qr")
return response
def storeCollectionThngsinCSV(collectionID):
# Do the stuff and let go
return null
############## ACTIONS
### Action types
# GET all action types
# FIXME Allow to give as param the projectId, the thng, the product, the tag(s)
def getActionTypes(scope=''):
if scope=='all':
scopedUrl="/actions?app=all"
elif scope=='':
scopedUrl="/actions"
else:
scopedUrl="/actions?app=%s" % scope
headers = {"Accept": "application/json"}
response = sendRequest(
method="GET",
url=scopedUrl,
headers=headers
)
validateResponse(response,200,"Problem GET /actions")
return response
# POST a new action type
def createActionType(actionTypeDocument,scope=''):
if scope=='all':
scopedUrl="/actions?app=all"
elif scope=='':
scopedUrl="/actions"
else:
scopedUrl="/actions?app=%s" % scope
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="POST",
url=scopedUrl,
body=actionTypeDocument,
headers=headers
)
validateResponse(response,201,"Problem POST /actions")
return response
# DELETE an action type
def deleteActionType(actionType):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="DELETE",
url="/actions/%s" % actionType,
headers=headers
)
validateResponse(response,200,"Problem DELETE /actions/{Type}")
return response
### Actions
def createAction(actionType,actionDocument):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="POST",
url="/actions/%s" % actionType,
body=actionDocument,
headers=headers
)
validateResponse(response,201,"Problem POST /actions/{Type}")
return response
# Attention this returns only 100 actions max
# Allow to give as param the projectId, the thng, the product, the tag(s)
def getActions(actionType):
headers = {"Accept": "application/json"}
response = sendRequest(
method="GET",
url="/actions/%s" % actionType,
headers=headers
)
validateResponse(response,200,"Problem GET /actions/{Type}")
return response
############## APPLICATIONS
# GET /applications --- Returns the list of all Applications
def getAllApplications():
headers = {"Accept": "application/json"}
response = sendRequest(
method="GET",
url="/applications",
headers=headers
)
validateResponse(response,200,"Problem GET /applications")
return response
# GET /applications --- Reads an existing application
def getApplication(appId):
headers = {"Accept": "application/json"}
response = sendRequest(
method="GET",
url="/applications/%s" % appId,
headers=headers
)
validateResponse(response,200,"Problem GET /applications/{id}")
return response
# POST /applications --- Create a new application
def createApplication(appDocument):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="POST",
url="/applications",
body=appDocument,
headers=headers
)
validateResponse(response,201,"Problem POST /applications")
return response
# PUT /applications --- Updates an existing application
def updateApplication(appId,appDocument):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="PUT",
url="/applications/%s" % appId,
body=appDocument,
headers=headers
)
validateResponse(response,200,"Problem PUT /applications/{id}")
return response
# POST /applications --- DELETE a new application
def deleteApplication(appId):
headers = {"Accept": "application/json"}
response = sendRequest(
method="DELETE",
url="/applications/%s" % appId,
headers=headers
)
validateResponse(response,200,"Problem DELETE /applications")
return response
############## USERS
# POST /auth/evrythng/users --- Create a new EVRYTHNG Anonymous user in an APP
def createAnonUser(userDocument):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="POST",
url="/auth/evrythng/users?anonymous=true",
body=userDocument,
headers=headers
)
# FIXME use correct API endpoint & signature
validateResponse(response,201,"Problem POST /auth/evrythng/users")
return response
# POST /auth/evrythng/users --- Create a new EVRYTHNG user in an APP
def createEvtUser(userDocument):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="POST",
url="/auth/evrythng/users",
body=userDocument,
headers=headers
)
validateResponse(response,201,"Problem POST /auth/evrythng/users")
return response
# POST /users/X/validate --- Create a new EVRYTHNG user in an APP
def validateEvtUser(userId,validationDocument):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="POST",
url="/auth/evrythng/users/%s/validate" % userId,
body=validationDocument,
headers=headers
)
validateResponse(response,200,"Problem POST /auth/evrythng/users")
return response
# POST /auth/evrythng/users --- Create a new application
# loginDocument={"email":"XXX","password":"YYY"}
def loginEvtUser(loginDocument):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="POST",
url="/auth/evrythng",
body=loginDocument,
headers=headers
)
validateResponse(response,201,"Problem POST /auth/evrythng/")
return response
# POST FB user
# {"access": {"expires" : <Timestamp>,"token"": <Facebook-Token>}}
def createFbUser(userDocument):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="POST",
url="/auth/facebook",
headers=headers
)
validateResponse(response,201,"Problem POST /auth/evrythng/users")
return response
# POST /logout -- Logs out the user, done using the user api key
def logoutUser():
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="POST",
url="/auth/all/logout",
headers=headers
)
# FIXME PUT instead of POST, 200 instead of 201
validateResponse(response,201,"Problem POST /auth/all/logout")
return response
# GET /users/X -- reads data about 1 user
def getUser(userId):
headers = {"Content-Type": "application/json"}
response = sendRequest(
method="GET",
url="/users/%s" % userId,
headers=headers
)
validateResponse(response,200,"Problem GET /users/X")
return response
# GET /users/ -- reads all users in a given app (or all apps)
def getUsers(appId=0):
headers = {"Accept": "application/json"}
if appId == 0:
userScope="/users"
else:
userScope="/users/?app="+str(appId)
response = sendRequest(
method="GET",
url=userScope,
headers=headers
)
validateResponse(response,200,"Problem GET /users")
return response
# TOOLS
# Reads a product file
def importProducts(filename):
products=[]
data = csv.reader(open(filename, 'rb'), delimiter=',', quotechar='"')
fields = data.next()
for row in data:
items = zip(fields, row)
products.append(dict(zip(fields, row)))
logging.info("Imported " + str(len(products)) + " products from:" + filename)
return products
# Not Done Not Tested
def importCsvData(filename):
output=[]
data = csv.reader(open(filename, 'rb'), delimiter=',', quotechar='"')
fields = data.next() # Gets the first column
for row in data:
output.append(dict(zip(fields, row)))
logging.info("Imported " + str(len(output)) + " entities from:" + filename)
return output