@@ -2,18 +2,25 @@ package code.api.v7_0_0
22
33import cats .data .{Kleisli , OptionT }
44import cats .effect ._
5- import cats .implicits ._
6- import code .api .util .{APIUtil , CustomJsonFormats }
5+ import code .api .Constant ._
6+ import code .api .ResourceDocs1_4_0 .SwaggerDefinitionsJSON ._
7+ import code .api .ResourceDocs1_4_0 .{ResourceDocs140 , ResourceDocsAPIMethodsUtil }
8+ import code .api .util .APIUtil .{EmptyBody , _ }
9+ import code .api .util .ApiTag ._
10+ import code .api .util .ErrorMessages ._
11+ import code .api .util .{ApiVersionUtils , CustomJsonFormats , NewStyle }
12+ import code .api .v1_4_0 .JSONFactory1_4_0
713import code .api .v4_0_0 .JSONFactory400
8- import code . bankconnectors . Connector
9- import com .openbankproject .commons .util .{ ApiVersion , ScannedApiVersion }
10- import net . liftweb . json . Formats
14+ import com . github . dwickern . macros . NameOf . nameOf
15+ import com .openbankproject .commons .ExecutionContext . Implicits . global
16+ import com . openbankproject . commons . util .{ ApiVersion , ApiVersionStatus , ScannedApiVersion }
1117import net .liftweb .json .JsonAST .prettyRender
12- import net .liftweb .json .Extraction
18+ import net .liftweb .json .{ Extraction , Formats }
1319import org .http4s ._
1420import org .http4s .dsl .io ._
1521import org .typelevel .vault .Key
1622
23+ import scala .collection .mutable .ArrayBuffer
1724import scala .concurrent .Future
1825import scala .language .{higherKinds , implicitConversions }
1926
@@ -24,12 +31,13 @@ object Http4s700 {
2431 implicit val formats : Formats = CustomJsonFormats .formats
2532 implicit def convertAnyToJsonString (any : Any ): String = prettyRender(Extraction .decompose(any))
2633
27- val apiVersion : ScannedApiVersion = ApiVersion .v7_0_0
28- val apiVersionString : String = apiVersion.toString
34+ val implementedInApiVersion : ScannedApiVersion = ApiVersion .v7_0_0
35+ val versionStatus = ApiVersionStatus .STABLE .toString
36+ val resourceDocs = ArrayBuffer [ResourceDoc ]()
2937
3038 case class CallContext (userId : String , requestId : String )
31- import cats . effect . unsafe . implicits . global
32- val callContextKey : Key [ CallContext ] = Key .newKey[IO , CallContext ].unsafeRunSync()
39+ val callContextKey : Key [ CallContext ] =
40+ Key .newKey[IO , CallContext ].unsafeRunSync()(cats.effect.unsafe. IORuntime .global )
3341
3442 object CallContextMiddleware {
3543
@@ -42,31 +50,108 @@ object Http4s700 {
4250 }
4351 }
4452
45- val v700Services : HttpRoutes [IO ] = HttpRoutes .of[IO ] {
46- case req @ GET -> Root / " obp" / `apiVersionString` / " root" =>
47- import com .openbankproject .commons .ExecutionContext .Implicits .global
48- val callContext = req.attributes.lookup(callContextKey).get.asInstanceOf [CallContext ]
49- Ok (IO .fromFuture(IO (
50- for {
51- _ <- Future () // Just start async call
52- } yield {
53- convertAnyToJsonString(
54- JSONFactory700 .getApiInfoJSON(apiVersion, s " Hello, ${callContext.userId}! Your request ID is ${callContext.requestId}. " )
55- )
56- }
57- )))
58-
59- case req @ GET -> Root / " obp" / `apiVersionString` / " banks" =>
60- import com .openbankproject .commons .ExecutionContext .Implicits .global
61- Ok (IO .fromFuture(IO (
62- for {
63- (banks, callContext) <- code.api.util.NewStyle .function.getBanks(None )
64- } yield {
65- convertAnyToJsonString(JSONFactory400 .createBanksJson(banks))
66- }
67- )))
53+ object Implementations7_0_0 {
54+
55+ // Common prefix: /obp/v7.0.0
56+ val prefixPath = Root / ApiPathZero .toString / implementedInApiVersion.toString
57+
58+
59+ resourceDocs += ResourceDoc (
60+ null ,
61+ implementedInApiVersion,
62+ nameOf(root),
63+ " GET" ,
64+ " /root" ,
65+ " Get API Info (root)" ,
66+ s """ Returns information about:
67+ |
68+ |* API version
69+ |* Hosted by information
70+ |* Git Commit
71+ | ${userAuthenticationMessage(false )}""" ,
72+ EmptyBody ,
73+ apiInfoJSON,
74+ List (UnknownError , " no connector set" ),
75+ apiTagApi :: Nil ,
76+ http4sPartialFunction = Some (root)
77+ )
78+
79+ // Route: GET /obp/v7.0.0/root
80+ val root : HttpRoutes [IO ] = HttpRoutes .of[IO ] {
81+ case req @ GET -> `prefixPath` / " root" =>
82+ val callContext = req.attributes.lookup(callContextKey).get.asInstanceOf [CallContext ]
83+ Ok (IO .fromFuture(IO (
84+ for {
85+ _ <- Future () // Just start async call
86+ } yield {
87+ convertAnyToJsonString(
88+ JSONFactory700 .getApiInfoJSON(implementedInApiVersion, s " Hello, ${callContext.userId}! Your request ID is ${callContext.requestId}. " )
89+ )
90+ }
91+ )))
92+ }
93+
94+ resourceDocs += ResourceDoc (
95+ null ,
96+ implementedInApiVersion,
97+ nameOf(getBanks),
98+ " GET" ,
99+ " /banks" ,
100+ " Get Banks" ,
101+ s """ Get banks on this API instance
102+ |Returns a list of banks supported on this server:
103+ |
104+ |* ID used as parameter in URLs
105+ |* Short and full name of bank
106+ |* Logo URL
107+ |* Website
108+ | ${userAuthenticationMessage(false )}""" ,
109+ EmptyBody ,
110+ banksJSON,
111+ List (UnknownError ),
112+ apiTagBank :: Nil ,
113+ http4sPartialFunction = Some (getBanks)
114+ )
115+
116+ // Route: GET /obp/v7.0.0/banks
117+ val getBanks : HttpRoutes [IO ] = HttpRoutes .of[IO ] {
118+ case req @ GET -> `prefixPath` / " banks" =>
119+ import com .openbankproject .commons .ExecutionContext .Implicits .global
120+ Ok (IO .fromFuture(IO (
121+ for {
122+ (banks, callContext) <- NewStyle .function.getBanks(None )
123+ } yield {
124+ convertAnyToJsonString(JSONFactory400 .createBanksJson(banks))
125+ }
126+ )))
127+ }
128+
129+ val getResourceDocsObpV700 : HttpRoutes [IO ] = HttpRoutes .of[IO ] {
130+ case req @ GET -> `prefixPath` / " resource-docs" / requestedApiVersionString / " obp" =>
131+ import com .openbankproject .commons .ExecutionContext .Implicits .global
132+ val logic = for {
133+ httpParams <- NewStyle .function.extractHttpParamsFromUrl(req.uri.renderString)
134+ tagsParam = httpParams.filter(_.name == " tags" ).map(_.values).headOption
135+ functionsParam = httpParams.filter(_.name == " functions" ).map(_.values).headOption
136+ localeParam = httpParams.filter(param => param.name == " locale" || param.name == " language" ).map(_.values).flatten.headOption
137+ contentParam = httpParams.filter(_.name == " content" ).map(_.values).flatten.flatMap(ResourceDocsAPIMethodsUtil .stringToContentParam).headOption
138+ apiCollectionIdParam = httpParams.filter(_.name == " api-collection-id" ).map(_.values).flatten.headOption
139+ tags = tagsParam.map(_.map(ResourceDocTag (_)))
140+ functions = functionsParam.map(_.toList)
141+ requestedApiVersion <- Future (ApiVersionUtils .valueOf(requestedApiVersionString))
142+ resourceDocs = ResourceDocs140 .ImplementationsResourceDocs .getResourceDocsList(requestedApiVersion).getOrElse(Nil )
143+ filteredDocs = ResourceDocsAPIMethodsUtil .filterResourceDocs(resourceDocs, tags, functions)
144+ resourceDocsJson = JSONFactory1_4_0 .createResourceDocsJson(filteredDocs, isVersion4OrHigher = true , localeParam)
145+ } yield convertAnyToJsonString(resourceDocsJson)
146+ Ok (IO .fromFuture(IO (logic)))
147+ }
148+
149+ // All routes combined
150+ val allRoutes : HttpRoutes [IO ] =
151+ Kleisli [HttpF , Request [IO ], Response [IO ]] { req : Request [IO ] =>
152+ root(req).orElse(getBanks(req)).orElse(getResourceDocsObpV700(req))
153+ }
68154 }
69155
70- val wrappedRoutesV700Services : HttpRoutes [IO ] = CallContextMiddleware .withCallContext(v700Services )
156+ val wrappedRoutesV700Services : HttpRoutes [IO ] = CallContextMiddleware .withCallContext(Implementations7_0_0 .allRoutes )
71157}
72-
0 commit comments