Skip to content

Commit ee54486

Browse files
Merge pull request #48 from gadomski/feature/47-stac-url-environ
Fall back to `STAC_URL` environment variable when opening a `Client`
2 parents 77d95a2 + 7e0aaf8 commit ee54486

File tree

4 files changed

+139
-5
lines changed

4 files changed

+139
-5
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- `Client.open` falls back to the `STAC_URL` environment variable if no url is provided as an argument [#48](https://github.com/stac-utils/pystac-client/pull/48)
12+
913
## [v0.1.1] - 2021-04-16
1014

1115
### Added

pystac_client/client.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from copy import deepcopy
2+
import os
23
from typing import Callable, Optional
34
from urllib.request import Request
45

@@ -75,28 +76,37 @@ def __repr__(self):
7576
return '<Catalog id={}>'.format(self.id)
7677

7778
@classmethod
78-
def open(cls, url, headers=None):
79+
def open(cls, url=None, headers=None):
7980
"""Alias for PySTAC's STAC Object `from_file` method
8081
8182
Parameters
8283
----------
83-
url : str
84-
The URL of a STAC Catalog
84+
url : str, optional
85+
The URL of a STAC Catalog. If not specified, this will use the `STAC_URL` environment variable.
8586
8687
Returns
8788
-------
8889
catalog : Client
8990
"""
9091
import pystac_client.stac_io
9192

93+
if url is None:
94+
url = os.environ.get("STAC_URL")
95+
96+
if url is None:
97+
raise TypeError(
98+
"'url' must be specified or the 'STAC_URL' environment variable must be set.")
99+
92100
def read_text_method(url):
93101
request = Request(url, headers=headers or {})
94102
return pystac_client.stac_io.read_text_method(request)
95103

96104
old_read_text_method = STAC_IO.read_text_method
97105
STAC_IO.read_text_method = read_text_method
98-
catalog = cls.from_file(url)
99-
STAC_IO.read_text_method = old_read_text_method
106+
try:
107+
catalog = cls.from_file(url)
108+
finally:
109+
STAC_IO.read_text_method = old_read_text_method
100110
catalog.headers = headers
101111
return catalog
102112

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
interactions:
2+
- request:
3+
body: null
4+
headers:
5+
Connection:
6+
- close
7+
Host:
8+
- eod-catalog-svc-prod.astraea.earth
9+
User-Agent:
10+
- Python-urllib/3.9
11+
method: GET
12+
uri: https://eod-catalog-svc-prod.astraea.earth
13+
response:
14+
body:
15+
string: '{"conformsTo":["http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/core","http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/oas30","http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/geojson","http://www.opengis.net/spec/ogcapi_common-2/1.0/req/collections","http://stacspec.org/spec/api/1.0.0-beta.1/core","http://stacspec.org/spec/api/1.0.0-beta.1/req/stac-search","http://stacspec.org/spec/api/1.0.0-beta.1/req/stac-response","http://stacspec.org/spec/api/1.0.0-beta.1/req/fields","http://stacspec.org/spec/api/1.0.0-beta.1/req/context","http://stacspec.org/spec/api/1.0.0-beta.1/req/sort"],"description":"Astraea
16+
Earth OnDemand geospatial imagery query and analysis tool","id":"astraea","links":[{"href":"https://eod-catalog-svc-prod.astraea.earth","rel":"self","title":"Self","type":"application/json"},{"href":"https://eod-catalog-svc-prod.astraea.earth","rel":"root","title":"Root","type":"application/json"},{"href":"https://eod-catalog-svc-prod.astraea.earth/api","rel":"service-desc","title":"OpenAPI
17+
specification","type":"application/json"},{"href":"https://eod-catalog-svc-prod.astraea.earth/api.html","rel":"service-doc","title":"API
18+
documentation","type":"text/html"},{"href":"https://eod-catalog-svc-prod.astraea.earth/collections","rel":"data","title":"Collections","type":"application/json"},{"href":"https://eod-catalog-svc-prod.astraea.earth/collections","rel":"collections","title":"Collections","type":"application/json"},{"href":"https://eod-catalog-svc-prod.astraea.earth/collections/landsat8_l1tp","rel":"child","title":"Landsat
19+
8 C1 T1","type":"application/json"},{"href":"https://eod-catalog-svc-prod.astraea.earth/collections/mcd43a4","rel":"child","title":"MCD43A4
20+
NBAR","type":"application/json"},{"href":"https://eod-catalog-svc-prod.astraea.earth/collections/mod11a1","rel":"child","title":"MOD11A1
21+
LST","type":"application/json"},{"href":"https://eod-catalog-svc-prod.astraea.earth/collections/myd11a1","rel":"child","title":"MYD11A1
22+
LST","type":"application/json"},{"href":"https://eod-catalog-svc-prod.astraea.earth/collections/mod13a1","rel":"child","title":"MOD13A1
23+
VI","type":"application/json"},{"href":"https://eod-catalog-svc-prod.astraea.earth/collections/myd13a1","rel":"child","title":"MYD13A1
24+
VI","type":"application/json"},{"href":"https://eod-catalog-svc-prod.astraea.earth/collections/sentinel1_l1c_grd","rel":"child","title":"Sentinel-1
25+
L1C GRD","type":"application/json"},{"href":"https://eod-catalog-svc-prod.astraea.earth/collections/sentinel2_l2a","rel":"child","title":"Sentinel-2
26+
L2A","type":"application/json"},{"href":"https://eod-catalog-svc-prod.astraea.earth/collections/sentinel2_l1c","rel":"child","title":"Sentinel-2
27+
L1C","type":"application/json"},{"href":"https://eod-catalog-svc-prod.astraea.earth/collections/naip","rel":"child","title":"NAIP","type":"application/json"},{"href":"https://eod-catalog-svc-prod.astraea.earth/collections/maxar_open_data","rel":"child","title":"Maxar
28+
Open Data","type":"application/json"},{"href":"https://eod-catalog-svc-prod.astraea.earth/collections/spacenet7","rel":"child","title":"SpaceNet
29+
7","type":"application/json"},{"href":"https://eod-catalog-svc-prod.astraea.earth/aggregate","rel":"aggregation","title":"Aggregation","type":"application/json"},{"capabilities":{"parameters":[{"name":"composite","values":[{"description":"Default
30+
View.","name":"default","title":"Default View"},{"description":"True Color
31+
Composite.","name":"true_color","title":"True Color Composite"},{"description":"Normalized
32+
Difference Vegetation Index.","name":"ndvi","title":"NDVI"},{"description":"Normalized
33+
Difference Water Index using Green and NIR per McFeeters(1997).","name":"ndwi","title":"NDWI
34+
(Green & NIR)"},{"description":"Normalized Difference Water Index using NIR
35+
and SWIR per Gao(1997).","name":"ndwi2","title":"NDWI (NIR & SWIR)"},{"description":"Color
36+
Infrared (Vegetation) consisting of Near-infrared, Red, and Green bands.","name":"color_infrared_veg1","title":"Color
37+
Infrared Vegetation (NRG)"},{"description":"Color Infrared (Vegetation) consisting
38+
of Near-infrared, Green, and Blue bands.","name":"color_infrared_veg2","title":"Color
39+
Infrared Vegetation 2 (NGB)"},{"description":"False Color Infrared (Urban).","name":"false_color_urban","title":"False
40+
Color (Urban)"},{"description":"Agriculture.","name":"agriculture","title":"Agriculture"},{"description":"Moisture
41+
Index.","name":"moisture_index","title":"Moisture Index"},{"description":"Geology.","name":"geology","title":"Geology"},{"description":"Bathymetric.","name":"bathymetric","title":"Bathymetric"},{"description":"Atmospheric
42+
Penetration.","name":"atmospheric_penetration","title":"Atmospheric Penetration"},{"description":"SWIR
43+
1.","name":"swir1","title":"SWIR 1"},{"description":"SWIR 2.","name":"swir2","title":"SWIR
44+
2"},{"description":"SAR VV Polarization.","name":"sar_vv","title":"SAR VV
45+
Polarization"},{"description":"SAR VH Polarization.","name":"sar_vh","title":"SAR
46+
VH Polarization"},{"description":"SAR HH Polarization.","name":"sar_hh","title":"SAR
47+
HH Polarization"},{"description":"SAR HV Polarization.","name":"sar_hv","title":"SAR
48+
HV Polarization"},{"description":"SAR False Color (Urban).","name":"sar_false_color_urban","title":"SAR
49+
False Color (Urban)"}]}]},"href":"https://eod-catalog-svc-prod.astraea.earth/aggregate","rel":"aggregate","title":"Aggregate","type":"application/json"},{"capabilities":{"parameters":[{"name":"composite","values":[{"description":"Default
50+
View.","name":"default","title":"Default View"},{"description":"True Color
51+
Composite.","name":"true_color","title":"True Color Composite"},{"description":"Normalized
52+
Difference Vegetation Index.","name":"ndvi","title":"NDVI"},{"description":"Normalized
53+
Difference Water Index using Green and NIR per McFeeters(1997).","name":"ndwi","title":"NDWI
54+
(Green & NIR)"},{"description":"Normalized Difference Water Index using NIR
55+
and SWIR per Gao(1997).","name":"ndwi2","title":"NDWI (NIR & SWIR)"},{"description":"Color
56+
Infrared (Vegetation) consisting of Near-infrared, Red, and Green bands.","name":"color_infrared_veg1","title":"Color
57+
Infrared Vegetation (NRG)"},{"description":"Color Infrared (Vegetation) consisting
58+
of Near-infrared, Green, and Blue bands.","name":"color_infrared_veg2","title":"Color
59+
Infrared Vegetation 2 (NGB)"},{"description":"False Color Infrared (Urban).","name":"false_color_urban","title":"False
60+
Color (Urban)"},{"description":"Agriculture.","name":"agriculture","title":"Agriculture"},{"description":"Moisture
61+
Index.","name":"moisture_index","title":"Moisture Index"},{"description":"Geology.","name":"geology","title":"Geology"},{"description":"Bathymetric.","name":"bathymetric","title":"Bathymetric"},{"description":"Atmospheric
62+
Penetration.","name":"atmospheric_penetration","title":"Atmospheric Penetration"},{"description":"SWIR
63+
1.","name":"swir1","title":"SWIR 1"},{"description":"SWIR 2.","name":"swir2","title":"SWIR
64+
2"},{"description":"SAR VV Polarization.","name":"sar_vv","title":"SAR VV
65+
Polarization"},{"description":"SAR VH Polarization.","name":"sar_vh","title":"SAR
66+
VH Polarization"},{"description":"SAR HH Polarization.","name":"sar_hh","title":"SAR
67+
HH Polarization"},{"description":"SAR HV Polarization.","name":"sar_hv","title":"SAR
68+
HV Polarization"},{"description":"SAR False Color (Urban).","name":"sar_false_color_urban","title":"SAR
69+
False Color (Urban)"}]}]},"href":"https://eod-catalog-svc-prod.astraea.earth/search","rel":"search","title":"Search","type":"application/json"},{"href":"https://eod-catalog-svc-prod.astraea.earth/grid_id_definitions?20200218","rel":"grid_id_definitions","title":"Grid
70+
ID Definitions","type":"application/json"}],"stac_extensions":["eo","proj","context"],"stac_version":"1.0.0-beta.2","title":"Astraea
71+
Earth OnDemand"}'
72+
headers:
73+
Access-Control-Allow-Credentials:
74+
- 'true'
75+
Access-Control-Allow-Headers:
76+
- X-Astraea-Catalog-Client
77+
- Authorization, Content-Type, X-Requested-With
78+
Access-Control-Allow-Origin:
79+
- '*'
80+
Cache-Control:
81+
- no-cache, no-store, must-revalidate
82+
Connection:
83+
- close
84+
Content-Length:
85+
- '7768'
86+
Content-Type:
87+
- application/json
88+
Date:
89+
- Mon, 03 May 2021 15:27:20 GMT
90+
Server:
91+
- akka-http/10.2.3
92+
status:
93+
code: 200
94+
message: OK
95+
version: 1

tests/test_client.py

+25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime
2+
import os
23

34
from dateutil.tz import tzutc
45
import pystac
@@ -96,6 +97,30 @@ def test_from_file(self):
9697

9798
assert api.title == 'Astraea Earth OnDemand'
9899

100+
@pytest.mark.vcr
101+
def test_environment_variable(self):
102+
old_stac_url = os.environ.get("STAC_URL")
103+
os.environ["STAC_URL"] = ASTRAEA_URL
104+
try:
105+
client = Client.open()
106+
assert client.title == "Astraea Earth OnDemand"
107+
finally:
108+
if old_stac_url:
109+
os.environ["STAC_URL"] = old_stac_url
110+
else:
111+
del os.environ["STAC_URL"]
112+
113+
def test_no_url(self):
114+
old_stac_url = os.environ.get("STAC_URL")
115+
if old_stac_url:
116+
del os.environ["STAC_URL"]
117+
try:
118+
with pytest.raises(TypeError):
119+
Client.open()
120+
finally:
121+
if old_stac_url:
122+
os.environ["STAC_URL"] = old_stac_url
123+
99124

100125
class TestAPISearch:
101126
@pytest.fixture(scope='function')

0 commit comments

Comments
 (0)