7373# this enables preservation of marker-symbol when moving
7474# an existing marker
7575# 5-30-20 TMG fix #2: v1.1.0: send signed requests to sartopo.com (online)
76+ # 6-2-20 TMG v1.1.1: fix #5 (use correct meaning of 'expires');
77+ # fix #6 (__init__ returns None on failure)
7678#
7779#-----------------------------------------------------------------------------
7880
8486import os
8587
8688class SartopoSession ():
87- def __init__ (self ,domainAndPort = "localhost:8080" ,mapID = None ,configpath = None ,account = None ,id = None ,key = None , expires = None ):
89+ def __init__ (self ,domainAndPort = "localhost:8080" ,mapID = None ,configpath = None ,account = None ,id = None ,key = None ):
8890 self .s = requests .session ()
8991 self .apiVersion = - 1
9092 if not mapID or not isinstance (mapID ,str ) or len (mapID )< 3 :
9193 print ("ERROR: you must specify a three-or-more-character sartopo map ID string (end of the URL) when opening a SartopoSession object." )
92- return - 1
94+ return None
9395 self .mapID = mapID
9496 self .domainAndPort = domainAndPort
95- # configpath, account, id, key, and expires are used to build
97+ # configpath, account, id, and key are used to build
9698 # signed requests for sartopo.com
9799 self .configpath = configpath
98100 self .account = account
99101 self .id = id
100102 self .key = key
101- self .expires = expires
102103 self .setupSession ()
103104
104105 def setupSession (self ):
105106 if "sartopo.com" in self .domainAndPort .lower ():
106107 id = None
107108 key = None
108- expires = None
109109 # if configpath and account are specified,
110110 # conigpath must be the full pathname of a configparser-compliant
111111 # config file, and account must be the name of a section within it,
112- # containing keys 'id', 'key', and 'expires '.
112+ # containing keys 'id' and 'key '.
113113 # otherwise, those parameters must have been specified in this object's
114114 # constructor.
115115 # if both are specified, first the config section is read and then
@@ -129,9 +129,8 @@ def setupSession(self):
129129 section = config [self .account ]
130130 id = section .get ("id" ,None )
131131 key = section .get ("key" ,None )
132- expires = section .get ("expires" ,None )
133- if id is None or key is None or expires is None :
134- print ("account entry '" + self .account + "' in config file '" + self .configpath + "' is not complete:\n it must specify id, key, and expires." )
132+ if id is None or key is None :
133+ print ("account entry '" + self .account + "' in config file '" + self .configpath + "' is not complete:\n it must specify id and key." )
135134 return - 1
136135 else :
137136 print ("specified config file '" + self .configpath + "' does not exist." )
@@ -142,21 +141,15 @@ def setupSession(self):
142141 id = self .id
143142 if self .key is not None :
144143 key = self .key
145- if self .expires is not None :
146- expires = self .expires
147144 # finally, save them back as parameters of this object
148145 self .id = id
149146 self .key = key
150- self .expires = int (expires )
151147
152148 if self .id is None :
153149 print ("sartopo session is invalid: 'id' must be specified for online maps" )
154150 return - 1
155151 if self .key is None :
156152 print ("sartopo session is invalid: 'key' must be specified for online maps" )
157- return - 1
158- if self .expires is None :
159- print ("sartopo session is invalid: 'expires' must be specified for online maps" )
160153 return - 1
161154
162155 # by default, do not assume any sartopo session is running;
@@ -234,13 +227,14 @@ def sendRequest(self,type,apiUrlEnd,j,id="",returnJson=None):
234227 params = {}
235228 params ["json" ]= json .dumps (j )
236229 if "sartopo.com" in self .domainAndPort .lower ():
237- data = "POST " + mid + apiUrlEnd + "\n " + str (self .expires )+ "\n " + json .dumps (j )
230+ expires = int (time .time ()* 1000 )+ 120000 # 2 minutes from current time, in milliseconds
231+ data = "POST " + mid + apiUrlEnd + "\n " + str (expires )+ "\n " + json .dumps (j )
238232# print("pre-hashed data:"+data)
239233 token = hmac .new (base64 .b64decode (self .key ),data .encode (),'sha256' ).digest ()
240234 token = base64 .b64encode (token ).decode ()
241235# print("hashed data:"+str(token))
242236 params ["id" ]= self .id
243- params ["expires" ]= self . expires
237+ params ["expires" ]= expires
244238 params ["signature" ]= token
245239# print("SENDING POST to '"+url+"':")
246240# print(json.dumps(params,indent=3))
@@ -322,31 +316,4 @@ def getFeatures(self,featureClass=None,since=0):
322316# rval.append([id,prop]) # return all properties
323317
324318 return rval
325-
326- if __name__ == "__main__" :
327- import time
328-
329- sts = SartopoSession ("localhost:8080" ,"VT3" )
330- fid = sts .addFolder ("MyFolder" )
331- sts .addMarker (39 ,- 120 ,"stuff" )
332- sts .addMarker (39.01 ,- 120.01 ,"myStuff" ,folderId = fid )
333- r = sts .getFeatures ("Marker" )
334- print ("r:" + str (r ))
335- print ("moving the marker after a pause:" + r [0 ]['id' ])
336- time .sleep (5 )
337- sts .addMarker (39.02 ,- 120.02 ,r [0 ]['properties' ]['title' ],existingId = r [0 ]['id' ])
338-
339- # sts2=SartopoSession(
340- # "sartopo.com",
341- # "KSG2",
342- # configpath="../../sts.ini",
343- # account="<account_name>")
344- # fid2=sts2.addFolder("MyOnlineFolder")
345- # sts2.addMarker(39,-120,"onlineStuff")
346- # sts2.addMarker(39.01,-119.99,"onlineStuff2",folderId=fid2)
347- # r2=sts2.getFeatures("Marker")
348- # print("return value from getFeatures('Marker'):")
349- # print(json.dumps(r2,indent=3))
350- # time.sleep(15)
351- # print("moving online after a pause:"+r2[0]['id'])
352- # sts2.addMarker(39.02,-119.98,r2[0]['properties']['title'],existingId=r2[0]['id'])
319+
0 commit comments