Start a new topic

Accessing Endpoint from Collection Hook

I can't find an example of accessing a Custom Endpoint from within a Collection Hook. The HTML5 guides show that Endpoints are accessed via the Kinvey.execute method. In Business Logic there is no Kinvey object.



So, how can I access a custom endpoint from within a pre/post collection hook?

You would have to make a REST call to it - http://devcenter.kinvey.com/rest/guides/business-logic#invoking-endpoint

Be careful not to make a cycle, as your backend will be blacklisted (denied access to BL for 1hr).

Looking at the docs for REST API, I see that I can post to the End Point. So, I played around in the API console and was able to successfully hit my endpoint.



However.... This is not really a good solution. the POST requires 2 critical things. The first is 'authorization'. The second is 'appKey'. In my collection hook, I could hardcode these values in. However, it will all fail when I push my code to production. Right now, I'm in the "development" environment. So, the 'authorization' and 'appKey' are for development. If I hardcode them into my collection hook, they will not work when I go to production.



Actually, they MIGHT work, but the PRODUCTION collection hook will be querying the DEVELOPMENT endpoint. This is a recipe for disaster.



So.. I hope Kinvey has a better solution.
Ivan - You must have answered while I was typing my "answer". Please see my issues with using a REST call to it.
The appKey you can dynamically get from http://devcenter.kinvey.com/rest/reference/business-logic/reference.html#backendcontext-module

For Authorization you have two options - just "forward" the incoming authorization header, in which case the endpoint will be called with the same credentials as the hook, or just do basic auth with the appKey:masterSecret. (BasicAuth doc - http://devcenter.kinvey.com/rest/guides/security#basicauthentication)



I guess we could provide wrappers to make this dead simple, but it doesn't feel too difficult?

Ivan,



I've struggled with this for 2 days. I can't come up with a onPreSave collection hook that calls my custom End point. See here. http://support.kinvey.com/hc/communities/public/questions/200359896-Working-Example-of-The-Request-Module-Using-POST-



If you've got a sample, of something that works, that would be amazingly useful.



Thanks,

Justin
Consider this closed. See http://support.kinvey.com/hc/communities/public/questions/200359896-Working-Example-of-The-Request-Module-Using-POST-
Login or Signup to post a comment