forked from OpenBankProject/OBP-API
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResourceDocs140.scala
More file actions
250 lines (231 loc) · 11.8 KB
/
ResourceDocs140.scala
File metadata and controls
250 lines (231 loc) · 11.8 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package code.api.ResourceDocs1_4_0
import scala.language.reflectiveCalls
import code.api.Constant.HostName
import code.api.OBPRestHelper
import code.api.cache.Caching
import code.api.util.APIUtil._
import code.api.util.{APIUtil, ApiVersionUtils, YAMLUtils}
import code.api.v1_4_0.JSONFactory1_4_0
import code.apicollectionendpoint.MappedApiCollectionEndpointsProvider
import code.util.Helper.{MdcLoggable, SILENCE_IS_GOLDEN}
import com.openbankproject.commons.model.enums.ContentParam.{DYNAMIC, STATIC}
import com.openbankproject.commons.util.{ApiVersion, ApiVersionStatus}
import net.liftweb.http.{GetRequest, InMemoryResponse, PlainTextResponse, Req, S}
object ResourceDocs140 extends OBPRestHelper with ResourceDocsAPIMethods with MdcLoggable {
val version = ApiVersion.v1_4_0 // "1.4.0" // We match other api versions so API explorer can easily use the path.
val versionStatus = ApiVersionStatus.STABLE.toString
val routes: Seq[OBPEndpoint] = List(
ImplementationsResourceDocs.getResourceDocsObp,
ImplementationsResourceDocs.getBankLevelDynamicResourceDocsObp,
ImplementationsResourceDocs.getResourceDocsSwagger,
)
routes.foreach(route => {
oauthServe(apiPrefix{route})
})
}
// Hack to provide Resource Docs / Swagger on endpoints other than 1.4.0 where it is defined.
object ResourceDocs200 extends OBPRestHelper with ResourceDocsAPIMethods with MdcLoggable {
val version = ApiVersion.v2_0_0 // "2.0.0" // We match other api versions so API explorer can easily use the path.
val versionStatus = ApiVersionStatus.STABLE.toString
val routes: Seq[OBPEndpoint] = List(
ImplementationsResourceDocs.getResourceDocsObp,
ImplementationsResourceDocs.getResourceDocsSwagger,
ImplementationsResourceDocs.getBankLevelDynamicResourceDocsObp,
)
routes.foreach(route => {
oauthServe(apiPrefix{route})
})
}
// Hack to provide Resource Docs / Swagger on endpoints other than 1.4.0 where it is defined.
object ResourceDocs210 extends OBPRestHelper with ResourceDocsAPIMethods with MdcLoggable {
val version: ApiVersion = ApiVersion.v2_1_0 // "2.1.0" // We match other api versions so API explorer can easily use the path.
val versionStatus = ApiVersionStatus.STABLE.toString
val routes: Seq[OBPEndpoint] = List(
ImplementationsResourceDocs.getResourceDocsObp,
ImplementationsResourceDocs.getResourceDocsSwagger,
ImplementationsResourceDocs.getBankLevelDynamicResourceDocsObp,
)
routes.foreach(route => {
oauthServe(apiPrefix{route})
})
}
// Hack to provide Resource Docs / Swagger on endpoints other than 1.4.0 where it is defined.
object ResourceDocs220 extends OBPRestHelper with ResourceDocsAPIMethods with MdcLoggable {
val version: ApiVersion = ApiVersion.v2_2_0 // "2.2.0" // We match other api versions so API explorer can easily use the path.
val versionStatus = ApiVersionStatus.STABLE.toString
val routes: Seq[OBPEndpoint] = List(
ImplementationsResourceDocs.getResourceDocsObp,
ImplementationsResourceDocs.getResourceDocsSwagger,
ImplementationsResourceDocs.getBankLevelDynamicResourceDocsObp,
)
routes.foreach(route => {
oauthServe(apiPrefix{route})
})
}
// Hack to provide Resource Docs / Swagger on endpoints other than 1.4.0 where it is defined.
object ResourceDocs300 extends OBPRestHelper with ResourceDocsAPIMethods with MdcLoggable {
val version : ApiVersion = ApiVersion.v3_0_0 // = "3.0.0" // We match other api versions so API explorer can easily use the path.
val versionStatus = ApiVersionStatus.STABLE.toString
val routes: Seq[OBPEndpoint] = List(
ImplementationsResourceDocs.getResourceDocsObp,
ImplementationsResourceDocs.getResourceDocsSwagger,
ImplementationsResourceDocs.getBankLevelDynamicResourceDocsObp,
)
routes.foreach(route => {
oauthServe(apiPrefix{route})
})
// Hack to provide Resource Docs / Swagger on endpoints other than 1.4.0 where it is defined.
object ResourceDocs310 extends OBPRestHelper with ResourceDocsAPIMethods with MdcLoggable {
val version: ApiVersion = ApiVersion.v3_1_0 // = "3.0.0" // We match other api versions so API explorer can easily use the path.
val versionStatus = ApiVersionStatus.STABLE.toString
val routes: Seq[OBPEndpoint] = List(
ImplementationsResourceDocs.getResourceDocsObp,
ImplementationsResourceDocs.getResourceDocsSwagger,
ImplementationsResourceDocs.getBankLevelDynamicResourceDocsObp,
)
routes.foreach(route => {
oauthServe(apiPrefix {
route
})
})
}
// Hack to provide Resource Docs / Swagger on endpoints other than 1.4.0 where it is defined.
object ResourceDocs400 extends OBPRestHelper with ResourceDocsAPIMethods with MdcLoggable {
val version: ApiVersion = ApiVersion.v4_0_0 // = "4.0.0" // We match other api versions so API explorer can easily use the path.
val versionStatus = ApiVersionStatus.STABLE.toString
val routes: Seq[OBPEndpoint] = List(
ImplementationsResourceDocs.getResourceDocsObpV400,
ImplementationsResourceDocs.getResourceDocsSwagger,
ImplementationsResourceDocs.getBankLevelDynamicResourceDocsObp,
)
routes.foreach(route => {
oauthServe(apiPrefix {
route
})
})
}
// Hack to provide Resource Docs / Swagger on endpoints other than 1.4.0 where it is defined.
object ResourceDocs500 extends OBPRestHelper with ResourceDocsAPIMethods with MdcLoggable {
val version: ApiVersion = ApiVersion.v5_0_0
val versionStatus = ApiVersionStatus.STABLE.toString
val routes: Seq[OBPEndpoint] = List(
ImplementationsResourceDocs.getResourceDocsObpV400,
ImplementationsResourceDocs.getResourceDocsSwagger,
ImplementationsResourceDocs.getBankLevelDynamicResourceDocsObp,
)
routes.foreach(route => {
oauthServe(apiPrefix {
route
})
})
}
object ResourceDocs510 extends OBPRestHelper with ResourceDocsAPIMethods with MdcLoggable {
val version: ApiVersion = ApiVersion.v5_1_0
val versionStatus = ApiVersionStatus.BLEEDING_EDGE.toString
val routes: Seq[OBPEndpoint] = List(
ImplementationsResourceDocs.getResourceDocsObpV400,
ImplementationsResourceDocs.getResourceDocsSwagger,
ImplementationsResourceDocs.getBankLevelDynamicResourceDocsObp,
// ImplementationsResourceDocs.getStaticResourceDocsObp
)
routes.foreach(route => {
oauthServe(apiPrefix {
route
})
})
}
object ResourceDocs600 extends OBPRestHelper with ResourceDocsAPIMethods with MdcLoggable {
val version: ApiVersion = ApiVersion.v6_0_0
val versionStatus = ApiVersionStatus.BLEEDING_EDGE.toString
val routes: Seq[OBPEndpoint] = List(
ImplementationsResourceDocs.getResourceDocsObpV400,
ImplementationsResourceDocs.getResourceDocsSwagger,
ImplementationsResourceDocs.getResourceDocsOpenAPI31,
ImplementationsResourceDocs.getBankLevelDynamicResourceDocsObp,
// ImplementationsResourceDocs.getStaticResourceDocsObp
)
routes.foreach(route => {
oauthServe(apiPrefix {
route
})
})
// Register YAML endpoint using standard RestHelper approach
serve {
case Req("obp" :: versionStr :: "resource-docs" :: requestedApiVersionString :: "openapi.yaml" :: Nil, _, GetRequest) if versionStr == version.toString =>
val (resourceDocTags, partialFunctions, locale, contentParam, apiCollectionIdParam) = ResourceDocsAPIMethodsUtil.getParams()
// Validate parameters
if (S.param("tags").exists(_.trim.isEmpty)) {
PlainTextResponse("Invalid tags parameter - empty values not allowed", 400)
} else if (S.param("functions").exists(_.trim.isEmpty)) {
PlainTextResponse("Invalid functions parameter - empty values not allowed", 400)
} else if (S.param("api-collection-id").exists(_.trim.isEmpty)) {
PlainTextResponse("Invalid api-collection-id parameter - empty values not allowed", 400)
} else if (S.param("content").isDefined && contentParam.isEmpty) {
PlainTextResponse("Invalid content parameter. Valid values: static, dynamic, all", 400)
} else {
try {
val requestedApiVersion = ApiVersionUtils.valueOf(requestedApiVersionString)
if (!versionIsAllowed(requestedApiVersion)) {
PlainTextResponse(s"API Version not supported: $requestedApiVersionString", 400)
} else if (locale.isDefined && APIUtil.obpLocaleValidation(locale.get) != SILENCE_IS_GOLDEN) {
PlainTextResponse(s"Invalid locale: ${locale.get}", 400)
} else {
val isVersion4OrHigher = true
val cacheKey = APIUtil.createResourceDocCacheKey(
Some("openapi31yaml"),
requestedApiVersionString,
resourceDocTags,
partialFunctions,
locale,
contentParam,
apiCollectionIdParam,
Some(isVersion4OrHigher)
)
val cacheValueFromRedis = Caching.getStaticSwaggerDocCache(cacheKey)
val yamlString = if (cacheValueFromRedis.isDefined) {
cacheValueFromRedis.get
} else {
// Generate OpenAPI JSON and convert to YAML
val openApiJValue = try {
val resourceDocsJsonFiltered = locale match {
case _ if (apiCollectionIdParam.isDefined) =>
val operationIds = MappedApiCollectionEndpointsProvider.getApiCollectionEndpoints(apiCollectionIdParam.getOrElse("")).map(_.operationId).map(getObpFormatOperationId)
val resourceDocs = ResourceDoc.getResourceDocs(operationIds)
val resourceDocsJson = JSONFactory1_4_0.createResourceDocsJson(resourceDocs, isVersion4OrHigher, locale)
resourceDocsJson.resource_docs
case _ =>
contentParam match {
case Some(DYNAMIC) =>
ImplementationsResourceDocs.getResourceDocsObpDynamicCached(resourceDocTags, partialFunctions, locale, None, isVersion4OrHigher).head.resource_docs
case Some(STATIC) => {
ImplementationsResourceDocs.getStaticResourceDocsObpCached(requestedApiVersionString, resourceDocTags, partialFunctions, locale, isVersion4OrHigher).head.resource_docs
}
case _ => {
ImplementationsResourceDocs.getAllResourceDocsObpCached(requestedApiVersionString, resourceDocTags, partialFunctions, locale, contentParam, isVersion4OrHigher).head.resource_docs
}
}
}
val hostname = HostName
val openApiDoc = code.api.ResourceDocs1_4_0.OpenAPI31JSONFactory.createOpenAPI31Json(resourceDocsJsonFiltered, requestedApiVersionString, hostname)
code.api.ResourceDocs1_4_0.OpenAPI31JSONFactory.OpenAPI31JsonFormats.toJValue(openApiDoc)
} catch {
case e: Exception =>
logger.error(s"Error generating OpenAPI JSON: ${e.getMessage}", e)
throw e
}
val yamlResult = YAMLUtils.jValueToYAMLSafe(openApiJValue, s"# Error converting OpenAPI to YAML: ${openApiJValue.toString}")
Caching.setStaticSwaggerDocCache(cacheKey, yamlResult)
yamlResult
}
val headers = List("Content-Type" -> YAMLUtils.getYAMLContentType)
val bytes = yamlString.getBytes("UTF-8")
InMemoryResponse(bytes, headers, Nil, 200)
}
} catch {
case _: Exception =>
PlainTextResponse(s"Invalid API version: $requestedApiVersionString", 400)
}
}
}
}
}