22import logging
33import re
44from copy import deepcopy
5- from typing import TYPE_CHECKING , Any , Dict , Iterator , List , Optional , Union
5+ from typing import TYPE_CHECKING , Any , Dict , Iterator , List , Optional
66from urllib .parse import urlparse
77
88import pystac
@@ -50,33 +50,19 @@ def __init__(
5050 # TODO - this should super() to parent class
5151 self .session = Session ()
5252 self .session .headers .update (headers or {})
53- self .session .params .update (parameters or {})
53+ self .session .params .update (parameters or {}) # type: ignore
5454
5555 self ._conformance = conformance
5656
57- def read_text (
58- self ,
59- source : Union [str , Link ],
60- * args : Any ,
61- parameters : Optional [Dict [str , Any ]] = None ,
62- ** kwargs : Any ,
63- ) -> str :
57+ def read_text (self , source : pystac .link .HREF , * args : Any , ** kwargs : Any ) -> str :
6458 """Read text from the given URI.
6559
6660 Overwrites the default method for reading text from a URL or file to allow
6761 :class:`urllib.request.Request` instances as input. This method also raises
6862 any :exc:`urllib.error.HTTPError` exceptions rather than catching
6963 them to allow us to handle different response status codes as needed.
7064 """
71- if isinstance (source , str ):
72- href = source
73- if bool (urlparse (href ).scheme ):
74- return self .request (href , * args , parameters = parameters , ** kwargs )
75- else :
76- with open (href ) as f :
77- href_contents = f .read ()
78- return href_contents
79- elif isinstance (source , Link ):
65+ if isinstance (source , Link ):
8066 link = source .to_dict ()
8167 href = link ["href" ]
8268 # get headers and body from Link and add to request from simple STAC
@@ -93,13 +79,26 @@ def read_text(
9379 # If "POST" use the body object that and respect the "merge" property.
9480 link_body = link .get ("body" , {})
9581 if method == "POST" :
96- parameters = {** parameters , ** link_body } if merge else link_body
82+ parameters = (
83+ {** (kwargs .get ("parameters" , {})), ** link_body }
84+ if merge
85+ else link_body
86+ )
9787 else :
9888 # parameters are already in the link href
9989 parameters = {}
90+
10091 return self .request (
101- href , * args , method = method , headers = headers , parameters = parameters
92+ href , method = method , headers = headers , parameters = parameters
10293 )
94+ else : # str or something that can be str'ed
95+ href = str (source )
96+ if bool (urlparse (href ).scheme ):
97+ return self .request (href , * args , ** kwargs )
98+ else :
99+ with open (href ) as f :
100+ href_contents = f .read ()
101+ return href_contents
103102
104103 def request (
105104 self ,
@@ -157,7 +156,7 @@ def write_text_to_href(self, href: str, *args: Any, **kwargs: Any) -> None:
157156 def stac_object_from_dict (
158157 self ,
159158 d : Dict [str , Any ],
160- href : Optional [str ] = None ,
159+ href : Optional [pystac . link . HREF ] = None ,
161160 root : Optional ["Catalog_Type" ] = None ,
162161 preserve_dict : bool = True ,
163162 ) -> "STACObject_Type" :
@@ -181,27 +180,27 @@ def stac_object_from_dict(
181180
182181 # Merge common properties in case this is an older STAC object.
183182 merge_common_properties (
184- d , json_href = href , collection_cache = collection_cache
183+ d , json_href = str ( href ) , collection_cache = collection_cache
185184 )
186185
187186 info = identify_stac_object (d )
188187 d = migrate_to_latest (d , info )
189188
190189 if info .object_type == pystac .STACObjectType .CATALOG :
191190 result = pystac_client .client .Client .from_dict (
192- d , href = href , root = root , migrate = False , preserve_dict = preserve_dict
191+ d , href = str ( href ) , root = root , migrate = False , preserve_dict = preserve_dict
193192 )
194193 result ._stac_io = self
195194 return result
196195
197196 if info .object_type == pystac .STACObjectType .COLLECTION :
198197 return pystac_client .collection_client .CollectionClient .from_dict (
199- d , href = href , root = root , migrate = False , preserve_dict = preserve_dict
198+ d , href = str ( href ) , root = root , migrate = False , preserve_dict = preserve_dict
200199 )
201200
202201 if info .object_type == pystac .STACObjectType .ITEM :
203202 return pystac .Item .from_dict (
204- d , href = href , root = root , migrate = False , preserve_dict = preserve_dict
203+ d , href = str ( href ) , root = root , migrate = False , preserve_dict = preserve_dict
205204 )
206205
207206 raise ValueError (f"Unknown STAC object type { info .object_type } " )
@@ -254,7 +253,8 @@ def conforms_to(self, conformance_class: ConformanceClasses) -> bool:
254253 provides such an endpoint.
255254
256255 Args:
257- key : The ``ConformanceClasses`` key to check conformance against.
256+ conformance_class : The ``ConformanceClasses`` key to check conformance
257+ against.
258258
259259 Return:
260260 bool: Indicates if the API conforms to the given spec or URI.
0 commit comments