Accessing C++ Rest Endpoints from Overrides

Dear Orthanc Users,

I’m currently working on a Python plugin, and I’ve run into a use-case where I want to override the default server behavior for a few edge-cases but in most instances (>95%) , I just want to return the standard server response.

Based on the Python plugin documentation (https://book.orthanc-server.com/plugins/python.html), I know how to register a handler for a specific endpoint, what I don’t know: Is there a way to access the behavior of the default endpoint (in effect call super) and then pass that data back in my response?

Basically, what I’d like to accomplish:

95% of the time

if not (condition):
return orthanc.RestApiGet(‘/endpoint’, …)

Execute my logic

Any suggestions would be greatly appreciated.

Cheers,
Rob Oakes

Hi Rob,

This is actually the default behaviour. Here’s a sample in which I override the tools/find and returns the “super” answer (the aim of course is to modify the answer from “super”)

def OnRestToolsFind(output, uri, **request):
print(‘Accessing uri in python: %s’ % uri)
pprint.pprint(request)

if request[‘method’] != ‘POST’:
output.SendMethodNotAllowed(‘POST’)
else:
query = json.loads(request[‘body’])

answers = orthanc.RestApiPost(‘/tools/find’, json.dumps(query))
output.AnswerBuffer(answers, ‘application/json’)

orthanc.RegisterRestCallback(‘/tools/find’, OnRestToolsFind)

At the opposite, if you want to call the “plugin flavored” endpoint, you should use RestApiPostAfterPlugins()

HTH

Alain.

Hi Alain,

Thank you for posting this example, it’s exactly what I needed!

Cheers,
Rob

Hi Alain,

Using your example and the other plugin documentation, I’ve been able to implement about 95% of the functionality I need, but I’m struggling with a related issue: How do you send an answer buffer with a response code other than 200 status code? (I can’t find a way of feeding additional parameters to out.AnswerBuffer.)

As an example of how this might be used: is there a way to send a JSON body with an error message with a 400 error code? This is a pattern that is common for many REST APIs where the client submitted an incorrect parameter.

Cheers
Rob

Hi Rob,

Here’s a sample. It’s a bit tricky and weird that we need to pass the length of the string → this might be improved in the future …

def OnRestToolsFind(output, uri, **request):
print(‘Accessing uri in python: %s’ % uri)
pprint.pprint(request)

if request[‘method’] != ‘POST’:
output.SendMethodNotAllowed(‘POST’)
else:
query = json.loads(request[‘body’])

answers = json.loads(orthanc.RestApiPost(‘/tools/find’, json.dumps(query)))
if False:
output.AnswerBuffer(answers, ‘application/json’)
else:
output.SetHttpHeader(“Content-Type”, ‘application/json’)
output.SendHttpStatus(201, json.dumps(answers), len(json.dumps(answers)))

HTH

Alain

Thanks Alain :slight_smile: