Skip to content

Commit 9a82ec2

Browse files
authored
Merge pull request #138 from descarteslabs/feat/service-baseurl
Simplify service calls and reuse sessions
2 parents 59175cd + 3cc17ef commit 9a82ec2

File tree

4 files changed

+34
-55
lines changed

4 files changed

+34
-55
lines changed

descarteslabs/services/metadata.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ def sources(self):
5555
[{'product': 'landsat:LC08:PRE:TOAR', 'sat_id': 'LANDSAT_8'}]
5656
5757
"""
58-
r = self.session.get('%s/sources' % self.url, timeout=self.TIMEOUT)
59-
58+
r = self.session.get('/sources')
6059
return r.json()
6160

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

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

8584
def products(self, bands=None, limit=None, offset=None):
@@ -141,7 +140,7 @@ def products(self, bands=None, limit=None, offset=None):
141140
if args[param] is not None
142141
}
143142

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

146145
return r.json()
147146

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

161160
return r.json()
162161

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

219218
if sat_id:
220-
221219
if isinstance(sat_id, string_types):
222220
sat_id = [sat_id]
223221

224222
kwargs['sat_id'] = sat_id
225223

226224
if products:
227-
228225
if isinstance(products, string_types):
229226
products = [products]
230227

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

270-
r = self.session.post('%s/summary' % self.url, json=kwargs, timeout=self.TIMEOUT)
271-
267+
r = self.session.post('/summary', json=kwargs)
272268
return r.json()
273269

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

382-
r = self.session.post('%s/search' % self.url, json=kwargs, timeout=self.TIMEOUT)
378+
r = self.session.post('/search', json=kwargs)
383379

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

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

503499
offset = 0
504-
505500
count = summary['count']
506501

507502
while offset < count:
508-
509503
features = self.search(sat_id=sat_id, products=products, const_id=None,
510504
date=date, place=place, geom=geom,
511505
start_time=start_time, end_time=end_time,
@@ -540,8 +534,7 @@ def get(self, key):
540534
'sat_id', 'solar_azimuth_angle', 'solar_elevation_angle', 'sw_version', 'terrain_correction',
541535
'tile_id']
542536
"""
543-
r = self.session.get('%s/get/%s' % (self.url, key), timeout=self.TIMEOUT)
544-
537+
r = self.session.get('/get/%s' % key)
545538
return r.json()
546539

547540
def get_product(self, product_id):
@@ -614,8 +607,7 @@ def get_product(self, product_id):
614607
'title': 'Landsat 8'}
615608
616609
"""
617-
r = self.session.get('%s/products/%s' % (self.url, product_id), timeout=self.TIMEOUT)
618-
610+
r = self.session.get('/products/%s' % product_id)
619611
return r.json()
620612

621613
def get_band(self, band_id):
@@ -624,6 +616,5 @@ def get_band(self, band_id):
624616
:param str band_id: Band Identifier.
625617
626618
"""
627-
r = self.session.get('%s/bands/%s' % (self.url, band_id), timeout=self.TIMEOUT)
628-
619+
r = self.session.get('/bands/%s' % band_id)
629620
return r.json()

descarteslabs/services/places.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ def placetypes(self):
4444
['continent', 'country', 'dependency', 'macroregion', 'region',
4545
'district', 'mesoregion', 'microregion', 'county', 'locality']
4646
"""
47-
r = self.session.get('%s/placetypes' % self.url, timeout=self.TIMEOUT)
48-
47+
r = self.session.get('/placetypes')
4948
return r.json()
5049

5150
@cachedmethod(operator.attrgetter('cache'), key=partial(hashkey, 'find'))
@@ -68,8 +67,7 @@ def find(self, path, **kwargs):
6867
'placetype': 'country',
6968
'slug': 'africa_morocco'}]
7069
"""
71-
r = self.session.get('%s/find/%s' % (self.url, path), params=kwargs, timeout=self.TIMEOUT)
72-
70+
r = self.session.get('/find/%s' % path, params=kwargs)
7371
return r.json()
7472

7573
@cachedmethod(operator.attrgetter('cache'), key=partial(hashkey, 'shape'))
@@ -100,8 +98,7 @@ def shape(self, slug, output='geojson', geom='low'):
10098
'slug': 'north-america_united-states_kansas'}
10199
102100
"""
103-
r = self.session.get('%s/shape/%s.%s' % (self.url, slug, output), params={'geom': geom}, timeout=self.TIMEOUT)
104-
101+
r = self.session.get('/shape/%s.%s' % (slug, output), params={'geom': geom})
105102
return r.json()
106103

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

132128
return r.json()

descarteslabs/services/raster.py

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ def get_bands_by_key(self, key):
4747
4848
:return: A dictionary of band entries and their metadata.
4949
"""
50-
r = self.session.get('%s/bands/key/%s' % (self.url, key), timeout=self.TIMEOUT)
51-
52-
if r.status_code != 200:
53-
raise RuntimeError("%s: %s" % (r.status_code, r.text))
50+
r = self.session.get('/bands/key/%s' % key)
5451

5552
return r.json()
5653

@@ -62,12 +59,7 @@ def get_bands_by_constellation(self, const):
6259
6360
:return: A dictionary of band entries and their metadata.
6461
"""
65-
r = self.session.get('%s/bands/constellation/%s' % (self.url, const),
66-
timeout=self.TIMEOUT)
67-
68-
if r.status_code != 200:
69-
raise RuntimeError("%s: %s" % (r.status_code, r.text))
70-
62+
r = self.session.get('/bands/constellation/%s' % const)
7163
return r.json()
7264

7365
def dltiles_from_shape(self, resolution, tilesize, pad, shape):
@@ -117,12 +109,7 @@ def dltiles_from_shape(self, resolution, tilesize, pad, shape):
117109
'shape': shape,
118110
}
119111

120-
r = self.session.post('%s/dlkeys/from_shape' % (self.url),
121-
json=params, timeout=self.TIMEOUT)
122-
123-
if r.status_code != 200:
124-
raise RuntimeError("%s: %s" % (r.status_code, r.text))
125-
112+
r = self.session.post('/dlkeys/from_shape', json=params)
126113
return r.json()
127114

128115
def dltile_from_latlon(self, lat, lon, resolution, tilesize, pad):
@@ -165,8 +152,7 @@ def dltile_from_latlon(self, lat, lon, resolution, tilesize, pad):
165152
'pad': pad,
166153
}
167154

168-
r = self.session.get('%s/dlkeys/from_latlon/%f/%f' % (self.url, lat, lon),
169-
params=params, timeout=self.TIMEOUT)
155+
r = self.session.get('/dlkeys/from_latlon/%f/%f' % (lat, lon), params=params)
170156

171157
return r.json()
172158

@@ -201,7 +187,7 @@ def dltile(self, key):
201187
'type': 'Feature'}
202188
"""
203189

204-
r = self.session.get('%s/dlkeys/%s' % (self.url, key), timeout=self.TIMEOUT)
190+
r = self.session.get('/dlkeys/%s' % key)
205191

206192
return r.json()
207193

@@ -309,10 +295,7 @@ def raster(
309295
else:
310296
params['dltile'] = dltile
311297

312-
r = self.session.post('%s/raster' % (self.url), json=params, timeout=self.TIMEOUT)
313-
314-
if r.status_code != 200:
315-
raise RuntimeError("%s: %s" % (r.status_code, r.text))
298+
r = self.session.post('/raster', json=params)
316299

317300
json_resp = r.json()
318301
# Decode base64
@@ -410,7 +393,7 @@ def ndarray(
410393
else:
411394
params['dltile'] = dltile
412395

413-
r = self.session.post('%s/npz' % (self.url), json=params, timeout=self.TIMEOUT)
396+
r = self.session.post('/npz', json=params)
414397

415398
io = BytesIO(r.content)
416399
npz = np.load(io)

descarteslabs/services/service.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,16 @@
2424

2525

2626
class WrappedSession(requests.Session):
27+
def __init__(self, base_url, timeout=None):
28+
self.base_url = base_url
29+
self.timeout = timeout
30+
super(WrappedSession, self).__init__()
31+
2732
def request(self, method, url, **kwargs):
28-
resp = super(WrappedSession, self).request(method, url, **kwargs)
33+
if self.timeout and 'timeout' not in kwargs:
34+
kwargs['timeout'] = self.timeout
35+
36+
resp = super(WrappedSession, self).request(method, self.base_url + url, **kwargs)
2937

3038
if resp.status_code == 200:
3139
return resp
@@ -48,10 +56,12 @@ class Service:
4856

4957
def __init__(self, url, token):
5058
self.auth = descarteslabs.descartes_auth
51-
self.url = url
59+
self.base_url = url
5260
if token:
5361
self.auth._token = token
5462

63+
self.session = self.build_session()
64+
5565
@property
5666
def token(self):
5767
return self.auth.token
@@ -60,9 +70,8 @@ def token(self):
6070
def token(self, token):
6171
self.auth._token = token
6272

63-
@property
64-
def session(self):
65-
s = WrappedSession()
73+
def build_session(self):
74+
s = WrappedSession(self.base_url, timeout=self.TIMEOUT)
6675

6776
retries = Retry(total=5,
6877
read=2,

0 commit comments

Comments
 (0)