forked from rexyai/RestRserve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContentHandlersFactory.R
159 lines (154 loc) · 5.36 KB
/
ContentHandlersFactory.R
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
#' @title Content handlers collection
#
#' @description
#' Controls how RestRserve encodes and decodes different content types.
#' Designed to work jointly with [EncodeDecodeMiddleware]
#
#' @seealso [Application] [EncodeDecodeMiddleware]
#
#' @name ContentHandlers
#' @keywords internal
#'
ContentHandlersFactory = R6::R6Class(
classname = "ContentHandlers",
public = list(
#' @field handlers Handlers storage environment.
handlers = NULL,
#' @description
#' Creates ContentHandlersFactory object.
initialize = function() {
self$reset()
},
#' @description
#' Set handler to encode body for the specific content type.
#' @param content_type MIME type.
#' @param FUN Function to encode response body.
set_encode = function(content_type, FUN) {
checkmate::assert_string(content_type, pattern = ".*/.*")
checkmate::assert_function(FUN)
content_type = tolower(content_type)
if (is.null(self$handlers[[content_type]])) {
self$handlers[[content_type]] = list()
}
self$handlers[[content_type]][["encode"]] = FUN
return(invisible(self))
},
#' @description
#' Get encoder function for the specific content type.
#' @param content_type MIME type.
get_encode = function(content_type) {
if (!is_string(content_type)) {
raise(HTTPError$internal_server_error(
body = list(error = "500 Internal Server Error: can't encode the body - invalid 'content_type'"))
)
}
content_type = tolower(content_type)
encode = self$handlers[[content_type]][["encode"]]
if (!is.function(encode)) {
# case when charset is provided (for example 'application/json; charset=utf-8')
content_type = strsplit(content_type, ';', TRUE)[[1]][[1]]
encode = self$handlers[[content_type]][["encode"]]
if (!is.function(encode)) {
err = sprintf("500 Internal Server Error: can't encode body with content_type = '%s'", content_type)
raise(HTTPError$internal_server_error(
body = list(error = err)
))
}
}
return(encode)
},
#' @description
#' Set handler to decode body for the specific content type.
#' @param content_type MIME type.
#' @param FUN Function to decode request body.
set_decode = function(content_type, FUN) {
checkmate::assert_string(content_type, pattern = ".*/.*")
checkmate::assert_function(FUN)
content_type = tolower(content_type)
if (is.null(self$handlers[[content_type]])) {
self$handlers[[content_type]] = list()
}
self$handlers[[content_type]][["decode"]] = FUN
private$supported_decode_types = unique(c(private$supported_decode_types, content_type))
return(invisible(self))
},
#' @description
#' Get decoder function for the specific content type.
#' @param content_type MIME type.
get_decode = function(content_type) {
if (!is_string(content_type)) {
err = "'content-type' header is not set/invalid - don't know how to decode the body"
raise(HTTPError$unsupported_media_type(body = list(error = err)))
}
content_type = tolower(content_type)
# ignore content types (exact match)
if (content_type %in% private$ingore$equal) {
return(identity)
}
# ignore content types (prefix match)
if (any(startsWith(content_type, private$ignore$prefix))) {
return(identity)
}
decode = self$handlers[[content_type]][["decode"]]
if (!is.function(decode)) {
# case when charset is provided (for example 'application/json; charset=utf-8')
content_type = strsplit(content_type, ';', TRUE)[[1]][[1]]
decode = self$handlers[[content_type]][["decode"]]
if (!is.function(decode)) {
err = sprintf("unsupported media type \"%s\"", content_type)
raise(HTTPError$unsupported_media_type(body = list(error = err)))
}
}
return(decode)
},
#' @description
#' Convert handlers to list.
#' @return List of handlers.
list = function() {
return(as.list(self$handlers))
},
#' @description
#' Resets all the content handlers to RestRserve defaults.
reset = function() {
self$handlers = new.env(parent = emptyenv())
private$supported_decode_types = NULL
# set default encoders
self$set_encode("application/json", to_json)
self$set_encode("text/plain", to_string)
self$set_encode("text/html", to_string)
self$set_encode("text/css", to_string)
# set default decoders
self$set_decode(
"application/json",
function(x) {
res = try(from_json(x), silent = TRUE)
if (inherits(res, "try-error")) {
raise(HTTPError$bad_request(body = attributes(res)$condition$message))
}
return(res)
}
)
self$set_decode("text/plain", function(x) {
if (is.raw(x)) {
x = try(rawToChar(x), silent = TRUE)
if (inherits(x, "try-error")) {
raise(HTTPError$bad_request(body = attributes(x)$condition$message))
}
}
return(x)
})
return(invisible(self))
}
),
private = list(
supported_decode_types = NULL,
ignore = list(
equal = c(
"application/x-www-form-urlencoded"
),
prefix = c(
"multipart/form-data"
)
)
)
)