Skip to content

Commit

Permalink
Clean up query code
Browse files Browse the repository at this point in the history
  • Loading branch information
dstansby committed Aug 29, 2022
1 parent 924cac5 commit 564dc7a
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions sunpy_soar/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,16 @@ def search(self, *query, **kwargs):
return qrt

@staticmethod
def _construct_url(query):
def _construct_payload(query):
"""
Construct search URL.
Construct search payload.
Parameters
----------
query : list[str]
List of query items.
payload : dict[str]
Payload to send to the TAP server.
"""
base_url = ('http://soar.esac.esa.int/soar-sl-tap/tap/'
'sync?REQUEST=doQuery&')
# Need to manually set the intervals based on a query
request_dict = {}
request_dict['LANG'] = 'ADQL'
request_dict['FORMAT'] = 'json'

# Construct ADQL query
url_query = {}
url_query['SELECT'] = '*'
# Assume science data by deafult
Expand All @@ -57,14 +51,14 @@ def _construct_url(query):
url_query['FROM'] = 'v_ll_data_item'

url_query['WHERE'] = '+AND+'.join(query)
request_dict['QUERY'] = '+'.join([f'{item}+{url_query[item]}' for
item in url_query])
adql_query = '+'.join([f'{item}+{url_query[item]}' for item in url_query])

request_str = ''
request_str = [f'{item}={request_dict[item]}' for item in request_dict]
request_str = '&'.join(request_str)
payload = {'REQUEST': 'doQuery',
'LANG': 'ADQL',
'FORMAT': 'json',
'QUERY': adql_query}

return base_url + request_str
return payload

@staticmethod
def _do_search(query):
Expand All @@ -81,10 +75,13 @@ def _do_search(query):
astropy.table.QTable
Query results.
"""
url = SOARClient._construct_url(query)
log.debug(f'Getting request from URL: {url}')
tap_endpoint = 'http://soar.esac.esa.int/soar-sl-tap/tap'
payload = SOARClient._construct_payload(query)
# Need to force requests to not form-encode the paramaters
payload = '&'.join([f'{key}={val}' for key, val in payload.items()])
# Get request info
r = requests.get(url)
r = requests.get(f'{tap_endpoint}/sync', params=payload)
log.debug(f'Sent query: {r.url}')
r.raise_for_status()

# Do some list/dict wrangling
Expand Down

0 comments on commit 564dc7a

Please sign in to comment.