Skip to content

Commit 071afa6

Browse files
committed
Base REST implementation
1 parent 1cf0f16 commit 071afa6

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

SQLKPI/AbstractREST.cls.xml

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,129 @@
33
<Class name="SQLKPI.AbstractREST">
44
<Super>%CSP.REST</Super>
55
<TimeCreated>64244,40123.382009</TimeCreated>
6+
7+
<Parameter name="UseSession">
8+
<Type>Integer</Type>
9+
<Default>1</Default>
10+
</Parameter>
11+
12+
<Method name="outputStatus">
13+
<Description>
14+
This method takes a status, renders it as jason (if requested) and outputs the result</Description>
15+
<Internal>1</Internal>
16+
<ClassMethod>1</ClassMethod>
17+
<FormalSpec>pSC:%Status</FormalSpec>
18+
<ReturnType>%Status</ReturnType>
19+
<Implementation><![CDATA[
20+
#dim tSC As %Status = $$$OK
21+
#dim e As %Exception.AbstractException
22+
23+
Try {
24+
25+
#dim tJSON As %ZEN.proxyObject
26+
27+
If ..AcceptsContentType("application/json") {
28+
29+
Set %response.ContentType = ..#CONTENTTYPEJSON
30+
31+
#; Convert the exception to a status and render to JSON
32+
Set tSC = ..StatusToProxyObject(pSC, .tJSON)
33+
Set tJSON.stack = ..getDebugInfo()
34+
If $$$ISERR(tSC) Quit
35+
36+
#; Write the JSON to the output device
37+
Set tSC = tJSON.%ToJSON(, "aeloqutwc")
38+
If $$$ISERR(tSC) Quit
39+
40+
} else {
41+
42+
#; Set plain text
43+
Set %response.ContentType = ..#CONTENTTYPETEXT
44+
45+
#; Write out a simple text message
46+
Do ##class(%Exception.StatusException).CreateFromStatus(pSC).OutputToDevice()
47+
}
48+
49+
} Catch (e) {
50+
51+
#; Oops
52+
Set tSC = e.AsStatus()
53+
}
54+
Quit $$$OK
55+
]]></Implementation>
56+
</Method>
57+
58+
<Method name="getDebugInfo">
59+
<ClassMethod>1</ClassMethod>
60+
<ReturnType>%ZEN.proxyObject</ReturnType>
61+
<Implementation><![CDATA[
62+
set obj = ##class(%ZEN.proxyObject).%New()
63+
set obj.stack = ..getStackInfo()
64+
set obj.objlasterror = $system.Status.GetErrorText($get(%objlasterror))
65+
set obj.request = %request
66+
set obj.responce = %response
67+
set obj.session = %session
68+
set obj.user = $username
69+
70+
return obj
71+
]]></Implementation>
72+
</Method>
73+
74+
<Method name="getStackInfo">
75+
<ClassMethod>1</ClassMethod>
76+
<ReturnType>%ListOfDataTypes</ReturnType>
77+
<Implementation><![CDATA[
78+
79+
set ex = ##class(%Exception.SystemException).%New()
80+
do ex.StackAsArray(.stack)
81+
set list = ##class(%ListOfObjects).%New()
82+
for i=1:1:stack {
83+
set obj = ##class(%ZEN.proxyObject).%New()
84+
set obj.line = stack(i,"PLACE")
85+
set obj.call = stack(i)
86+
set obj.part = $piece($piece(stack(i,"PLACE"), "^", *), " ", 1)
87+
do list.Insert(obj)
88+
}
89+
90+
return list
91+
]]></Implementation>
92+
</Method>
93+
94+
<Method name="OnPreDispatch">
95+
<Description>
96+
This method Gets called prior to dispatch of the request. Put any common code here
97+
that you want to be executed for EVERY request. If pContinue is set to 0, the
98+
request will NOT be dispatched according to the UrlMap. If this case it's the
99+
responsibility of the user to return a response.</Description>
100+
<ClassMethod>1</ClassMethod>
101+
<FormalSpec><![CDATA[pUrl:%String,pMethod:%String,&pContinue:%Boolean]]></FormalSpec>
102+
<ReturnType>%Status</ReturnType>
103+
<Implementation><![CDATA[ Quit ..convertRequestBody()
104+
]]></Implementation>
105+
</Method>
106+
107+
<Method name="convertRequestBody">
108+
<Description>
109+
Конвертируем %request.Content в UTF8 и в объект класса %ZEN.proxyObject</Description>
110+
<ClassMethod>1</ClassMethod>
111+
<ReturnType>%Status</ReturnType>
112+
<Implementation><![CDATA[
113+
#dim %request As %CSP.Request
114+
#dim obj As %ZEN.proxyObject
115+
116+
return:'$isObject(%request.Content) $$$OK // нет тела запроса
117+
return:'$$$classIsStream($className(%request.Content)) $$$OK // уже сконвертировали
118+
// Конвертируем объект в UTF8 %ZEN.proxyObject
119+
set content = %request.Content.Read($$$MaxStringLength)
120+
set content = $ZCVT(content,"I","UTF8")
121+
set sс = ##class(Form.JSON.OBJ).%ConvertJSONToObject(content,,.obj,1)
122+
123+
return:$$$ISERR(sс) sс
124+
return:'$isObject(obj) $$$ERROR($$$ArgumentIsNotAnObject,"Body")
125+
126+
set %request.Content = obj // obj это валидный UTF8 %ZEN.proxyObject
127+
return $$$OK
128+
]]></Implementation>
129+
</Method>
6130
</Class>
7131
</Export>

SQLKPI/REST.cls.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,39 @@
33
<Class name="SQLKPI.REST">
44
<Super>SQLKPI.AbstractREST</Super>
55
<TimeCreated>64244,40141.013098</TimeCreated>
6+
7+
<XData name="UrlMap">
8+
<XMLNamespace>http://www.intersystems.com/urlmap</XMLNamespace>
9+
<Data><![CDATA[
10+
<Routes>
11+
<Route Url="/logout" Method="GET" Call="logout"/>
12+
<Route Url="/test" Method="GET" Call="test"/>
13+
</Routes>
14+
]]></Data>
15+
</XData>
16+
17+
<Method name="logout">
18+
<Description>
19+
Logout current session</Description>
20+
<ClassMethod>1</ClassMethod>
21+
<ReturnType>%Status</ReturnType>
22+
<Implementation><![CDATA[
23+
#dim %session As %CSP.Session
24+
set st = %session.Logout(1)
25+
set %session.EndSession = 1
26+
return st
27+
]]></Implementation>
28+
</Method>
29+
30+
<Method name="test">
31+
<Description>
32+
Test</Description>
33+
<ClassMethod>1</ClassMethod>
34+
<ReturnType>%Status</ReturnType>
35+
<Implementation><![CDATA[
36+
write "{""Status"": ""OK""}"
37+
return $$$OK
38+
]]></Implementation>
39+
</Method>
640
</Class>
741
</Export>

0 commit comments

Comments
 (0)