1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Base classes used by `nitweb`.
19 import popcorn
::pop_config
20 import popcorn
::pop_repos
21 import popcorn
::pop_json
23 import commands
::commands_http
24 import commands
::commands_json
25 import commands
::commands_html
31 redef fun default_db_name
do return "nitweb"
36 # MModule used to flatten model.
37 var mainmodule
: MModule
39 # Modelbuilder used to access sources.
40 var modelbuilder
: ModelBuilder
42 # The JSON API does not filter anything by default.
43 var filter
: nullable ModelFilter
45 # Catalog to pass to handlers.
49 # Specific handler for the nitweb API.
50 abstract class APIHandler
54 var config
: NitwebConfig
56 # Find the MEntity ` with `full_name`.
57 fun find_mentity
(full_name
: nullable String): nullable MEntity do
58 if full_name
== null then return null
59 var mentity
= config
.model
.mentity_by_full_name
(full_name
.from_percent_encoding
, config
.filter
)
60 if mentity
== null then return null
62 var filter
= config
.filter
63 if filter
== null or filter
.accept_mentity
(mentity
) then return mentity
67 # Try to load the mentity from uri with `/:id`.
69 # Send 400 if `:id` is null.
70 # Send 404 if no entity is found.
71 # Return null in both cases.
72 fun mentity_from_uri
(req
: HttpRequest, res
: HttpResponse): nullable MEntity do
73 var id
= req
.param
("id")
75 res
.api_error
(400, "Expected mentity full name")
78 var mentity
= find_mentity
(id
)
79 if mentity
== null then
80 res
.api_error
(404, "MEntity `{id}` not found")
85 # Paginate a json array
87 # Returns only a subset of `results` depending on the current `page` and the
88 # number of elements to return set by `limit`.
90 # Transforms the json array into an object:
100 fun paginate
(results
: JsonArray, count
: Int, page
, limit
: nullable Int): JsonObject do
101 if page
== null or page
<= 0 then page
= 1
102 if limit
== null or limit
<= 0 then limit
= 20
104 var max
= count
/ limit
108 else if page
> max
then
112 var lstart
= (page
- 1) * limit
114 if lstart
+ lend
> count
then lend
= count
- lstart
116 var res
= new JsonObject
119 res
["results"] = new JsonArray.from
(results
.subarray
(lstart
, lend
))
126 # A Rooter dedicated to APIHandlers.
131 var config
: NitwebConfig
134 redef class HttpResponse
136 # Return an HTTP error response with `status`
138 # Like the rest of the API, errors are formated as JSON:
140 # { "status": 404, "message": "Not found" }
142 fun api_error
(status
: Int, message
: String) do
143 json
(new APIError(status
, message
), status
)
146 # Return `serializable` as a json string
148 # Uses `req` to define serialization options.
149 fun api_json
(req
: HttpRequest, serializable
: nullable Serializable, status
: nullable Int, plain
, pretty
: nullable Bool) do
150 json
(serializable
, status
, plain
, req
.bool_arg
("pretty"))
154 # An error returned by the API.
156 # Can be serialized to json.
163 # Response error message
169 # URL to `self` within the web interface.
170 fun web_url
: String do return "/doc/" / full_name
172 # URL to `self` within the JSON api.
173 fun api_url
: String do return "/api/entity/" / full_name
175 redef fun html_url
do return web_url
177 redef fun core_serialize_to
(v
) do
179 v
.serialize_attribute
("web_url", web_url
)
180 v
.serialize_attribute
("api_url", api_url
)
184 redef class MEntityRef
185 redef fun core_serialize_to
(v
) do
187 v
.serialize_attribute
("web_url", mentity
.web_url
)
188 v
.serialize_attribute
("api_url", mentity
.api_url
)
192 redef class MClassDef
193 redef fun web_url
do return "{mclass.web_url}/lin#{full_name}"
197 redef fun web_url
do return "{mproperty.web_url}/lin#{full_name}"
201 redef fun core_serialize_to
(v
) do
203 v
.serialize_attribute
("web_url", web_url
)
204 v
.serialize_attribute
("api_url", api_url
)
208 redef class MClassType
209 redef var web_url
= mclass
.web_url
is lazy
210 redef var api_url
= mclass
.api_url
is lazy
213 redef class MNullableType
214 redef var web_url
= mtype
.web_url
is lazy
215 redef var api_url
= mtype
.api_url
is lazy
218 redef class MParameterType
219 redef var web_url
= mclass
.web_url
is lazy
220 redef var api_url
= mclass
.api_url
is lazy
223 redef class MVirtualType
224 redef var web_url
= mproperty
.web_url
is lazy
225 redef var api_url
= mproperty
.api_url
is lazy
228 redef class CmdLicenseFile
229 redef var file_url
is lazy
do
230 var mentity
= self.mentity
231 if mentity
== null then return super
232 return "{mentity.web_url}/license"
236 redef class CmdContribFile
237 redef var file_url
is lazy
do
238 var mentity
= self.mentity
239 if mentity
== null then return super
240 return "{mentity.web_url}/contrib"