Skip to content

Commit

Permalink
Merge pull request #138 from descarteslabs/feat/service-baseurl
Browse files Browse the repository at this point in the history
Simplify service calls and reuse sessions
  • Loading branch information
samskillman authored Jul 5, 2017
2 parents 59175cd + 3cc17ef commit 9a82ec2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 55 deletions.
27 changes: 9 additions & 18 deletions descarteslabs/services/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ def sources(self):
[{'product': 'landsat:LC08:PRE:TOAR', 'sat_id': 'LANDSAT_8'}]
"""
r = self.session.get('%s/sources' % self.url, timeout=self.TIMEOUT)

r = self.session.get('/sources')
return r.json()

def bands(self, limit=None, products=None, offset=None, wavelength=None, resolution=None, tags=None):
Expand All @@ -78,8 +77,8 @@ def bands(self, limit=None, products=None, offset=None, wavelength=None, resolut
for param in params
if args[param] is not None
}
r = self.session.post('%s/bands/search' % self.url, json=kwargs, timeout=self.TIMEOUT)

r = self.session.post('/bands/search', json=kwargs)
return r.json()

def products(self, bands=None, limit=None, offset=None):
Expand Down Expand Up @@ -141,7 +140,7 @@ def products(self, bands=None, limit=None, offset=None):
if args[param] is not None
}

r = self.session.post('%s/products/search' % self.url, json=kwargs, timeout=self.TIMEOUT)
r = self.session.post('/products/search', json=kwargs)

return r.json()

Expand All @@ -156,7 +155,7 @@ def available_products(self):
['landsat:LC08:PRE:TOAR']
"""
r = self.session.get('%s/products' % self.url, timeout=self.TIMEOUT)
r = self.session.get('/products')

return r.json()

Expand Down Expand Up @@ -217,14 +216,12 @@ def summary(self, products=None, const_id=None, sat_id=None, date='acquired', pa
kwargs = {}

if sat_id:

if isinstance(sat_id, string_types):
sat_id = [sat_id]

kwargs['sat_id'] = sat_id

if products:

if isinstance(products, string_types):
products = [products]

Expand Down Expand Up @@ -267,8 +264,7 @@ def summary(self, products=None, const_id=None, sat_id=None, date='acquired', pa
if params:
kwargs['params'] = json.dumps(params)

r = self.session.post('%s/summary' % self.url, json=kwargs, timeout=self.TIMEOUT)

r = self.session.post('/summary', json=kwargs)
return r.json()

def search(self, products=None, const_id=None, sat_id=None, date='acquired', place=None,
Expand Down Expand Up @@ -379,7 +375,7 @@ def search(self, products=None, const_id=None, sat_id=None, date='acquired', pla
if sort_order is not None:
kwargs['sort_order'] = sort_order

r = self.session.post('%s/search' % self.url, json=kwargs, timeout=self.TIMEOUT)
r = self.session.post('/search', json=kwargs)

return {'type': 'FeatureCollection', "features": r.json()}

Expand Down Expand Up @@ -501,11 +497,9 @@ def features(self, products=None, const_id=None, sat_id=None, date='acquired', p
params=params, dltile=dltile)

offset = 0

count = summary['count']

while offset < count:

features = self.search(sat_id=sat_id, products=products, const_id=None,
date=date, place=place, geom=geom,
start_time=start_time, end_time=end_time,
Expand Down Expand Up @@ -540,8 +534,7 @@ def get(self, key):
'sat_id', 'solar_azimuth_angle', 'solar_elevation_angle', 'sw_version', 'terrain_correction',
'tile_id']
"""
r = self.session.get('%s/get/%s' % (self.url, key), timeout=self.TIMEOUT)

r = self.session.get('/get/%s' % key)
return r.json()

def get_product(self, product_id):
Expand Down Expand Up @@ -614,8 +607,7 @@ def get_product(self, product_id):
'title': 'Landsat 8'}
"""
r = self.session.get('%s/products/%s' % (self.url, product_id), timeout=self.TIMEOUT)

r = self.session.get('/products/%s' % product_id)
return r.json()

def get_band(self, band_id):
Expand All @@ -624,6 +616,5 @@ def get_band(self, band_id):
:param str band_id: Band Identifier.
"""
r = self.session.get('%s/bands/%s' % (self.url, band_id), timeout=self.TIMEOUT)

r = self.session.get('/bands/%s' % band_id)
return r.json()
12 changes: 4 additions & 8 deletions descarteslabs/services/places.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ def placetypes(self):
['continent', 'country', 'dependency', 'macroregion', 'region',
'district', 'mesoregion', 'microregion', 'county', 'locality']
"""
r = self.session.get('%s/placetypes' % self.url, timeout=self.TIMEOUT)

r = self.session.get('/placetypes')
return r.json()

@cachedmethod(operator.attrgetter('cache'), key=partial(hashkey, 'find'))
Expand All @@ -68,8 +67,7 @@ def find(self, path, **kwargs):
'placetype': 'country',
'slug': 'africa_morocco'}]
"""
r = self.session.get('%s/find/%s' % (self.url, path), params=kwargs, timeout=self.TIMEOUT)

r = self.session.get('/find/%s' % path, params=kwargs)
return r.json()

@cachedmethod(operator.attrgetter('cache'), key=partial(hashkey, 'shape'))
Expand Down Expand Up @@ -100,8 +98,7 @@ def shape(self, slug, output='geojson', geom='low'):
'slug': 'north-america_united-states_kansas'}
"""
r = self.session.get('%s/shape/%s.%s' % (self.url, slug, output), params={'geom': geom}, timeout=self.TIMEOUT)

r = self.session.get('/shape/%s.%s' % (slug, output), params={'geom': geom})
return r.json()

@cachedmethod(operator.attrgetter('cache'), key=partial(hashkey, 'prefix'))
Expand All @@ -126,7 +123,6 @@ def prefix(self, slug, output='geojson', placetype=None, geom='low'):
if placetype:
params['placetype'] = placetype
params['geom'] = geom
r = self.session.get('%s/prefix/%s.%s' % (self.url, slug, output),
params=params, timeout=self.TIMEOUT)
r = self.session.get('/prefix/%s.%s' % (slug, output), params=params)

return r.json()
31 changes: 7 additions & 24 deletions descarteslabs/services/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ def get_bands_by_key(self, key):
:return: A dictionary of band entries and their metadata.
"""
r = self.session.get('%s/bands/key/%s' % (self.url, key), timeout=self.TIMEOUT)

if r.status_code != 200:
raise RuntimeError("%s: %s" % (r.status_code, r.text))
r = self.session.get('/bands/key/%s' % key)

return r.json()

Expand All @@ -62,12 +59,7 @@ def get_bands_by_constellation(self, const):
:return: A dictionary of band entries and their metadata.
"""
r = self.session.get('%s/bands/constellation/%s' % (self.url, const),
timeout=self.TIMEOUT)

if r.status_code != 200:
raise RuntimeError("%s: %s" % (r.status_code, r.text))

r = self.session.get('/bands/constellation/%s' % const)
return r.json()

def dltiles_from_shape(self, resolution, tilesize, pad, shape):
Expand Down Expand Up @@ -117,12 +109,7 @@ def dltiles_from_shape(self, resolution, tilesize, pad, shape):
'shape': shape,
}

r = self.session.post('%s/dlkeys/from_shape' % (self.url),
json=params, timeout=self.TIMEOUT)

if r.status_code != 200:
raise RuntimeError("%s: %s" % (r.status_code, r.text))

r = self.session.post('/dlkeys/from_shape', json=params)
return r.json()

def dltile_from_latlon(self, lat, lon, resolution, tilesize, pad):
Expand Down Expand Up @@ -165,8 +152,7 @@ def dltile_from_latlon(self, lat, lon, resolution, tilesize, pad):
'pad': pad,
}

r = self.session.get('%s/dlkeys/from_latlon/%f/%f' % (self.url, lat, lon),
params=params, timeout=self.TIMEOUT)
r = self.session.get('/dlkeys/from_latlon/%f/%f' % (lat, lon), params=params)

return r.json()

Expand Down Expand Up @@ -201,7 +187,7 @@ def dltile(self, key):
'type': 'Feature'}
"""

r = self.session.get('%s/dlkeys/%s' % (self.url, key), timeout=self.TIMEOUT)
r = self.session.get('/dlkeys/%s' % key)

return r.json()

Expand Down Expand Up @@ -309,10 +295,7 @@ def raster(
else:
params['dltile'] = dltile

r = self.session.post('%s/raster' % (self.url), json=params, timeout=self.TIMEOUT)

if r.status_code != 200:
raise RuntimeError("%s: %s" % (r.status_code, r.text))
r = self.session.post('/raster', json=params)

json_resp = r.json()
# Decode base64
Expand Down Expand Up @@ -410,7 +393,7 @@ def ndarray(
else:
params['dltile'] = dltile

r = self.session.post('%s/npz' % (self.url), json=params, timeout=self.TIMEOUT)
r = self.session.post('/npz', json=params)

io = BytesIO(r.content)
npz = np.load(io)
Expand Down
19 changes: 14 additions & 5 deletions descarteslabs/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@


class WrappedSession(requests.Session):
def __init__(self, base_url, timeout=None):
self.base_url = base_url
self.timeout = timeout
super(WrappedSession, self).__init__()

def request(self, method, url, **kwargs):
resp = super(WrappedSession, self).request(method, url, **kwargs)
if self.timeout and 'timeout' not in kwargs:
kwargs['timeout'] = self.timeout

resp = super(WrappedSession, self).request(method, self.base_url + url, **kwargs)

if resp.status_code == 200:
return resp
Expand All @@ -48,10 +56,12 @@ class Service:

def __init__(self, url, token):
self.auth = descarteslabs.descartes_auth
self.url = url
self.base_url = url
if token:
self.auth._token = token

self.session = self.build_session()

@property
def token(self):
return self.auth.token
Expand All @@ -60,9 +70,8 @@ def token(self):
def token(self, token):
self.auth._token = token

@property
def session(self):
s = WrappedSession()
def build_session(self):
s = WrappedSession(self.base_url, timeout=self.TIMEOUT)

retries = Retry(total=5,
read=2,
Expand Down

0 comments on commit 9a82ec2

Please sign in to comment.