1
1
module Servers
2
2
3
3
using JSON
4
+ using HTTP
4
5
5
- import .. OpenAPI: APIModel, ValidationException, from_json
6
+ import .. OpenAPI: APIModel, ValidationException, from_json, to_json
6
7
7
- # const OpenAPIParams = :openapi_params
8
-
9
- function middleware (read, validate, invoke; init= nothing , pre_validation= nothing , pre_invoke= nothing , post_invoke= nothing )
10
- ret = (init === nothing ) ? read : init |> read
11
- ret = (pre_validation === nothing ) ? ret |> validate : ret |> pre_validation |> validate
12
- ret = (pre_invoke === nothing ) ? ret |> invoke : ret |> pre_invoke |> invoke
13
- if post_invoke != = nothing
14
- ret = ret |> post_invoke
8
+ function middleware (impl, read, validate, invoke;
9
+ init= nothing ,
10
+ pre_validation= nothing ,
11
+ pre_invoke= nothing ,
12
+ post_invoke= nothing
13
+ )
14
+ handler = req -> (invoke (impl; post_invoke= post_invoke))(req)
15
+ if ! isnothing (pre_invoke)
16
+ handler = pre_invoke (handler)
15
17
end
16
-
17
- return ret
18
+ handler = validate (handler)
19
+ if ! isnothing (pre_validation)
20
+ handler = pre_validation (handler)
21
+ end
22
+ handler = read (handler)
23
+ if ! isnothing (init)
24
+ handler = init (handler)
25
+ end
26
+ return handler
18
27
end
19
28
20
29
# #############################
@@ -41,12 +50,21 @@ function to_param_type(::Type{T}, strval::String) where {T <: APIModel}
41
50
from_json (T, JSON. parse (strval))
42
51
end
43
52
53
+ function to_param_type (:: Type{T} , json:: Dict{String,Any} ) where {T <: APIModel }
54
+ from_json (T, json)
55
+ end
56
+
44
57
function to_param_type (:: Type{Vector{T}} , strval:: String , delim:: String ) where {T}
45
- elems = strip .(split (strval, delim))
58
+ elems = string .( strip .(split (strval, delim) ))
46
59
return map (x-> to_param_type (T, x), elems)
47
60
end
48
61
49
- function to_param (T, source:: Dict , name:: String ; required:: Bool = false , collection_format:: Union{String,Nothing} = nothing , multipart:: Bool = false , isfile:: Bool = false )
62
+ function to_param_type (:: Type{Vector{T}} , strval:: String ) where {T}
63
+ elems = JSON. parse (strval)
64
+ return map (x-> to_param_type (T, x), elems)
65
+ end
66
+
67
+ function to_param (T, source:: Dict , name:: String ; required:: Bool = false , collection_format:: Union{String,Nothing} = " ," , multipart:: Bool = false , isfile:: Bool = false )
50
68
param = get_param (source, name, required)
51
69
if param === nothing
52
70
return nothing
@@ -62,4 +80,9 @@ function to_param(T, source::Dict, name::String; required::Bool=false, collectio
62
80
end
63
81
end
64
82
83
+ server_response (resp:: HTTP.Response ) = resp
84
+ server_response (:: Nothing ) = server_response (" " )
85
+ server_response (ret) = server_response (to_json (ret))
86
+ server_response (resp:: String ) = HTTP. Response (200 , resp)
87
+
65
88
end # module Servers
0 commit comments