How can I get post REST callback
I heed to his a callback method after all REST call. I have already found a method to call before REST call, but after eludes me. Here's the code:
Class Test.REST Extends %CSP.REST { XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap" ] { <Routes> <Route Url="/:classname" Method="GET" Call="TEST" Cors="true"/> </Routes> } /// This method Gets called prior to dispatch of the request. Put any common code here /// that you want to be executed for EVERY request. If pContinue is set to 0, the /// request will NOT be dispatched according to the UrlMap. If this case it's the /// responsibility of the user to return a response. ClassMethod OnPreDispatch(pUrl As %String, pMethod As %String, ByRef pContinue As %Boolean) As %Status { set ^CacheTemp.DBG($i(^CacheTemp.DBG)) = "OnPreDispatch" set pContinue = $$$YES quit $$$OK } ClassMethod TEST(name) As %Status { set ^CacheTemp.DBG($i(^CacheTemp.DBG)) = "TEST" //w 1/0 quit $$$OK } /// Issue an '500' error and give some indication as to what occurred ClassMethod Http500(pE As %Exception.AbstractException) As %Status { set ^CacheTemp.DBG($i(^CacheTemp.DBG)) = "Http500" quit ##super() } /// Doesn't work ClassMethod OnPostHTTP() [ Abstract, ServerOnly = 1 ] { set ^CacheTemp.DBG($i(^CacheTemp.DBG)) = "OnPostHTTP" } /// Doesn't work ClassMethod OnPostHyperEvent(class As %String, method As %String) As %Status [ ServerOnly = 1 ] { set ^CacheTemp.DBG($i(^CacheTemp.DBG)) = "OnPostHyperEvent" quit $$$OK } }
After I create /test web app with Test.REST and call: http;//host/test/123, ^CacheTemp.DBG looks like this:
^CacheTemp.DBG=2 ^CacheTemp.DBG(1)="OnPreDispatch" ^CacheTemp.DBG(2)="TEST"
How can I define a callback for successful REST call?
My current efforts so far:
Class Test.REST Extends %CSP.REST { XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap" ] { <Routes> <Route Url="/:classname" Method="GET" Call="TEST" Cors="true"/> </Routes> } ClassMethod TEST(name) As %Status { set ^CacheTemp.DBG($i(^CacheTemp.DBG)) = "TEST" set ^DBG($i(^DBG)) = "TEST" //w 1/0 quit $$$OK } /// Issue an '500' error and give some indication as to what occurred ClassMethod Http500(pE As %Exception.AbstractException) As %Status { set %zzzsc = pE.AsStatus() set ^CacheTemp.DBG($i(^CacheTemp.DBG)) = "Http500" set ^DBG($i(^DBG)) = "Http500" quit ##super(pE) } /// Dispatch a REST request according to URL and Method ClassMethod DispatchRequest(pUrl As %String, pMethod As %String, pForwarded As %Boolean = 0) As %Status { #dim %zzzsc As %Status = $$$OK kill ^CacheTemp.DBG, ^DBG tstart set ^CacheTemp.DBG($i(^CacheTemp.DBG)) = "BeforeDispatch" set ^DBG($i(^DBG)) = "BeforeDispatch" set sc = ##super(pUrl, pMethod, pForwarded) set ^CacheTemp.DBG($i(^CacheTemp.DBG)) = "AfterDispatch" set ^DBG($i(^DBG)) = "AfterDispatch" if ($$$ISERR(sc) || $$$ISERR(%zzzsc)) { set ^CacheTemp.DBG($i(^CacheTemp.DBG)) = "trollback" trollback } else { set ^CacheTemp.DBG($i(^CacheTemp.DBG)) = "tcommit" tcommit } quit sc }
It works, but I'm searching for a better solution.
Rollbacked transaction:
Commited transaction:
Hi Eduard,
I would look at implementing the OnPreHTTP() and OnPostHTTP() methods.
Also, if you want to issue a 500 error from the dispatch request it will be to late as the headers will have been written. You will need to do that from OnPreHTTP().
Sean.
OnPostHTTP() doesn't get hit, I have tried it first thing. The sample is in the question.
That's interesting. The REST page is just a %CSP.Page so you would think it would. I'll have a dig around.
Ahhh OK, the Page() method has been overridden so we lose all of the %CSP.Page event methods.
Since there are no event methods the only thing you can do is override either the Page() method or the DispatchRequest(). At least the headers are not written too until after the method call.
I guess what you are doing will be as good as it gets. Only worry is if the implementation changes in a later release.
Ideally the class should have an OnBeforeRequest() and an OnAfterRequest() set of methods.