Skip to content

Commit f4f251b

Browse files
committed
Merge pull request #518 from keflavich/template_async
Update template module
2 parents 85fd465 + 2fadaeb commit f4f251b

File tree

1 file changed

+39
-76
lines changed

1 file changed

+39
-76
lines changed

astroquery/template_module/core.py

Lines changed: 39 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,29 @@
3232
class DummyClass(BaseQuery):
3333

3434
"""
35-
Not all the methods below are necessary but these cover most of the common cases, new methods may be added if necessary, follow the guidelines at <http://astroquery.readthedocs.org/en/latest/api.html>
35+
Not all the methods below are necessary but these cover most of the common
36+
cases, new methods may be added if necessary, follow the guidelines at
37+
<http://astroquery.readthedocs.org/en/latest/api.html>
3638
"""
3739
# use the Configuration Items imported from __init__.py to set the URL, TIMEOUT, etc.
3840
URL = conf.server
3941
TIMEOUT = conf.timeout
4042

41-
def query_object(self, object_name, get_query_payload=False, verbose=False):
43+
# all query methods are implemented with an "async" method that handles
44+
# making the actual HTTP request and returns the raw HTTP response, which
45+
# should be parsed by a separate _parse_result method. The query_object
46+
# method is created by async_to_sync automatically. It would look like
47+
# this:
48+
"""
49+
def query_object(object_name, get_query_payload=False)
50+
response = self.query_object_async(object_name, get_query_payload=get_query_payload)
51+
if get_query_payload:
52+
return response
53+
result = self._parse_result(response, verbose=verbose)
54+
return result
55+
"""
56+
57+
def query_object_async(self, object_name, get_query_payload=False):
4258
"""
4359
This method is for services that can parse object names. Otherwise
4460
use :meth:`astroquery.template_module.DummyClass.query_region`.
@@ -59,52 +75,16 @@ def query_object(self, object_name, get_query_payload=False, verbose=False):
5975
6076
Returns
6177
-------
62-
result : `~astropy.table.Table`
63-
The result of the query as a `~astropy.table.Table` object.
64-
All queries other than image queries should typically return
65-
results like this.
78+
response : `requests.Response`
79+
The HTTP response returned from the service.
80+
All async methods should return the raw HTTP response.
6681
6782
Examples
6883
--------
6984
While this section is optional you may put in some examples that
7085
show how to use the method. The examples are written similar to
7186
standard doctests in python.
72-
"""
7387
74-
# typically query_object should have the following steps:
75-
# 1. call the corresponding query_object_async method, and
76-
# get the HTTP response of the query
77-
# 2. check if 'get_query_payload' is set to True, then
78-
# simply return the dict of HTTP request parameters.
79-
# 3. otherwise call the parse_result method on the
80-
# HTTP response returned by query_object_async and
81-
# return the result parsed as astropy.Table
82-
# These steps are filled in below, but may be replaced
83-
# or modified as required.
84-
85-
response = self.query_object_async(object_name, get_query_payload=get_query_payload)
86-
if get_query_payload:
87-
return response
88-
result = self._parse_result(response, verbose=verbose)
89-
return result
90-
91-
# all query methods usually have a corresponding async method
92-
# that handles making the actual HTTP request and returns the
93-
# raw HTTP response, which should be parsed by a separate
94-
# parse_result method. Since these async counterparts take in
95-
# the same parameters as the corresponding query methods, but
96-
# differ only in the return value, they should be decorated with
97-
# prepend_docstr_noreturns which will automatically generate
98-
# the common docs. See below for an example.
99-
100-
@prepend_docstr_noreturns(query_object.__doc__)
101-
def query_object_async(self, object_name, get_query_payload=False):
102-
"""
103-
Returns
104-
-------
105-
response : `requests.Response`
106-
The HTTP response returned from the service.
107-
All async methods should return the raw HTTP response.
10888
"""
10989
# the async method should typically have the following steps:
11090
# 1. First construct the dictionary of the HTTP request params.
@@ -132,14 +112,10 @@ def query_object_async(self, object_name, get_query_payload=False):
132112

133113
if get_query_payload:
134114
return request_payload
135-
# commons.send_request takes 4 parameters - the URL to query, the dict of
136-
# HTTP request parameters we constructed above, the TIMEOUT which we imported
137-
# from __init__.py and the type of HTTP request - either 'GET' or 'POST', which
138-
# defaults to 'GET'.
139-
response = commons.send_request(self.URL,
140-
request_payload,
141-
self.TIMEOUT,
142-
request_type='GET')
115+
# BaseQuery classes come with a _request method that includes a
116+
# built-in caching system
117+
response = self._request('GET', self.URL, params=request_payload,
118+
timeout=self.TIMEOUT, cache=cache)
143119
return response
144120

145121
# For services that can query coordinates, use the query_region method.
@@ -149,7 +125,12 @@ def query_object_async(self, object_name, get_query_payload=False):
149125
# the keywords 'width' and 'height' should be used instead. The coordinates
150126
# may be accepted as an `astropy.coordinates` object or as a
151127
# string, which may be further parsed.
152-
def query_region(self, coordinates, radius, width, height, get_query_payload=False, verbose=False):
128+
129+
# similarly we write a query_region_async method that makes the
130+
# actual HTTP request and returns the HTTP response
131+
132+
def query_region_async(self, coordinates, radius, height, width,
133+
get_query_payload=False, cache=True):
153134
"""
154135
Queries a region around the specified coordinates.
155136
@@ -168,27 +149,6 @@ def query_region(self, coordinates, radius, width, height, get_query_payload=Fal
168149
verbose : bool, optional
169150
Display VOTable warnings or not.
170151
171-
Returns
172-
-------
173-
result : `~astropy.table.Table`
174-
The result of the query as a `~astropy.table.Table` object.
175-
All queries other than image queries should typically return
176-
results like this.
177-
"""
178-
# the documentation and code are similar to the steps outlined in the
179-
# `query_object` method, but a rough sketch is provided below
180-
181-
response = self.query_region_async(coordinates, radius, height, width,
182-
get_query_payload=get_query_payload)
183-
result = self._parse_result(response, verbose=verbose)
184-
return result
185-
186-
# similarly we write a query_region_async method that makes the
187-
# actual HTTP request and returns the HTTP response
188-
189-
@prepend_docstr_noreturns(query_region.__doc__)
190-
def query_region_async(self, coordinates, radius, height, width, get_query_payload=False):
191-
"""
192152
Returns
193153
-------
194154
response : `requests.Response`
@@ -198,10 +158,8 @@ def query_region_async(self, coordinates, radius, height, width, get_query_paylo
198158
request_payload = self._args_to_payload(coordinates, radius, height, width)
199159
if get_query_payload:
200160
return request_payload
201-
response = commons.send_request(self.URL,
202-
request_payload,
203-
self.TIMEOUT,
204-
request_type='GET')
161+
response = self._request('GET', self.URL, params=request_payload,
162+
timeout=self.TIMEOUT, cache=cache)
205163
return response
206164

207165
# as we mentioned earlier use various python regular expressions, etc
@@ -233,7 +191,12 @@ def _parse_result(self, response, verbose=False):
233191
# return raw result/ handle in some way
234192
pass
235193

236-
# for image queries, the results should be returned as a
194+
# Image queries do not use the async_to_sync approach: the "synchronous"
195+
# version must be defined explicitly. The example below therefore presents
196+
# a complete example of how to write your own synchronous query tools if
197+
# you prefer to avoid the automatic approach.
198+
#
199+
# For image queries, the results should be returned as a
237200
# list of `astropy.fits.HDUList` objects. Typically image queries
238201
# have the following method family:
239202
# 1. get_images - this is the high level method that interacts with

0 commit comments

Comments
 (0)