generated from intersystems-community/intersystems-iris-dev-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Spec.cls
128 lines (99 loc) · 3.92 KB
/
Spec.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
Include dc.openapi.common.macro
Class dc.openapi.client.Spec Extends %RegisteredObject
{
Parameter XDataName = "OpenAPI";
XData OpenAPI [ MimeType = application/json ]
{
{ }
}
/// Generate the client classes<br/>
/// Specify your application package in appName argument.<br/>
/// spec is the open api 2.0 specification in JSON Format<br/>
/// It could be a stream, a dynamic object or an URL.<br/>
/// ex: Set sc = ##class(dc.openapi.client.Spec).generateApp("petstore", "https://petstore.swagger.io:443/v2/swagger.json")
ClassMethod generateApp(
appName As %String,
spec,
features As %Binary,
externals As %DynamicObject = "") As %Status
{
Set sc = $$$OK
Set spec = ##class(dc.openapi.common.SpecificationLoader).AbstractSpecLoader(spec, .sc, '$Get(features("noExtRef"),0))
Quit:$$$ISERR(sc) sc
Return ..%CreateClientApplication(appName, spec, .features, externals)
}
ClassMethod %CreateClientApplication(
applicationName As %String,
swagger As %DynamicObject,
ByRef features,
externals As %DynamicObject,
Output newApplication As %Boolean) As %Status
{
Set sc = $$$OK
Try {
Set specClass = applicationName _ ".model.spec"
$$$zlog("* Create a class with the specification in a XData block " _ specClass)
Set cDef=##class(%Dictionary.ClassDefinition).%OpenId(specClass,4,.sc)
If $Isobject(cDef) {
Do ..%RemoveXDataBlock(cDef,..#XDataName)
Set newApplication = $$$NO
} Else {
Set cDef=##class(%Dictionary.ClassDefinition).%New(specClass)
Set newApplication = $$$YES
}
; Security to avoid overwrite an existing package on the server ...
If newApplication && ..packageExists(applicationName) {
Set sc = $$$ERROR(5001, "Overwritting code protection. This package already exist on the server, please choose an other appName "_applicationName)
$$$ThrowStatus(sc)
}
Set cDef.Super = "%RegisteredObject"
Set cDef.ProcedureBlock = $$$YES
Set xData = ##class(%Dictionary.XDataDefinition).%New()
Set xData.Name = ..#XDataName
Set xData.MimeType = "application/json"
; keep the last transformed swagger (debug purpose)
Set ^CacheTemplsc(1) = swagger.%ToJSON()
Kill transformer
$$$ThrowOnError(##class(%JSON.Formatter).%New().FormatToStream(swagger, xData.Data))
$$$ThrowOnError(cDef.XDatas.Insert(xData))
$$$ThrowOnError(cDef.%Save())
$$$ThrowOnError($system.OBJ.Compile(specClass,"ck"))
If $Get(features("modelOnly")) '= 1
{
Do ##class(community.utils.SSLClient).CreateSSLConfiguration("DefaultSSL")
}
// After saving the spec, set externals!
set swagger.externals = externals
If ($Get(features("simpleHttpClientOnly")) = 1) || ($Get(features("modelOnly")) = 1) {
Set generator = ##class(dc.openapi.client.HttpClientGenerator).%New(swagger, applicationName)
Set sc = generator.GenerateClient(.features)
} Else {
Set generator = ##class(dc.openapi.client.ProductionGenerator).%New(swagger, applicationName)
Set sc = generator.Generate(.features)
}
} Catch (ex) {
Set sc = ex.AsStatus()
}
If $$$ISOK(sc), $Get(features("compile"), 1) {
; no error -> compile
Set sc = $SYSTEM.OBJ.CompilePackage(applicationName)
}
Do ##class(dc.openapi.common.Utils).KillTempData()
Return sc
}
ClassMethod %RemoveXDataBlock(
cDef,
blockName)
{
Set key = cDef.XDatas.FindObjectId(cDef.Name_"||"_blockName)
If (key'="") {
Set oid = cDef.XDatas.RemoveAt(key)
}
}
/// Check if a package exists on server ...
ClassMethod packageExists(packageName As %String) As %Boolean
{
Set packageList = ##class(%Dictionary.PackageDefinition).GetPackageList()
Return ''packageList.Find($zcvt(packageName, "U"))
}
}