OceanNetworksCanada / api-python-client

Provides easy access to ONC data in Python
https://oceannetworkscanada.github.io/api-python-client/
Apache License 2.0
10 stars 9 forks source link

dataProductDelivery service raises TypeError when no data available #3

Closed Jacob-Stevens-Haas closed 10 months ago

Jacob-Stevens-Haas commented 1 year ago

(Created from email ticket)

Description

When requesting data products during an interval when no data exists, the onc package raises a TypeError: sting indices must be integers

Expected Behavior

Either raise a more meaningful error, return a request dictionary with a null id

Actual behavior/MWE:

MWE:

from onc.onc import ONC
onc = ONC(<your token>)
request = onc.requestDataProduct(
    filters={
        "dataProductCode": "AD",
        "extension": "wav",
        "dateFrom": "2016-01-01T12:00:00.000Z",
        "dateTo": "2016-01-01T12:01:00.000Z",
        "deviceCode": "ICLISTENHF1251",
        "dpo_hydrophoneDataDiversionMode": "OD",
        "dpo_audioDownsample": -1,
    }
)

Raises:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/home/jmsh/github/tehom/env/lib/python3.7/site-packages/onc/onc.py", line 99, in requestDataProduct
    return self.delivery.requestDataProduct(filters)
  File "/home/jmsh/github/tehom/env/lib/python3.7/site-packages/onc/modules/_OncDelivery.py", line 62, in requestDataProduct
    self._printProductRequest(response)
  File "/home/jmsh/github/tehom/env/lib/python3.7/site-packages/onc/modules/_OncDelivery.py", line 235, in _printProductRequest
    print('Request Id: {:d}'.format(response['dpRequestId']))
TypeError: string indices must be integers

Discussion:

The error results because

  1. _OncDelivery.requestDataProduct() dispatches to OncService.doRequest() (link)
  2. _OncService.doRequest() gets a HTTP200 response with json content that is just a string, in violation of the API (link)
  3. That request gets passed back to _OncDelivery.requestDataProduct()...
  4. which is then passed on to _OncService._printProductRequest()
  5. _OncService._printProductRequest() then queries response['dpRequestId'], but response is a string, raising the TypeError (link)

The API is clear that the server should be returning HTTP 400 with an error code of 33 and the message "No data found for data search with given parameters". If it's easier to fix the client library rather than the server, I could PR a stopgap where doRequest checks the json response for a top-level string rather than a dict, and raises a ValueError appropriately. Thoughts?

kan-fu commented 11 months ago

The bug has been fixed in the backend. Could you double check to see if the issue could be closed?

Jacob-Stevens-Haas commented 11 months ago

It works and causes the exception in the proper place. However, I still think the error should be a ValueError (as noted in (this SO post)[https://stackoverflow.com/a/30855949/534674]. the else exception could just use Response.raise_for_status()

kan-fu commented 11 months ago

Yeah, I agree. Pylint gave me a lot of broad-exception-caught warning. These exceptions could be narrowed down to give a more meaningful error. I am not experienced in dealing with Exceptions. Do you want to create a new issue for this enhancement?