Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up query code #47

Merged
merged 2 commits into from
Sep 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions sunpy_soar/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,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 @@ -60,14 +54,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 @@ -84,10 +78,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()])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, if you need this, doesn't this defeat the purpose of having the payload as a dict? What is form-encoded that should not be form-encoded? I guess the '+'s from the ADQL query, but these could be spaces in the first place (then encoded to '+'s; for example, I have code in which the ADQL query is built by f"SELECT filename FROM v_sc_data_item WHERE (instrument='SPICE') AND (insertion_time>='{date_range_str[0]}') AND (insertion_time<='{date_range_str[1]}')"). Anything else?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, unfortunately I think spaces or + characters are form encoded, so neither works. I think this PR is still an improvement though, so thanks for suggesting :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using urllib.parse.urlencode rather than doing this manually would be a small improvement.

# 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