Documentation for the Python API

Hi,

I am receiving an error when calling

res = orthanc.RestApiGet(“/series/” + seriesId)

from within our Python plugin code.

It throws an “Internal error” exception.

Are there any docs on what errors and exceptions the RestApiGet call can return? or any recommended code or examples of how to correctly handle all the possible errors.

In this case I think the error is because the seriesId does not exist in Orthanc. I’m not sure I would except this to throw an exception though.

Thanks in advance.

Darren

Hello,

The Python SDK for Orthanc plugin is automatically wrapped from the C SDK:
https://sdk.orthanc-server.com/

So, if you don’t know how to use a Python primitive, check out the documentation of the corresponding C function. Also note that the Orthanc Book contains multiple working samples:
https://book.orthanc-server.com/plugins/python.html

Regarding your “Internal error” exception, running Orthanc in “–verbose” should help you understand the issue. You can evidently try/catch the exception.

Sébastien-

Hello,

I think that Darren is trying to recover specific http response codes. For example, in his case a NotFoundException can be useful in order to handle inexistent items while fetching the REST API.
InternalError is too generic, you don’t know if the item was not found, or maybe is a 403 error and it is almost impossible to make decisions depending on http response code.

So, Darren, to solve this problem I had to install request python module inside docker orthanc image (requests==2.25.1 - tested and working fine) and implement a custom api client for some endpoints:

def request(method, authorization, url, content_type, custom_headers, data = {}):
headers
= {
‘token’: str(authorization),
‘Content-Type’: content_type
}
headers_.update(custom_headers)

try:
response = requests.request(method, str(url), headers=headers_, data=data)
status_code = response.status_code
if not is_2xx_success(status_code):
print(f’PY-EVENTS > ORTHANC-API :: ERROR :: Cannot {method} " {url} ", request status code was {status_code}')

implement logic here

return response
except requests.exceptions.RequestException as e:
return None

Also, the response object is returned to be used later. For example you can recover custom headers from the server.
This method will be a little bit slower because of network round trips.

Have a nice day!

Hello again,

I have just added a new “orthanc.OrthancException” class that is raised whenever an error is encountered (instead of built-in exception “ValueError”):
https://hg.orthanc-server.com/orthanc-python/rev/e7ff5efb100d

The corresponding explanations have been added to the Orthanc Book:
https://book.orthanc-server.com/plugins/python.html#catching-exceptions

In your case, you should get the error code 17, that corresponds to the “orthanc.ErrorCode.UNKNOWN_RESOURCE”, if trying to access a non-existing series.

This feature will be part of forthcoming release 3.3 of the Python plugin for Orthanc.

HTH,
Sébastien-

Thanks.

I look forward to trying this.

Darren