astropy / astroquery
1
"""
2
Utility for building URLs for accessing the Gemini Archive RESTful services.
3

4
The Gemini REST API is used behind the query calls so this utility is
5
used internally by those methods to construct appropriate URLs.
6
"""
7 1
import astropy
8 1
from astropy import units
9

10 1
from astroquery.utils import commons
11

12

13 1
def handle_keyword_arg(url, key, value):
14
    """ Handler function for generic keyword argument with no special handling. """
15 1
    return "%s/%s=%s" % (url, key, value)
16

17

18 1
def handle_radius(url, key, radius):
19
    """ Handler function for radius keyword with smart conversion to a degrees value """
20 1
    assert(key == "radius")
21 1
    if isinstance(radius, (int, float)):
22 0
        radius = radius * units.deg
23 1
    radius = astropy.coordinates.Angle(radius)
24 1
    if radius is not None:
25 1
        return "%s/sr=%fd" % (url, radius.deg)
26 0
    return url
27

28

29 1
def handle_coordinates(url, key, coordinates):
30
    """ Handler function for coordinates """
31 1
    assert(key == "coordinates")
32 1
    coordinates = commons.parse_coordinates(coordinates)
33 1
    if coordinates is not None:
34 1
        return "%s/ra=%f/dec=%f" % (url, coordinates.ra.deg, coordinates.dec.deg)
35 0
    return url
36

37

38
"""
39
Dictionary of custom handlers by key.
40

41
This dictionary will pass keyword/value pairs to the appropriate handler, if
42
they are present.  By default, the url will use `__handle_keyword_arg` to
43
add the key/value pair to the URL.
44
"""
45 1
handlers = {
46
    "radius": handle_radius,
47
    "coordinates": handle_coordinates
48
}
49

50

51 1
class URLHelper(object):
52 1
    def __init__(self, server="https://archive.gemini.edu"):
53
        """ Make a URL Helper for building URLs to the Gemini Archive REST service. """
54 1
        if server is None:
55 0
            self.server = "https://archive.gemini.edu"
56 1
        elif not server.lower().startswith("http:") \
57
            and not server.lower().startswith("https:"):
58 0
            self.server = "https://%s" % server
59
        else:
60 1
            self.server = server
61

62 1
    def build_url(self, *args, **kwargs):
63
        """ Build a URL with the given args and kwargs as the query parameters.
64

65
        Parameters
66
        ----------
67
        args : list
68
            The arguments to be passed in the URL without a key.  Each of
69
            these is simply added as another component of the path in the url.
70
        kwargs : dict of key/value parameters for the url
71
            The arguments to be passed in key=value form.
72
        Returns
73
        -------
74
        response : `string` url to execute the query
75
        """
76

77 1
        url = "%s/jsonsummary/notengineering/NotFail" % self.server
78

79 1
        for arg in args:
80 1
            url = "%s/%s" % (url, arg)
81 1
        for key, value in kwargs.items():
82 1
            if key != "orderby":
83 1
                handler = handlers.get(key, handle_keyword_arg)
84 1
                url = handler(url, key, value)
85 1
        if "orderby" in kwargs:
86 0
            url = "%s?orderby=%s" % (url, kwargs["orderby"])
87 1
        return url

Read our documentation on viewing source code .

Loading