Question
· Mar 1, 2017

Is it possible to call rest services method in CSP pages?

I'm trying to implement Crud operation in rest services.

I'm confused about this implementation. 

Is it possible to call rest services method in CSP pages?

If not means, can you tell me another way?

I need some examples or reference to implement Crud operations.

Thanks

Discussion (9)0
Log in or sign up to continue

Is it possible?: Yes
Is it a good idea: No

Proof of concept of bad idea:

Class Rest.BadExample Extends %CSP.Page
{

Parameter CHARSET = "utf-8";

Parameter CONTENTTYPE = "application/json";

ClassMethod OnPage() As %Status
{

  Set String = "[ {code:1,description:'cat'},{code:2,description:'lyon'}]"
  Write String

  Quit $$$OK
}

}

As Eduard said, you should make your own class that inherits from %CSP.REST

There are a number of reasons why building a REST service off a CSP page would be bad.  They all come down to that a CSP page does not implement all the features of a "proper" REST service.  REST is really not that difficult in Cache once you get the basics down.  Just to be sure you are looking at the right documentation here is a link to the most recent.  

http://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=...

It is not clear from your question if this is in Cache or Ensemble.  In practice setting up the service itself is best done in Cache even in you are using Ensemble.  EnsLib.REST.Service does not, as of yet, provide a full implementation of the %CSP.REST class that it inherits from.   It WILL work, however there may be some features of %CSP.REST that you can not take advantage of.  I don't have a list of those.  Post a follow up question if that is of interest and the information can be dug up. 

When you look at a REST service call from you need to breakdown the URL to understand how this is implement in Cache.  Take www.somecompany.com/api/v1/person/1,  there are three sections we are worried about

server:  www.somecompany.com

CSP Web Application: /api/v1  - this is setup in the system management portal under security->Applications-

resource being accessed: /person/1 - this is mapped in a Class that inherits from %CSP.REST in an XDATA block as shown below.  The '1' in this case is a data value indicating which person record you want to access and is also part of the mapping.  Here is what that class might look like:

Class Example.RESTservice extends %CSP.REST {

XData UrlMap [ XMLNamespace = "http://www.intersystems.com/urlmap" ]
{
<Routes>
<Route Url="/Person/:ID" Method="Get" Call="RetrievePerson" />
<Route Url="/test" Method="GET" Call="TestService" />
</Routes>
}

Classmethod RetreivePerson(ID as %String) as %Status

{

// code to retreive the person record and format the response which is simple "written" out from this method

//be sure to put a proper HTTP status in the %response.status property and a correct content type

// (ie. application/json) in %response.ContentType

}

}

Note the :ID in the map of the URL. This indicates a data value that is to be passed to the class method that is called.  If there are multiple data values they are passed to each parameter of the method in order they occur.  The name in the URL has no real meaning.  These data parameters can also appear in any place in the URL so www.somecompany.com/api/v1/1/person could also be a valid URL.

Now tie this together by putting your REST class name that you just created (ie Example.RESTservice) in the Dispatch Class field of the web application.  Now anything that is part of the URL that is past the CSP web application will be forwarded to the dispatch class to be handled.

Hope this helps