How can I expose a function through an API?

We have a library function that takes around one hour to run and return. I cannot modify the function. We want to expose the function through a web API so that remote clients can call it. How Can i do it?

When you say you can’t modify the function, do you mean that its functionality (or location) can’t be edited, or that you can’t access it?

it means that its functionality can’t be edited.

Could you post a sample page that use this function?

If he could post a sample page that uses the function, he’s already made an API, because it called the function and gave the output :stuck_out_tongue:

If the function really takes an hour to run, A: Dear god don’t expose it to anyone but extremely trusted clients, because whatever it is will bog down your system with multiple calls. B: Try and optimize it to work better, cause that’s a LONG wait.

I think what you’d want is an API that allows requests to be made for this function to be queued - then you can allow clients to query the API for the job status, and provide an endpoint for them to retrieve the results when the job is completed.

As @m_hutley pointed out, I’d also make sure that it’s behind some kind of access control. You want to be able to regulate who can call the API, how many jobs they can submit in a given period of time, etc, to avoid your servers being overloaded.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.