grape 0.5.0 → 3.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +1223 -164
- data/CONTRIBUTING.md +148 -0
- data/LICENSE +1 -1
- data/README.md +3531 -675
- data/UPGRADING.md +1851 -0
- data/grape.gemspec +26 -32
- data/grape.png +0 -0
- data/lib/grape/api/instance.rb +194 -0
- data/lib/grape/api.rb +110 -485
- data/lib/grape/content_types.rb +28 -0
- data/lib/grape/cookies.rb +32 -25
- data/lib/grape/declared_params_handler.rb +118 -0
- data/lib/grape/dry_types.rb +54 -0
- data/lib/grape/dsl/callbacks.rb +19 -0
- data/lib/grape/dsl/declared.rb +35 -0
- data/lib/grape/dsl/desc.rb +65 -0
- data/lib/grape/dsl/headers.rb +21 -0
- data/lib/grape/dsl/helpers.rb +103 -0
- data/lib/grape/dsl/inside_route.rb +293 -0
- data/lib/grape/dsl/logger.rb +19 -0
- data/lib/grape/dsl/middleware.rb +36 -0
- data/lib/grape/dsl/parameters.rb +239 -0
- data/lib/grape/dsl/request_response.rb +168 -0
- data/lib/grape/dsl/routing.rb +288 -0
- data/lib/grape/dsl/settings.rb +67 -0
- data/lib/grape/dsl/validations.rb +51 -0
- data/lib/grape/endpoint.rb +287 -386
- data/lib/grape/env.rb +20 -0
- data/lib/grape/error_formatter/base.rb +56 -19
- data/lib/grape/error_formatter/json.rb +18 -7
- data/lib/grape/error_formatter/serializable_hash.rb +7 -0
- data/lib/grape/error_formatter/txt.rb +14 -11
- data/lib/grape/error_formatter/xml.rb +5 -11
- data/lib/grape/error_formatter.rb +15 -0
- data/lib/grape/exceptions/base.rb +31 -47
- data/lib/grape/exceptions/incompatible_option_values.rb +11 -0
- data/lib/grape/exceptions/invalid_accept_header.rb +11 -0
- data/lib/grape/exceptions/invalid_formatter.rb +3 -5
- data/lib/grape/exceptions/invalid_message_body.rb +11 -0
- data/lib/grape/exceptions/invalid_response.rb +11 -0
- data/lib/grape/exceptions/invalid_version_header.rb +11 -0
- data/lib/grape/exceptions/invalid_versioner_option.rb +3 -6
- data/lib/grape/exceptions/invalid_with_option_for_represent.rb +3 -7
- data/lib/grape/exceptions/method_not_allowed.rb +11 -0
- data/lib/grape/exceptions/missing_group_type.rb +11 -0
- data/lib/grape/exceptions/missing_mime_type.rb +3 -6
- data/lib/grape/exceptions/missing_vendor_option.rb +3 -5
- data/lib/grape/exceptions/request_error.rb +11 -0
- data/lib/grape/exceptions/unknown_auth_strategy.rb +11 -0
- data/lib/grape/exceptions/unknown_parameter.rb +11 -0
- data/lib/grape/exceptions/unknown_params_builder.rb +11 -0
- data/lib/grape/exceptions/unknown_validator.rb +3 -4
- data/lib/grape/exceptions/unsupported_group_type.rb +11 -0
- data/lib/grape/exceptions/validation.rb +20 -8
- data/lib/grape/exceptions/validation_array_errors.rb +14 -0
- data/lib/grape/exceptions/validation_errors.rb +59 -0
- data/lib/grape/formatter/base.rb +9 -26
- data/lib/grape/formatter/json.rb +6 -7
- data/lib/grape/formatter/serializable_hash.rb +24 -20
- data/lib/grape/formatter/txt.rb +5 -7
- data/lib/grape/formatter/xml.rb +6 -7
- data/lib/grape/formatter.rb +17 -0
- data/lib/grape/json.rb +10 -0
- data/lib/grape/locale/en.yml +47 -20
- data/lib/grape/middleware/auth/base.rb +25 -0
- data/lib/grape/middleware/auth/dsl.rb +38 -0
- data/lib/grape/middleware/auth/strategies.rb +25 -0
- data/lib/grape/middleware/auth/strategy_info.rb +15 -0
- data/lib/grape/middleware/base.rb +73 -19
- data/lib/grape/middleware/error.rb +104 -44
- data/lib/grape/middleware/filter.rb +5 -3
- data/lib/grape/middleware/formatter.rb +115 -105
- data/lib/grape/middleware/globals.rb +14 -0
- data/lib/grape/middleware/stack.rb +110 -0
- data/lib/grape/middleware/versioner/accept_version_header.rb +11 -40
- data/lib/grape/middleware/versioner/base.rb +76 -0
- data/lib/grape/middleware/versioner/header.rb +61 -78
- data/lib/grape/middleware/versioner/param.rb +9 -21
- data/lib/grape/middleware/versioner/path.rb +16 -31
- data/lib/grape/middleware/versioner.rb +10 -15
- data/lib/grape/namespace.rb +39 -10
- data/lib/grape/params_builder/base.rb +20 -0
- data/lib/grape/params_builder/hash.rb +11 -0
- data/lib/grape/params_builder/hash_with_indifferent_access.rb +11 -0
- data/lib/grape/params_builder/hashie_mash.rb +11 -0
- data/lib/grape/params_builder.rb +15 -0
- data/lib/grape/parser/base.rb +9 -24
- data/lib/grape/parser/json.rb +8 -7
- data/lib/grape/parser/xml.rb +8 -7
- data/lib/grape/parser.rb +15 -0
- data/lib/grape/path.rb +76 -0
- data/lib/grape/presenters/presenter.rb +11 -0
- data/lib/grape/railtie.rb +9 -0
- data/lib/grape/request.rb +182 -0
- data/lib/grape/router/base_route.rb +48 -0
- data/lib/grape/router/greedy_route.rb +26 -0
- data/lib/grape/router/pattern.rb +67 -0
- data/lib/grape/router/route.rb +58 -0
- data/lib/grape/router.rb +167 -0
- data/lib/grape/serve_stream/file_body.rb +36 -0
- data/lib/grape/serve_stream/sendfile_response.rb +21 -0
- data/lib/grape/serve_stream/stream_response.rb +23 -0
- data/lib/grape/util/api_description.rb +58 -0
- data/lib/grape/util/base_inheritable.rb +46 -0
- data/lib/grape/util/cache.rb +18 -0
- data/lib/grape/util/deep_freeze.rb +35 -0
- data/lib/grape/util/endpoint_configuration.rb +8 -0
- data/lib/grape/util/header.rb +13 -0
- data/lib/grape/util/inheritable_setting.rb +107 -0
- data/lib/grape/util/inheritable_values.rb +29 -0
- data/lib/grape/util/lazy/block.rb +29 -0
- data/lib/grape/util/lazy/value.rb +38 -0
- data/lib/grape/util/lazy/value_array.rb +21 -0
- data/lib/grape/util/lazy/value_enumerable.rb +34 -0
- data/lib/grape/util/lazy/value_hash.rb +21 -0
- data/lib/grape/util/media_type.rb +70 -0
- data/lib/grape/util/registry.rb +27 -0
- data/lib/grape/util/reverse_stackable_values.rb +15 -0
- data/lib/grape/util/stackable_values.rb +36 -0
- data/lib/grape/util/translation.rb +42 -0
- data/lib/grape/validations/attributes_iterator.rb +77 -0
- data/lib/grape/validations/contract_scope.rb +28 -0
- data/lib/grape/validations/multiple_attributes_iterator.rb +13 -0
- data/lib/grape/validations/param_scope_tracker.rb +57 -0
- data/lib/grape/validations/params_documentation.rb +50 -0
- data/lib/grape/validations/params_scope.rb +529 -0
- data/lib/grape/validations/single_attribute_iterator.rb +26 -0
- data/lib/grape/validations/types/array_coercer.rb +60 -0
- data/lib/grape/validations/types/custom_type_coercer.rb +164 -0
- data/lib/grape/validations/types/custom_type_collection_coercer.rb +56 -0
- data/lib/grape/validations/types/dry_type_coercer.rb +59 -0
- data/lib/grape/validations/types/file.rb +31 -0
- data/lib/grape/validations/types/invalid_value.rb +17 -0
- data/lib/grape/validations/types/json.rb +71 -0
- data/lib/grape/validations/types/multiple_type_coercer.rb +57 -0
- data/lib/grape/validations/types/primitive_coercer.rb +46 -0
- data/lib/grape/validations/types/set_coercer.rb +35 -0
- data/lib/grape/validations/types/variant_collection_coercer.rb +51 -0
- data/lib/grape/validations/types.rb +198 -0
- data/lib/grape/validations/validators/all_or_none_of_validator.rb +19 -0
- data/lib/grape/validations/validators/allow_blank_validator.rb +25 -0
- data/lib/grape/validations/validators/as_validator.rb +14 -0
- data/lib/grape/validations/validators/at_least_one_of_validator.rb +18 -0
- data/lib/grape/validations/validators/base.rb +158 -0
- data/lib/grape/validations/validators/coerce_validator.rb +55 -0
- data/lib/grape/validations/validators/contract_scope_validator.rb +45 -0
- data/lib/grape/validations/validators/default_validator.rb +31 -0
- data/lib/grape/validations/validators/exactly_one_of_validator.rb +24 -0
- data/lib/grape/validations/validators/except_values_validator.rb +33 -0
- data/lib/grape/validations/validators/length_validator.rb +48 -0
- data/lib/grape/validations/validators/multiple_params_base.rb +34 -0
- data/lib/grape/validations/validators/mutually_exclusive_validator.rb +18 -0
- data/lib/grape/validations/validators/presence_validator.rb +17 -0
- data/lib/grape/validations/validators/regexp_validator.rb +24 -0
- data/lib/grape/validations/validators/same_as_validator.rb +20 -0
- data/lib/grape/validations/validators/values_validator.rb +68 -0
- data/lib/grape/validations.rb +10 -226
- data/lib/grape/version.rb +4 -1
- data/lib/grape/xml.rb +10 -0
- data/lib/grape.rb +79 -77
- metadata +160 -306
- data/.gitignore +0 -38
- data/.rspec +0 -2
- data/.travis.yml +0 -9
- data/.yardopts +0 -2
- data/Gemfile +0 -19
- data/Guardfile +0 -15
- data/Rakefile +0 -64
- data/lib/grape/exceptions/missing_option.rb +0 -13
- data/lib/grape/exceptions/unknown_options.rb +0 -14
- data/lib/grape/http/request.rb +0 -28
- data/lib/grape/middleware/auth/basic.rb +0 -30
- data/lib/grape/middleware/auth/digest.rb +0 -30
- data/lib/grape/middleware/auth/oauth2.rb +0 -72
- data/lib/grape/route.rb +0 -23
- data/lib/grape/util/content_types.rb +0 -17
- data/lib/grape/util/deep_merge.rb +0 -23
- data/lib/grape/util/hash_stack.rb +0 -120
- data/lib/grape/validations/coerce.rb +0 -62
- data/lib/grape/validations/default.rb +0 -24
- data/lib/grape/validations/presence.rb +0 -12
- data/lib/grape/validations/regexp.rb +0 -14
- data/spec/grape/api_spec.rb +0 -2129
- data/spec/grape/endpoint_spec.rb +0 -605
- data/spec/grape/entity_spec.rb +0 -281
- data/spec/grape/exceptions/invalid_formatter_spec.rb +0 -18
- data/spec/grape/exceptions/invalid_versioner_option_spec.rb +0 -18
- data/spec/grape/exceptions/missing_mime_type_spec.rb +0 -24
- data/spec/grape/exceptions/missing_option_spec.rb +0 -18
- data/spec/grape/exceptions/unknown_options_spec.rb +0 -18
- data/spec/grape/exceptions/unknown_validator_spec.rb +0 -18
- data/spec/grape/middleware/auth/basic_spec.rb +0 -31
- data/spec/grape/middleware/auth/digest_spec.rb +0 -47
- data/spec/grape/middleware/auth/oauth2_spec.rb +0 -90
- data/spec/grape/middleware/base_spec.rb +0 -63
- data/spec/grape/middleware/error_spec.rb +0 -49
- data/spec/grape/middleware/exception_spec.rb +0 -164
- data/spec/grape/middleware/formatter_spec.rb +0 -244
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +0 -121
- data/spec/grape/middleware/versioner/header_spec.rb +0 -277
- data/spec/grape/middleware/versioner/param_spec.rb +0 -58
- data/spec/grape/middleware/versioner/path_spec.rb +0 -44
- data/spec/grape/middleware/versioner_spec.rb +0 -22
- data/spec/grape/util/hash_stack_spec.rb +0 -133
- data/spec/grape/validations/coerce_spec.rb +0 -143
- data/spec/grape/validations/default_spec.rb +0 -67
- data/spec/grape/validations/presence_spec.rb +0 -144
- data/spec/grape/validations/regexp_spec.rb +0 -33
- data/spec/grape/validations/zh-CN.yml +0 -9
- data/spec/grape/validations_spec.rb +0 -217
- data/spec/shared/versioning_examples.rb +0 -121
- data/spec/spec_helper.rb +0 -25
- data/spec/support/basic_auth_encode_helpers.rb +0 -4
- data/spec/support/versioned_helpers.rb +0 -48
|
@@ -1,70 +1,130 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Grape
|
|
4
4
|
module Middleware
|
|
5
5
|
class Error < Base
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
:
|
|
15
|
-
:
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
end
|
|
6
|
+
DEFAULT_OPTIONS = {
|
|
7
|
+
default_status: 500,
|
|
8
|
+
default_message: '',
|
|
9
|
+
format: :txt,
|
|
10
|
+
rescue_all: false,
|
|
11
|
+
rescue_grape_exceptions: false,
|
|
12
|
+
rescue_subclasses: true,
|
|
13
|
+
rescue_options: {
|
|
14
|
+
backtrace: false,
|
|
15
|
+
original_exception: false
|
|
16
|
+
}.freeze
|
|
17
|
+
}.freeze
|
|
20
18
|
|
|
21
19
|
def call!(env)
|
|
22
20
|
@env = env
|
|
21
|
+
error_response(catch(:error) { return @app.call(@env) })
|
|
22
|
+
rescue Exception => e # rubocop:disable Lint/RescueException
|
|
23
|
+
run_rescue_handler(find_handler(e.class), e, @env[Grape::Env::API_ENDPOINT])
|
|
24
|
+
end
|
|
23
25
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
is_rescuable = rescuable?(e.class)
|
|
30
|
-
if e.is_a?(Grape::Exceptions::Base) && !is_rescuable
|
|
31
|
-
handler = lambda {|e| error_response(e) }
|
|
32
|
-
else
|
|
33
|
-
raise unless is_rescuable
|
|
34
|
-
handler = options[:rescue_handlers][e.class] || options[:rescue_handlers][:all]
|
|
35
|
-
end
|
|
36
|
-
handler.nil? ? handle_error(e) : self.instance_exec(e, &handler)
|
|
37
|
-
end
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def rack_response(status, headers, message)
|
|
29
|
+
message = Rack::Utils.escape_html(message) if headers[Rack::CONTENT_TYPE] == 'text/html'
|
|
30
|
+
Rack::Response.new(Array.wrap(message), Rack::Utils.status_code(status), Grape::Util::Header.new.merge(headers))
|
|
38
31
|
end
|
|
39
32
|
|
|
40
|
-
def
|
|
41
|
-
|
|
33
|
+
def format_message(message, backtrace, original_exception = nil)
|
|
34
|
+
format = env[Grape::Env::API_FORMAT] || options[:format]
|
|
35
|
+
formatter = Grape::ErrorFormatter.formatter_for(format, options[:error_formatters], options[:default_error_formatter])
|
|
36
|
+
return formatter.call(message, backtrace, options, env, original_exception) if formatter
|
|
37
|
+
|
|
38
|
+
throw :error,
|
|
39
|
+
status: 406,
|
|
40
|
+
message: "The requested format '#{format}' is not supported.",
|
|
41
|
+
backtrace:,
|
|
42
|
+
original_exception:
|
|
42
43
|
end
|
|
43
44
|
|
|
44
|
-
def
|
|
45
|
-
|
|
45
|
+
def find_handler(klass)
|
|
46
|
+
rescue_handler_for_base_only_class(klass) ||
|
|
47
|
+
rescue_handler_for_class_or_its_ancestor(klass) ||
|
|
48
|
+
rescue_handler_for_grape_exception(klass) ||
|
|
49
|
+
rescue_handler_for_any_class(klass) ||
|
|
50
|
+
raise
|
|
46
51
|
end
|
|
47
52
|
|
|
48
53
|
def error_response(error = {})
|
|
49
54
|
status = error[:status] || options[:default_status]
|
|
55
|
+
env[Grape::Env::API_ENDPOINT].status(status) # error! may not have been called
|
|
50
56
|
message = error[:message] || options[:default_message]
|
|
51
|
-
headers = {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
57
|
+
headers = { Rack::CONTENT_TYPE => content_type }.tap do |h|
|
|
58
|
+
h.merge!(error[:headers]) if error[:headers].is_a?(Hash)
|
|
59
|
+
end
|
|
60
|
+
backtrace = error[:backtrace] || error[:original_exception]&.backtrace || []
|
|
61
|
+
original_exception = error.is_a?(Exception) ? error : error[:original_exception]
|
|
62
|
+
rack_response(status, headers, format_message(message, backtrace, original_exception))
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def default_rescue_handler(exception)
|
|
66
|
+
error_response(message: exception.message, backtrace: exception.backtrace, original_exception: exception)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def rescue_handler_for_base_only_class(klass)
|
|
70
|
+
error, handler = options[:base_only_rescue_handlers]&.find { |err, _handler| klass == err }
|
|
71
|
+
|
|
72
|
+
return unless error
|
|
73
|
+
|
|
74
|
+
handler || method(:default_rescue_handler)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def rescue_handler_for_class_or_its_ancestor(klass)
|
|
78
|
+
error, handler = options[:rescue_handlers]&.find { |err, _handler| klass <= err }
|
|
79
|
+
|
|
80
|
+
return unless error
|
|
81
|
+
|
|
82
|
+
handler || method(:default_rescue_handler)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def rescue_handler_for_grape_exception(klass)
|
|
86
|
+
return unless klass <= Grape::Exceptions::Base
|
|
87
|
+
return method(:error_response) if klass == Grape::Exceptions::InvalidVersionHeader
|
|
88
|
+
return unless options[:rescue_grape_exceptions] || !options[:rescue_all]
|
|
89
|
+
|
|
90
|
+
options[:grape_exceptions_rescue_handler] || method(:error_response)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def rescue_handler_for_any_class(klass)
|
|
94
|
+
return unless klass <= StandardError
|
|
95
|
+
return unless options[:rescue_all] || options[:rescue_grape_exceptions]
|
|
96
|
+
|
|
97
|
+
options[:all_rescue_handler] || method(:default_rescue_handler)
|
|
55
98
|
end
|
|
56
99
|
|
|
57
|
-
def
|
|
58
|
-
|
|
100
|
+
def run_rescue_handler(handler, error, endpoint)
|
|
101
|
+
handler = endpoint.public_method(handler) if handler.instance_of?(Symbol)
|
|
102
|
+
response = catch(:error) do
|
|
103
|
+
handler.arity.zero? ? endpoint.instance_exec(&handler) : endpoint.instance_exec(error, &handler)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
if error?(response)
|
|
107
|
+
error_response(response)
|
|
108
|
+
elsif response.is_a?(Rack::Response)
|
|
109
|
+
response
|
|
110
|
+
else
|
|
111
|
+
run_rescue_handler(method(:default_rescue_handler), Grape::Exceptions::InvalidResponse.new, endpoint)
|
|
112
|
+
end
|
|
59
113
|
end
|
|
60
114
|
|
|
61
|
-
def
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
115
|
+
def error!(message, status = options[:default_status], headers = {}, backtrace = [], original_exception = nil)
|
|
116
|
+
env[Grape::Env::API_ENDPOINT].status(status) # not error! inside route
|
|
117
|
+
rack_response(
|
|
118
|
+
status, headers.reverse_merge(Rack::CONTENT_TYPE => content_type),
|
|
119
|
+
format_message(message, backtrace, original_exception)
|
|
120
|
+
)
|
|
66
121
|
end
|
|
67
122
|
|
|
123
|
+
def error?(response)
|
|
124
|
+
return false unless response.is_a?(Hash)
|
|
125
|
+
|
|
126
|
+
response.key?(:message) && response.key?(:status) && response.key?(:headers)
|
|
127
|
+
end
|
|
68
128
|
end
|
|
69
129
|
end
|
|
70
130
|
end
|
|
@@ -1,16 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Grape
|
|
2
4
|
module Middleware
|
|
3
5
|
# This is a simple middleware for adding before and after filters
|
|
4
6
|
# to Grape APIs. It is used like so:
|
|
5
7
|
#
|
|
6
|
-
# use Grape::Middleware::Filter, :
|
|
8
|
+
# use Grape::Middleware::Filter, before: -> { do_something }, after: -> { do_something }
|
|
7
9
|
class Filter < Base
|
|
8
10
|
def before
|
|
9
|
-
app.instance_eval
|
|
11
|
+
app.instance_eval(&options[:before]) if options[:before]
|
|
10
12
|
end
|
|
11
13
|
|
|
12
14
|
def after
|
|
13
|
-
app.instance_eval
|
|
15
|
+
app.instance_eval(&options[:after]) if options[:after]
|
|
14
16
|
end
|
|
15
17
|
end
|
|
16
18
|
end
|
|
@@ -1,20 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Grape
|
|
4
4
|
module Middleware
|
|
5
5
|
class Formatter < Base
|
|
6
|
+
DEFAULT_OPTIONS = {
|
|
7
|
+
default_format: :txt
|
|
8
|
+
}.freeze
|
|
6
9
|
|
|
7
|
-
|
|
8
|
-
{
|
|
9
|
-
:default_format => :txt,
|
|
10
|
-
:formatters => {},
|
|
11
|
-
:parsers => {}
|
|
12
|
-
}
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def headers
|
|
16
|
-
env.dup.inject({}){|h,(k,v)| h[k.to_s.downcase[5..-1]] = v if k.to_s.downcase.start_with?('http_'); h}
|
|
17
|
-
end
|
|
10
|
+
ALL_MEDIA_TYPES = '*/*'
|
|
18
11
|
|
|
19
12
|
def before
|
|
20
13
|
negotiate_content_type
|
|
@@ -22,121 +15,138 @@ module Grape
|
|
|
22
15
|
end
|
|
23
16
|
|
|
24
17
|
def after
|
|
18
|
+
return unless @app_response
|
|
19
|
+
|
|
25
20
|
status, headers, bodies = *@app_response
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
formatter.call body, env
|
|
32
|
-
end
|
|
33
|
-
rescue Grape::Exceptions::InvalidFormatter => e
|
|
34
|
-
throw :error, :status => 500, :message => e.message
|
|
21
|
+
|
|
22
|
+
if Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(status)
|
|
23
|
+
[status, headers, []]
|
|
24
|
+
else
|
|
25
|
+
build_formatted_response(status, headers, bodies)
|
|
35
26
|
end
|
|
36
|
-
headers['Content-Type'] = content_type_for(env['api.format']) unless headers['Content-Type']
|
|
37
|
-
Rack::Response.new(bodymap, status, headers).to_a
|
|
38
27
|
end
|
|
39
28
|
|
|
40
29
|
private
|
|
41
30
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (request.post? || request.put? || request.patch?) &&
|
|
45
|
-
(! request.form_data? || ! request.media_type) &&
|
|
46
|
-
(! request.parseable_data?) &&
|
|
47
|
-
(request.content_length.to_i > 0 || request.env['HTTP_TRANSFER_ENCODING'] == 'chunked')
|
|
48
|
-
|
|
49
|
-
if (input = env['rack.input'])
|
|
50
|
-
input.rewind
|
|
51
|
-
body = env['api.request.input'] = input.read
|
|
52
|
-
begin
|
|
53
|
-
read_rack_input(body) if body && body.length > 0
|
|
54
|
-
ensure
|
|
55
|
-
input.rewind
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
end
|
|
31
|
+
def build_formatted_response(status, headers, bodies)
|
|
32
|
+
headers = ensure_content_type(headers)
|
|
60
33
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
fmt ||= options[:default_format]
|
|
65
|
-
if content_type_for(fmt)
|
|
66
|
-
parser = Grape::Parser::Base.parser_for fmt, options
|
|
67
|
-
if parser
|
|
68
|
-
begin
|
|
69
|
-
body = (env['api.request.body'] = parser.call(body, env))
|
|
70
|
-
if body.is_a?(Hash)
|
|
71
|
-
env['rack.request.form_hash'] = env['rack.request.form_hash'] ?
|
|
72
|
-
env['rack.request.form_hash'].merge(body) :
|
|
73
|
-
body
|
|
74
|
-
env['rack.request.form_input'] = env['rack.input']
|
|
75
|
-
end
|
|
76
|
-
rescue Exception => e
|
|
77
|
-
throw :error, :status => 400, :message => e.message
|
|
78
|
-
end
|
|
79
|
-
else
|
|
80
|
-
env['api.request.body'] = body
|
|
81
|
-
end
|
|
82
|
-
else
|
|
83
|
-
throw :error, :status => 406, :message => "The requested content-type '#{request.media_type}' is not supported."
|
|
34
|
+
if bodies.is_a?(Grape::ServeStream::StreamResponse)
|
|
35
|
+
Grape::ServeStream::SendfileResponse.new([], status, headers) do |resp|
|
|
36
|
+
resp.body = bodies.stream
|
|
84
37
|
end
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
env['api.format'] = fmt
|
|
91
|
-
else
|
|
92
|
-
throw :error, :status => 406, :message => "The requested format '#{fmt}' is not supported."
|
|
38
|
+
else
|
|
39
|
+
# Allow content-type to be explicitly overwritten
|
|
40
|
+
formatter = fetch_formatter(headers, options)
|
|
41
|
+
bodymap = ActiveSupport::Notifications.instrument('format_response.grape', formatter:, env:) do
|
|
42
|
+
bodies.collect { |body| formatter.call(body, env) }
|
|
93
43
|
end
|
|
44
|
+
Rack::Response.new(bodymap, status, headers)
|
|
94
45
|
end
|
|
46
|
+
rescue Grape::Exceptions::InvalidFormatter => e
|
|
47
|
+
throw :error, status: 500, message: e.message, backtrace: e.backtrace, original_exception: e
|
|
48
|
+
end
|
|
95
49
|
|
|
96
|
-
|
|
97
|
-
|
|
50
|
+
def fetch_formatter(headers, options)
|
|
51
|
+
api_format = env.fetch(Grape::Env::API_FORMAT) { mime_types[headers[Rack::CONTENT_TYPE]] }
|
|
52
|
+
Grape::Formatter.formatter_for(api_format, options[:formatters])
|
|
53
|
+
end
|
|
98
54
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
55
|
+
# Set the content type header for the API format if it is not already present.
|
|
56
|
+
#
|
|
57
|
+
# @param headers [Hash]
|
|
58
|
+
# @return [Hash]
|
|
59
|
+
def ensure_content_type(headers)
|
|
60
|
+
if headers[Rack::CONTENT_TYPE]
|
|
61
|
+
headers
|
|
62
|
+
else
|
|
63
|
+
headers.merge(Rack::CONTENT_TYPE => content_type_for(env[Grape::Env::API_FORMAT]))
|
|
105
64
|
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def read_body_input
|
|
68
|
+
input = rack_request.body # reads RACK_INPUT
|
|
69
|
+
return if input.nil?
|
|
70
|
+
return unless read_body_input?
|
|
71
|
+
|
|
72
|
+
rewind = input.respond_to?(:rewind)
|
|
106
73
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
74
|
+
input.rewind if rewind
|
|
75
|
+
body = env[Grape::Env::API_REQUEST_INPUT] = input.read
|
|
76
|
+
begin
|
|
77
|
+
read_rack_input(body)
|
|
78
|
+
ensure
|
|
79
|
+
input.rewind if rewind
|
|
112
80
|
end
|
|
81
|
+
end
|
|
113
82
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
83
|
+
def read_rack_input(body)
|
|
84
|
+
return if body.empty?
|
|
85
|
+
|
|
86
|
+
media_type = rack_request.media_type
|
|
87
|
+
fmt = media_type ? mime_types[media_type] : options[:default_format]
|
|
88
|
+
|
|
89
|
+
throw :error, status: 415, message: "The provided content-type '#{media_type}' is not supported." unless content_type_for(fmt)
|
|
90
|
+
parser = Grape::Parser.parser_for fmt, options[:parsers]
|
|
91
|
+
if parser
|
|
92
|
+
begin
|
|
93
|
+
body = (env[Grape::Env::API_REQUEST_BODY] = parser.call(body, env))
|
|
94
|
+
if body.is_a?(Hash)
|
|
95
|
+
env[Rack::RACK_REQUEST_FORM_HASH] = if env.key?(Rack::RACK_REQUEST_FORM_HASH)
|
|
96
|
+
env[Rack::RACK_REQUEST_FORM_HASH].merge(body)
|
|
97
|
+
else
|
|
98
|
+
body
|
|
99
|
+
end
|
|
100
|
+
env[Rack::RACK_REQUEST_FORM_INPUT] = env[Rack::RACK_INPUT]
|
|
118
101
|
end
|
|
102
|
+
rescue Grape::Exceptions::Base => e
|
|
103
|
+
raise e
|
|
104
|
+
rescue StandardError => e
|
|
105
|
+
throw :error, status: 400, message: e.message, backtrace: e.backtrace, original_exception: e
|
|
119
106
|
end
|
|
120
|
-
|
|
107
|
+
else
|
|
108
|
+
env[Grape::Env::API_REQUEST_BODY] = body
|
|
121
109
|
end
|
|
110
|
+
end
|
|
122
111
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
112
|
+
# this middleware will not try to format the following content-types since Rack already handles them
|
|
113
|
+
# when calling Rack's `params` function
|
|
114
|
+
# - application/x-www-form-urlencoded
|
|
115
|
+
# - multipart/form-data
|
|
116
|
+
# - multipart/related
|
|
117
|
+
# - multipart/mixed
|
|
118
|
+
def read_body_input?
|
|
119
|
+
(rack_request.post? || rack_request.put? || rack_request.patch? || rack_request.delete?) &&
|
|
120
|
+
!(rack_request.form_data? && rack_request.content_type) &&
|
|
121
|
+
!rack_request.parseable_data? &&
|
|
122
|
+
(rack_request.content_length.to_i.positive? || rack_request.env['HTTP_TRANSFER_ENCODING'] == 'chunked')
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def negotiate_content_type
|
|
126
|
+
fmt = format_from_extension || query_params['format'] || options[:format] || format_from_header || options[:default_format]
|
|
127
|
+
if content_type_for(fmt)
|
|
128
|
+
env[Grape::Env::API_FORMAT] = fmt.to_sym
|
|
129
|
+
else
|
|
130
|
+
throw :error, status: 406, message: "The requested format '#{fmt}' is not supported."
|
|
138
131
|
end
|
|
132
|
+
end
|
|
139
133
|
|
|
134
|
+
def format_from_extension
|
|
135
|
+
request_path = try_scrub(rack_request.path)
|
|
136
|
+
dot_pos = request_path.rindex('.')
|
|
137
|
+
return unless dot_pos
|
|
138
|
+
|
|
139
|
+
extension = request_path[(dot_pos + 1)..]
|
|
140
|
+
extension if content_type_for(extension)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def format_from_header
|
|
144
|
+
accept_header = try_scrub(env['HTTP_ACCEPT'])
|
|
145
|
+
return if accept_header.blank? || accept_header == ALL_MEDIA_TYPES
|
|
146
|
+
|
|
147
|
+
media_type = Rack::Utils.best_q_match(accept_header, mime_types.keys)
|
|
148
|
+
mime_types[media_type] if media_type
|
|
149
|
+
end
|
|
140
150
|
end
|
|
141
151
|
end
|
|
142
152
|
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Middleware
|
|
5
|
+
class Globals < Base
|
|
6
|
+
def before
|
|
7
|
+
request = Grape::Request.new(@env, build_params_with: @options[:build_params_with])
|
|
8
|
+
@env[Grape::Env::GRAPE_REQUEST] = request
|
|
9
|
+
@env[Grape::Env::GRAPE_REQUEST_HEADERS] = request.headers
|
|
10
|
+
@env[Grape::Env::GRAPE_REQUEST_PARAMS] = request.params if @env[Rack::RACK_INPUT]
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Grape
|
|
4
|
+
module Middleware
|
|
5
|
+
# Class to handle the stack of middlewares based on ActionDispatch::MiddlewareStack
|
|
6
|
+
# It allows to insert and insert after
|
|
7
|
+
class Stack
|
|
8
|
+
extend Forwardable
|
|
9
|
+
|
|
10
|
+
class Middleware
|
|
11
|
+
attr_reader :args, :block, :klass
|
|
12
|
+
|
|
13
|
+
def initialize(klass, args, block)
|
|
14
|
+
@klass = klass
|
|
15
|
+
@args = args
|
|
16
|
+
@block = block
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def name
|
|
20
|
+
klass.name
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def ==(other)
|
|
24
|
+
case other
|
|
25
|
+
when Middleware
|
|
26
|
+
klass == other.klass
|
|
27
|
+
when Class
|
|
28
|
+
klass == other || (name.nil? && klass.superclass == other)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def inspect
|
|
33
|
+
klass.to_s
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def build(builder)
|
|
37
|
+
# we need to force the ruby2_keywords_hash for middlewares that initialize contains keywords
|
|
38
|
+
# like ActionDispatch::RequestId since middleware arguments are serialized
|
|
39
|
+
# https://rubyapi.org/3.4/o/hash#method-c-ruby2_keywords_hash
|
|
40
|
+
args[-1] = Hash.ruby2_keywords_hash(args[-1]) if args.last.is_a?(Hash) && Hash.respond_to?(:ruby2_keywords_hash)
|
|
41
|
+
builder.use(klass, *args, &block)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
include Enumerable
|
|
46
|
+
|
|
47
|
+
attr_accessor :middlewares, :others
|
|
48
|
+
|
|
49
|
+
def_delegators :middlewares, :each, :size, :last, :[]
|
|
50
|
+
|
|
51
|
+
def initialize
|
|
52
|
+
@middlewares = []
|
|
53
|
+
@others = []
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def insert(index, klass, *args, &block)
|
|
57
|
+
index = assert_index(index, :before)
|
|
58
|
+
middlewares.insert(index, self.class::Middleware.new(klass, args, block))
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
alias insert_before insert
|
|
62
|
+
|
|
63
|
+
def insert_after(index, ...)
|
|
64
|
+
index = assert_index(index, :after)
|
|
65
|
+
insert(index + 1, ...)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def use(klass, *args, &block)
|
|
69
|
+
middleware = self.class::Middleware.new(klass, args, block)
|
|
70
|
+
middlewares.push(middleware)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def merge_with(middleware_specs)
|
|
74
|
+
middleware_specs.each do |operation, klass, *args|
|
|
75
|
+
if args.last.is_a?(Proc)
|
|
76
|
+
last_proc = args.pop
|
|
77
|
+
public_send(operation, klass, *args, &last_proc)
|
|
78
|
+
else
|
|
79
|
+
public_send(operation, klass, *args)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# @return [Rack::Builder] the builder object with our middlewares applied
|
|
85
|
+
def build
|
|
86
|
+
Rack::Builder.new.tap do |builder|
|
|
87
|
+
others.shift(others.size).each { |m| merge_with(m) }
|
|
88
|
+
middlewares.each do |m|
|
|
89
|
+
m.build(builder)
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# @description Add middlewares with :use operation to the stack. Store others with :insert_* operation for later
|
|
95
|
+
# @param [Array] other_specs An array of middleware specifications (e.g. [[:use, klass], [:insert_before, *args]])
|
|
96
|
+
def concat(other_specs)
|
|
97
|
+
use, not_use = other_specs.partition { |o| o.first == :use }
|
|
98
|
+
others << not_use
|
|
99
|
+
merge_with(use)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
protected
|
|
103
|
+
|
|
104
|
+
def assert_index(index, where)
|
|
105
|
+
i = index.is_a?(Integer) ? index : middlewares.index(index)
|
|
106
|
+
i || raise("No such middleware to insert #{where}: #{index.inspect}")
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Grape
|
|
4
4
|
module Middleware
|
|
@@ -11,56 +11,27 @@ module Grape
|
|
|
11
11
|
#
|
|
12
12
|
# The following rack env variables are set:
|
|
13
13
|
#
|
|
14
|
-
# env['api.version] => 'v1'
|
|
14
|
+
# env['api.version'] => 'v1'
|
|
15
15
|
#
|
|
16
16
|
# If version does not match this route, then a 406 is raised with
|
|
17
|
-
# X-Cascade header to alert
|
|
17
|
+
# X-Cascade header to alert Grape::Router to attempt the next matched
|
|
18
18
|
# route.
|
|
19
19
|
class AcceptVersionHeader < Base
|
|
20
|
-
|
|
21
20
|
def before
|
|
22
|
-
potential_version = (env['HTTP_ACCEPT_VERSION']
|
|
23
|
-
|
|
24
|
-
if strict?
|
|
25
|
-
# If no Accept-Version header:
|
|
26
|
-
if potential_version.empty?
|
|
27
|
-
throw :error, :status => 406, :headers => error_headers, :message => 'Accept-Version header must be set.'
|
|
28
|
-
end
|
|
29
|
-
end
|
|
21
|
+
potential_version = try_scrub(env['HTTP_ACCEPT_VERSION'])
|
|
22
|
+
not_acceptable!('Accept-Version header must be set.') if strict && potential_version.blank?
|
|
30
23
|
|
|
31
|
-
|
|
32
|
-
# If the requested version is not supported:
|
|
33
|
-
if !versions.any? { |v| v.to_s == potential_version }
|
|
34
|
-
throw :error, :status => 406, :headers => error_headers, :message => 'The requested version is not supported.'
|
|
35
|
-
end
|
|
24
|
+
return if potential_version.blank?
|
|
36
25
|
|
|
37
|
-
|
|
38
|
-
|
|
26
|
+
not_acceptable!('The requested version is not supported.') unless potential_version_match?(potential_version)
|
|
27
|
+
env[Grape::Env::API_VERSION] = potential_version
|
|
39
28
|
end
|
|
40
29
|
|
|
41
|
-
|
|
30
|
+
private
|
|
42
31
|
|
|
43
|
-
def
|
|
44
|
-
|
|
32
|
+
def not_acceptable!(message)
|
|
33
|
+
throw :error, status: 406, headers: error_headers, message:
|
|
45
34
|
end
|
|
46
|
-
|
|
47
|
-
def strict?
|
|
48
|
-
options[:version_options] && options[:version_options][:strict]
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
# By default those errors contain an `X-Cascade` header set to `pass`, which allows nesting and stacking
|
|
52
|
-
# of routes (see [Rack::Mount](https://github.com/josh/rack-mount) for more information). To prevent
|
|
53
|
-
# this behavior, and not add the `X-Cascade` header, one can set the `:cascade` option to `false`.
|
|
54
|
-
def cascade?
|
|
55
|
-
options[:version_options] && options[:version_options].has_key?(:cascade) ?
|
|
56
|
-
!! options[:version_options][:cascade] :
|
|
57
|
-
true
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
def error_headers
|
|
61
|
-
cascade? ? { 'X-Cascade' => 'pass' } : {}
|
|
62
|
-
end
|
|
63
|
-
|
|
64
35
|
end
|
|
65
36
|
end
|
|
66
37
|
end
|