class RedisCredStorage(oauth2client.client.Storage): def __init__(self, key, lock, expires): self.r = DARedis() self.key = key self.lockkey = lock self.expires = expires def acquire_lock(self): pipe = self.r.pipeline() pipe.set(self.lockkey, 1) pipe.expire(self.lockkey, 5) pipe.execute() def release_lock(self): self.r.delete(self.lockkey) def locked_get(self): json_creds = self.r.get(self.key) creds = None if json_creds is not None: self.r.expire(self.key, self.expires) json_creds = json_creds.decode() try: creds = oauth2client.client.Credentials.new_from_json(json_creds) except: log("RedisCredStorage: could not read credentials from " + str(json_creds)) return creds def locked_put(self, credentials): if self.expires: pipe = self.r.pipeline() pipe.set(self.key, credentials.to_json()) pipe.expire(self.key, self.expires) pipe.execute() else: self.r.set(self.key, credentials.to_json()) def locked_delete(self): self.r.delete(self.key)
def service_areas(): redis = DARedis() result = redis.get('lsc_service_areas') if result is None: #sys.stderr.write('service_areas: calling arcgis.\n') r = requests.get( 'https://services3.arcgis.com/n7h3cEoHTyNCwjCf/ArcGIS/rest/services/BasicFieldServiceAreas_GrantCycle/FeatureServer/0/query?where=OBJECTID%3E%3D0&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&resultType=none&distance=0.0&units=esriSRUnit_Meter&returnGeodetic=false&outFields=*&returnGeometry=false&returnCentroid=false&multipatchOption=xyFootprint&maxAllowableOffset=&geometryPrecision=&outSR=&datumTransformation=&applyVCSProjection=false&returnIdsOnly=false&returnUniqueIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&returnQueryGeometry=false&returnDistinctValues=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&having=&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&returnExceededLimitFeatures=true&quantizationParameters=&sqlFormat=none&f=pjson&token=' ) if r.status_code != 200: redis.set('lsc_service_areas', '{}') sys.stderr.write( 'load_service_areas: got error code {} from ArcGIS. Response: {}\n' .format(r.status_code, r.text)) else: try: the_dict = r.json() assert 'features' in the_dict assert len(the_dict['features']) > 0 redis.set('lsc_service_areas', r.text) except Exception as the_err: redis.set('lsc_service_areas', '{}') sys.stderr.write( 'load_service_areas: got invalid response from server: {}\n' .format(text_type(the_err))) redis.expire('lsc_service_areas', 60 * 60 * 24 * 7) result = redis.get('lsc_service_areas') return json.loads(result.decode())
def _get_random_unique_id(self): r = DARedis() tries = 10 while tries > 0: key = random_alphanumeric(32) if r.setnx('da:' + self.appname + ':status:uniqueid:' + key, 'None'): r.expire('da:' + self.appname + ':status:uniqueid:' + key, 300) return key tries -= 1 raise Exception("DAOAuth: unable to set a random unique id")