Question
· Apr 26, 2017

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?

Discussion (5)1
Log in or sign up to continue

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:

>zw ^CacheTemp.DBG, ^DBG
^CacheTemp.DBG=5
^CacheTemp.DBG(1)="BeforeDispatch"
^CacheTemp.DBG(2)="TEST"
^CacheTemp.DBG(3)="Http500"
^CacheTemp.DBG(4)="AfterDispatch"
^CacheTemp.DBG(5)="trollback"

 Commited transaction:

>zw ^CacheTemp.DBG,  ^DBG
^CacheTemp.DBG=4
^CacheTemp.DBG(1)="BeforeDispatch"
^CacheTemp.DBG(2)="TEST"
^CacheTemp.DBG(3)="AfterDispatch"
^CacheTemp.DBG(4)="tcommit"
^DBG=3
^DBG(1)="BeforeDispatch"
^DBG(2)="TEST"
^DBG(3)="AfterDispatch"

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.