Skip to content

Commit 4a3f214

Browse files
authored
Merge pull request #2193 from jaymedina/monkey-patch-services
monkey patch services
2 parents c933c3d + 45d7ab8 commit 4a3f214

File tree

3 files changed

+43
-35
lines changed

3 files changed

+43
-35
lines changed

astroquery/mast/discovery_portal.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,23 @@ class PortalAPI(BaseQuery):
116116
Should be used to facilitate all Portal API queries.
117117
"""
118118

119+
MAST_REQUEST_URL = conf.server + "/api/v0/invoke"
120+
COLUMNS_CONFIG_URL = conf.server + "/portal/Mashup/Mashup.asmx/columnsconfig"
121+
MAST_DOWNLOAD_URL = conf.server + "/api/v0.1/Download/file"
122+
MAST_BUNDLE_URL = conf.server + "/api/v0.1/Download/bundle"
123+
124+
TIMEOUT = conf.timeout
125+
PAGESIZE = conf.pagesize
126+
127+
_column_configs = dict()
128+
_current_service = None
129+
119130
def __init__(self, session=None):
120131

121132
super(PortalAPI, self).__init__()
122133
if session:
123134
self._session = session
124135

125-
self.MAST_REQUEST_URL = conf.server + "/api/v0/invoke"
126-
self.COLUMNS_CONFIG_URL = conf.server + "/portal/Mashup/Mashup.asmx/columnsconfig"
127-
self.MAST_DOWNLOAD_URL = conf.server + "/api/v0.1/Download/file"
128-
self.MAST_BUNDLE_URL = conf.server + "/api/v0.1/Download/bundle"
129-
130-
self.TIMEOUT = conf.timeout
131-
self.PAGESIZE = conf.pagesize
132-
133-
self._column_configs = dict()
134-
self._current_service = None
135-
136136
def _request(self, method, url, params=None, data=None, headers=None,
137137
files=None, stream=False, auth=None, retrieve_all=True):
138138
"""
@@ -482,6 +482,7 @@ def _get_columnsconfig_metadata(self, colconf_name):
482482
headers = {"User-Agent": self._session.headers["User-Agent"],
483483
"Content-type": "application/x-www-form-urlencoded",
484484
"Accept": "text/plain"}
485+
485486
response = self._request("POST", self.COLUMNS_CONFIG_URL,
486487
data=("colConfigId={}".format(colconf_name)), headers=headers)
487488

astroquery/mast/observations.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
This module contains various methods for querying MAST observations.
77
"""
88

9-
109
import warnings
1110
import json
1211
import time
@@ -38,7 +37,6 @@
3837
from . import conf, utils
3938
from .core import MastQueryWithLogin
4039

41-
4240
__all__ = ['Observations', 'ObservationsClass',
4341
'MastClass', 'Mast']
4442

@@ -51,6 +49,13 @@ class ObservationsClass(MastQueryWithLogin):
5149
Class for querying MAST observational data.
5250
"""
5351

52+
# Calling static class variables
53+
_caom_all = 'Mast.Caom.All'
54+
_caom_cone = 'Mast.Caom.Cone'
55+
_caom_filtered_position = 'Mast.Caom.Filtered.Position'
56+
_caom_filtered = 'Mast.Caom.Filtered'
57+
_caom_products = 'Mast.Caom.Products'
58+
5459
def _parse_result(self, responses, verbose=False): # Used by the async_to_sync decorator functionality
5560
"""
5661
Parse the results of a list of `~requests.Response` objects and returns an `~astropy.table.Table` of results.
@@ -82,7 +87,7 @@ def list_missions(self):
8287
"""
8388

8489
# getting all the histogram information
85-
service = "Mast.Caom.All"
90+
service = self._caom_all
8691
params = {}
8792
response = self._portal_api_connection.service_request_async(service, params, format='extjs')
8893
json_response = response[0].json()
@@ -112,9 +117,9 @@ def get_metadata(self, query_type):
112117
"""
113118

114119
if query_type.lower() == "observations":
115-
colconf_name = "Mast.Caom.Cone"
120+
colconf_name = self._caom_cone
116121
elif query_type.lower() == "products":
117-
colconf_name = "Mast.Caom.Products"
122+
colconf_name = self._caom_products
118123
else:
119124
raise InvalidQueryError("Unknown query type.")
120125

@@ -152,14 +157,14 @@ def _parse_caom_criteria(self, **criteria):
152157

153158
# Build the mashup filter object and store it in the correct service_name entry
154159
if coordinates or objectname:
155-
mashup_filters = self._portal_api_connection.build_filter_set("Mast.Caom.Cone",
156-
"Mast.Caom.Filtered.Position",
157-
**criteria)
160+
mashup_filters = self._portal_api_connection.build_filter_set(self._caom_cone,
161+
self._caom_filtered_position,
162+
**criteria)
158163
coordinates = utils.parse_input_location(coordinates, objectname)
159164
else:
160-
mashup_filters = self._portal_api_connection.build_filter_set("Mast.Caom.Cone",
161-
"Mast.Caom.Filtered",
162-
**criteria)
165+
mashup_filters = self._portal_api_connection.build_filter_set(self._caom_cone,
166+
self._caom_filtered,
167+
**criteria)
163168

164169
# handle position info (if any)
165170
position = None
@@ -168,7 +173,7 @@ def _parse_caom_criteria(self, **criteria):
168173
# if radius is just a number we assume degrees
169174
radius = coord.Angle(radius, u.deg)
170175

171-
# build the coordinates string needed by Mast.Caom.Filtered.Position
176+
# build the coordinates string needed by ObservationsClass._caom_filtered_position
172177
position = ', '.join([str(x) for x in (coordinates.ra.deg, coordinates.dec.deg, radius.deg)])
173178

174179
return position, mashup_filters
@@ -209,7 +214,7 @@ def query_region_async(self, coordinates, radius=0.2*u.deg, pagesize=None, page=
209214
# if radius is just a number we assume degrees
210215
radius = coord.Angle(radius, u.deg)
211216

212-
service = 'Mast.Caom.Cone'
217+
service = self._caom_cone
213218
params = {'ra': coordinates.ra.deg,
214219
'dec': coordinates.dec.deg,
215220
'radius': radius.deg}
@@ -286,12 +291,12 @@ def query_criteria_async(self, pagesize=None, page=None, **criteria):
286291
raise InvalidQueryError("At least one non-positional criterion must be supplied.")
287292

288293
if position:
289-
service = "Mast.Caom.Filtered.Position"
294+
service = self._caom_filtered_position
290295
params = {"columns": "*",
291296
"filters": mashup_filters,
292297
"position": position}
293298
else:
294-
service = "Mast.Caom.Filtered"
299+
service = self._caom_filtered
295300
params = {"columns": "*",
296301
"filters": mashup_filters}
297302

@@ -322,7 +327,7 @@ def query_region_count(self, coordinates, radius=0.2*u.deg, pagesize=None, page=
322327
response : int
323328
"""
324329

325-
# build the coordinates string needed by Mast.Caom.Filtered.Position
330+
# build the coordinates string needed by ObservationsClass._caom_filtered_position
326331
coordinates = commons.parse_coordinates(coordinates)
327332

328333
# if radius is just a number we assume degrees
@@ -331,7 +336,7 @@ def query_region_count(self, coordinates, radius=0.2*u.deg, pagesize=None, page=
331336
# turn coordinates into the format
332337
position = ', '.join([str(x) for x in (coordinates.ra.deg, coordinates.dec.deg, radius.deg)])
333338

334-
service = "Mast.Caom.Filtered.Position"
339+
service = self._caom_filtered_position
335340
params = {"columns": "COUNT_BIG(*)",
336341
"filters": [],
337342
"position": position}
@@ -399,12 +404,12 @@ def query_criteria_count(self, pagesize=None, page=None, **criteria):
399404

400405
# send query
401406
if position:
402-
service = "Mast.Caom.Filtered.Position"
407+
service = self._caom_filtered_position
403408
params = {"columns": "COUNT_BIG(*)",
404409
"filters": mashup_filters,
405410
"position": position}
406411
else:
407-
service = "Mast.Caom.Filtered"
412+
service = self._caom_filtered
408413
params = {"columns": "COUNT_BIG(*)",
409414
"filters": mashup_filters}
410415

@@ -440,7 +445,7 @@ def get_product_list_async(self, observations):
440445
if len(observations) == 0:
441446
raise InvalidQueryError("Observation list is empty, no associated products.")
442447

443-
service = 'Mast.Caom.Products'
448+
service = self._caom_products
444449
params = {'obsid': ','.join(observations)}
445450

446451
return self._portal_api_connection.service_request_async(service, params)

astroquery/mast/services.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,16 @@ class ServiceAPI(BaseQuery):
101101
Should be used to facilitate all microservice API queries.
102102
"""
103103

104+
SERVICE_URL = conf.server
105+
REQUEST_URL = conf.server + "/api/v0.1/"
106+
SERVICES = {}
107+
104108
def __init__(self, session=None):
105109

106110
super().__init__()
107111
if session:
108112
self._session = session
109113

110-
self.REQUEST_URL = conf.server + "/api/v0.1/"
111-
self.SERVICES = {}
112-
113114
self.TIMEOUT = conf.timeout
114115

115116
def set_service_params(self, service_dict, service_name="", server_prefix=False):
@@ -128,7 +129,7 @@ def set_service_params(self, service_dict, service_name="", server_prefix=False)
128129
vs. the default of mast.stsci.edu/service_name
129130
"""
130131

131-
service_url = conf.server
132+
service_url = self.SERVICE_URL
132133
if server_prefix:
133134
service_url = service_url.replace("mast", f"{service_name}.mast")
134135
else:
@@ -248,6 +249,7 @@ def service_request_async(self, service, params, page_size=None, page=None, **kw
248249
compiled_service_args[service_argument] = found_argument.lower()
249250

250251
request_url = self.REQUEST_URL + service_url.format(**compiled_service_args)
252+
251253
headers = {
252254
'User-Agent': self._session.headers['User-Agent'],
253255
'Content-type': 'application/x-www-form-urlencoded',

0 commit comments

Comments
 (0)