wrest 4.0.0-universal-java-18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +169 -0
  3. data/LICENCE +7 -0
  4. data/README.md +436 -0
  5. data/bin/wrest +4 -0
  6. data/bin/wrest_shell.rb +23 -0
  7. data/lib/wrest/async_request/event_machine_backend.rb +32 -0
  8. data/lib/wrest/async_request/thread_backend.rb +34 -0
  9. data/lib/wrest/async_request/thread_pool.rb +29 -0
  10. data/lib/wrest/async_request.rb +51 -0
  11. data/lib/wrest/cache_proxy.rb +119 -0
  12. data/lib/wrest/caching/memcached.rb +37 -0
  13. data/lib/wrest/caching/redis.rb +38 -0
  14. data/lib/wrest/caching.rb +57 -0
  15. data/lib/wrest/callback.rb +70 -0
  16. data/lib/wrest/components/container/alias_accessors.rb +70 -0
  17. data/lib/wrest/components/container/typecaster.rb +178 -0
  18. data/lib/wrest/components/container.rb +204 -0
  19. data/lib/wrest/components/mutators/base.rb +65 -0
  20. data/lib/wrest/components/mutators/camel_to_snake_case.rb +26 -0
  21. data/lib/wrest/components/mutators/xml_type_caster.rb +56 -0
  22. data/lib/wrest/components/mutators.rb +42 -0
  23. data/lib/wrest/components/translators/content_types.rb +25 -0
  24. data/lib/wrest/components/translators/json.rb +36 -0
  25. data/lib/wrest/components/translators/txt.rb +35 -0
  26. data/lib/wrest/components/translators/xml/conversions.rb +56 -0
  27. data/lib/wrest/components/translators/xml.rb +77 -0
  28. data/lib/wrest/components/translators.rb +30 -0
  29. data/lib/wrest/components.rb +22 -0
  30. data/lib/wrest/core_ext/hash/conversions.rb +45 -0
  31. data/lib/wrest/core_ext/hash.rb +7 -0
  32. data/lib/wrest/core_ext/string/conversions.rb +38 -0
  33. data/lib/wrest/core_ext/string.rb +7 -0
  34. data/lib/wrest/exceptions.rb +38 -0
  35. data/lib/wrest/hash_with_case_insensitive_access.rb +52 -0
  36. data/lib/wrest/hash_with_indifferent_access.rb +442 -0
  37. data/lib/wrest/http_codes.rb +83 -0
  38. data/lib/wrest/http_shared/headers.rb +345 -0
  39. data/lib/wrest/http_shared/standard_headers.rb +22 -0
  40. data/lib/wrest/http_shared/standard_tokens.rb +21 -0
  41. data/lib/wrest/http_shared.rb +25 -0
  42. data/lib/wrest/multipart.rb +84 -0
  43. data/lib/wrest/native/connection_factory.rb +28 -0
  44. data/lib/wrest/native/delete.rb +27 -0
  45. data/lib/wrest/native/get.rb +83 -0
  46. data/lib/wrest/native/options.rb +27 -0
  47. data/lib/wrest/native/patch.rb +27 -0
  48. data/lib/wrest/native/post.rb +27 -0
  49. data/lib/wrest/native/post_multipart.rb +36 -0
  50. data/lib/wrest/native/put.rb +27 -0
  51. data/lib/wrest/native/put_multipart.rb +36 -0
  52. data/lib/wrest/native/redirection.rb +39 -0
  53. data/lib/wrest/native/request.rb +161 -0
  54. data/lib/wrest/native/response.rb +278 -0
  55. data/lib/wrest/native/session.rb +66 -0
  56. data/lib/wrest/native.rb +36 -0
  57. data/lib/wrest/test/request_patches.rb +12 -0
  58. data/lib/wrest/test.rb +3 -0
  59. data/lib/wrest/uri/builders.rb +48 -0
  60. data/lib/wrest/uri.rb +312 -0
  61. data/lib/wrest/uri_template.rb +63 -0
  62. data/lib/wrest/utils.rb +129 -0
  63. data/lib/wrest/version.rb +14 -0
  64. data/lib/wrest.rb +77 -0
  65. data/lib/wrest_no_ext.rb +7 -0
  66. metadata +286 -0
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2009 Sidu Ponnappa
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
8
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
+ # See the License for the specific language governing permissions and limitations under the License.
10
+
11
+ module Wrest
12
+ module Native
13
+ class Get < Request
14
+ QUERY_PARAMS_SEPERATOR = '?'
15
+ EMPTY_QUERY_PARAMS = ''
16
+
17
+ attr_reader :cache_proxy
18
+
19
+ def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
20
+ follow_redirects = options[:follow_redirects]
21
+ options[:follow_redirects] = (follow_redirects.nil? ? true : follow_redirects)
22
+
23
+ cache_store = (options[:cache_store] || Wrest::Caching.default_store) unless options[:disable_cache]
24
+ @cache_proxy = Wrest::CacheProxy.new(self, cache_store)
25
+
26
+ super(wrest_uri, Net::HTTP::Get, parameters, nil, headers, options)
27
+ end
28
+
29
+ # Checks equality between two Wrest::Native::Get objects.
30
+ # Comparing two Wrest::Native::Get objects with identical values for the following properties would return True.
31
+ # uri, parameters, username, password and ssh verify_mode.
32
+ def ==(other)
33
+ return true if equal?(other)
34
+ return false unless other.class == self.class
35
+ return true if these_fields_are_equal(other)
36
+
37
+ false
38
+ end
39
+
40
+ # Returns a hash value for this Wrest::Native::Get object.
41
+ # Objects that returns true when compared using the == operator would return the same hash value also.
42
+ def hash
43
+ [uri, parameters, username, password, verify_mode].hash
44
+ end
45
+
46
+ # :nodoc:
47
+ def invoke_with_cache_check
48
+ cache_proxy.get
49
+ end
50
+
51
+ alias invoke_without_cache_check invoke
52
+ alias invoke invoke_with_cache_check
53
+
54
+ def build_request_without_cache_store(cache_validation_headers)
55
+ new_headers = headers.clone.merge cache_validation_headers
56
+ new_options = # do not run this through the caching mechanism.
57
+ options.clone.tap do |opts|
58
+ opts.delete :cache_store
59
+ opts[:disable_cache] = true
60
+ end
61
+ Wrest::Native::Get.new(uri, parameters, new_headers, new_options)
62
+ end
63
+
64
+ def full_uri_string
65
+ @uri.to_s + query_params_string
66
+ end
67
+
68
+ private
69
+
70
+ def these_fields_are_equal(other)
71
+ (uri == other.uri) &&
72
+ (parameters == other.parameters) &&
73
+ (username == other.username) &&
74
+ (password == other.password) &&
75
+ (verify_mode == other.verify_mode)
76
+ end
77
+
78
+ def query_params_string
79
+ @parameters.any? ? QUERY_PARAMS_SEPERATOR + Utils.hash_to_param(@parameters) : EMPTY_QUERY_PARAMS
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2009 Sidu Ponnappa
4
+
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
9
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and limitations under the License.
11
+
12
+ module Wrest
13
+ module Native
14
+ class Options < Request
15
+ def initialize(wrest_uri, options = {})
16
+ super(
17
+ wrest_uri,
18
+ Net::HTTP::Options,
19
+ {},
20
+ nil,
21
+ {},
22
+ options
23
+ )
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2009 Sidu Ponnappa
4
+
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
9
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and limitations under the License.
11
+
12
+ module Wrest
13
+ module Native
14
+ class Patch < Request
15
+ def initialize(wrest_uri, body = '', headers = {}, parameters = {}, options = {})
16
+ super(
17
+ wrest_uri,
18
+ Net::HTTP::Patch,
19
+ parameters,
20
+ body,
21
+ headers,
22
+ options
23
+ )
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2009 Sidu Ponnappa
4
+
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
9
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and limitations under the License.
11
+
12
+ module Wrest
13
+ module Native
14
+ class Post < Request
15
+ def initialize(wrest_uri, body = '', headers = {}, parameters = {}, options = {})
16
+ super(
17
+ wrest_uri,
18
+ Net::HTTP::Post,
19
+ parameters,
20
+ body,
21
+ headers,
22
+ options
23
+ )
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2009 - 2010 Sidu Ponnappa
4
+
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
9
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and limitations under the License.
11
+ require 'net/http/post/multipart'
12
+
13
+ module Wrest
14
+ module Native
15
+ class PostMultipart < Request
16
+ def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
17
+ super(
18
+ wrest_uri,
19
+ Net::HTTP::Post::Multipart,
20
+ parameters,
21
+ nil,
22
+ headers,
23
+ options
24
+ )
25
+ end
26
+
27
+ def build_request(request_klass, uri, parameters, headers)
28
+ request_klass.new(uri.full_path, parameters, headers)
29
+ end
30
+
31
+ def do_request
32
+ @connection.request(@http_request)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2009 Sidu Ponnappa
4
+
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
9
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and limitations under the License.
11
+
12
+ module Wrest
13
+ module Native
14
+ class Put < Request
15
+ def initialize(wrest_uri, body = '', headers = {}, parameters = {}, options = {})
16
+ super(
17
+ wrest_uri,
18
+ Net::HTTP::Put,
19
+ parameters,
20
+ body,
21
+ headers,
22
+ options
23
+ )
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2009 - 2010 Sidu Ponnappa
4
+
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
9
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and limitations under the License.
11
+ require 'net/http/post/multipart'
12
+
13
+ module Wrest
14
+ module Native
15
+ class PutMultipart < Request
16
+ def initialize(wrest_uri, parameters = {}, headers = {}, options = {})
17
+ super(
18
+ wrest_uri,
19
+ Net::HTTP::Put::Multipart,
20
+ parameters,
21
+ nil,
22
+ headers,
23
+ options
24
+ )
25
+ end
26
+
27
+ def build_request(request_klass, uri, parameters, headers)
28
+ request_klass.new(uri.full_path, parameters, headers)
29
+ end
30
+
31
+ def do_request
32
+ @connection.request(@http_request)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2009 Sidu Ponnappa
4
+
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at Http://www.apache.org/licenses/LICENSE-2.0
8
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
9
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and limitations under the License.
11
+
12
+ module Wrest # :nodoc:
13
+ module Native # :nodoc:
14
+ # Constructed by Wrest::Response.new if the HTTP response code is 3xx
15
+ # (http://en.wikipedia.org/wiki/300_Multiple_Choices#3xx_Redirection)
16
+ #
17
+ # This class is necessary because Net::HTTP doesn't seem to support
18
+ # redirection natively.
19
+ class Redirection < Response
20
+ # A get is invoked on the url stored in the response headers
21
+ # under the key 'location' and the new Response is returned.
22
+ #
23
+ # The follow_redirects_count and follow_redirects_limit options
24
+ # should be present. follow_redirects_count will be incremented by 1.
25
+ #
26
+ # This method will raise a Wrest::Exceptions::AutoRedirectLimitExceeded
27
+ # if the follow_redirects_count equals the follow_redirects_limit.
28
+ def follow(redirect_request_options = {})
29
+ target = self['location']
30
+ redirect_request_options = redirect_request_options.clone
31
+
32
+ raise Wrest::Exceptions::AutoRedirectLimitExceeded if (redirect_request_options[:follow_redirects_count] += 1) > redirect_request_options[:follow_redirects_limit]
33
+
34
+ Wrest.logger.debug "-| Redirecting to #{target}"
35
+ Wrest::Uri.new(target, redirect_request_options).get
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,161 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2009 Sidu Ponnappa
4
+
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
9
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and limitations under the License.
11
+
12
+ module Wrest
13
+ module Native
14
+ # This represents a HTTP request. Typically you will never need to instantiate
15
+ # one of these yourself - you can use one of the more conveient APIs via Wrest::Uri
16
+ # or Wrest::Native::Get etc. instead.
17
+ class Request
18
+ attr_reader :http_request, :uri, :body, :headers, :username, :password, :follow_redirects,
19
+ :follow_redirects_limit, :follow_redirects_count, :timeout, :connection, :parameters,
20
+ :cache_store, :verify_mode, :options, :ca_path
21
+
22
+ # Valid tuples for the options are:
23
+ # :username => String, defaults to nil
24
+ # :password => String, defaults to nil
25
+ # :follow_redirects => Boolean, defaults to true for Get, false for anything else
26
+ # :follow_redirects_limit => Integer, defaults to 5. This is the number of redirects
27
+ # that Wrest will automatically follow before raising an
28
+ # Wrest::Exceptions::AutoRedirectLimitExceeded exception.
29
+ # For example, if you set this to 1, the very first redirect
30
+ # will raise the exception.
31
+ # :follow_redirects_count => Integer, defaults to 0. This is a count of the hops made to
32
+ # get to this request and increases by one for every redirect
33
+ # until the follow_redirects_limit is hit. You should never set
34
+ # this option yourself.
35
+ # :timeout => The period, in seconds, after which a Timeout::Error is raised
36
+ # in the event of a connection failing to open. Defaulted to 60 by Uri#create_connection.
37
+ # :connection => The HTTP Connection object to use. This is how a keep-alive connection can be
38
+ # used for multiple requests.
39
+ # :cache_store => The object which should be used as cache store for cacheable responses. If not supplied, caching will be disabled.
40
+ # :callback => A Hash whose keys are the response codes (or Range of response codes),
41
+ # and the values are the callback functions to be executed.
42
+ # eg: { <response code> => lambda { |response| some_operation } }
43
+ # The following options are Net::HTTP specific config options
44
+ # :detailed_http_logging => nil/$stdout/$stderr or File/Logger/IO object. Defaults to nil (recommended).
45
+ # *WARNING* : detailed_http_logging causes a serious security hole. Never use it in production code.
46
+ # :verify_mode => The verification mode to be used for Net::HTTP https connections. Defaults to OpenSSL::SSL::VERIFY_PEER
47
+ # :ca_path => The path to the certificates
48
+ def initialize(wrest_uri, http_request_klass, parameters = {}, body = nil, headers = {}, options = {}) # rubocop:disable Metrics/ParameterLists
49
+ setup_request_state!(body, headers, parameters, wrest_uri)
50
+ setup_options_state!(options)
51
+ @detailed_http_logging = options[:detailed_http_logging]
52
+ @follow_redirects = (@options[:follow_redirects] ||= false)
53
+ @follow_redirects_count = (@options[:follow_redirects_count] ||= 0)
54
+ @follow_redirects_limit = (@options[:follow_redirects_limit] ||= 5)
55
+ @callback = @options[:callback] || Wrest::Callback.new({})
56
+ @callback = @callback.merge(Wrest::Callback.new(@options[:callback_block] || {}))
57
+ @http_request = build_request(http_request_klass, @uri, @parameters, @headers)
58
+ end
59
+
60
+ # Makes a request, runs the appropriate callback if any and
61
+ # returns a Wrest::Native::Response.
62
+ #
63
+ # Data about the request is and logged to Wrest.logger
64
+ # The log entry contains the following information:
65
+ #
66
+ # <- indicates a request
67
+ # -> indicates a response
68
+ #
69
+ # The type of request is mentioned in caps, followed by a hash
70
+ # uniquely identifying a particular request/response pair.
71
+ # In a multi-process or multi-threaded scenario, this can be used
72
+ # to identify request-response pairs.
73
+ #
74
+ # The request hash is followed by a connection hash; requests using the
75
+ # same connection (effectively a keep-alive connection) will have the
76
+ # same connection hash.
77
+ #
78
+ # Passing nil for either username or password will skip HTTP authentication
79
+ #
80
+ # This is followed by the response code, the payload size and the time taken.
81
+ def invoke
82
+ response = nil
83
+ setup_connection!
84
+
85
+ response = execute_request(response)
86
+
87
+ execute_callback_if_any(response)
88
+
89
+ @follow_redirects ? response.follow(@options) : response
90
+ rescue Timeout::Error => e
91
+ raise Wrest::Exceptions::Timeout, e
92
+ end
93
+
94
+ # :nodoc:
95
+ def build_request(request_klass, uri, parameters, headers)
96
+ if uri.query.empty?
97
+ request_klass.new(parameters.empty? ? uri.uri_path.to_s : "#{uri.uri_path}?#{Utils.hash_to_param(parameters)}", headers)
98
+ else
99
+ request_klass.new(
100
+ parameters.empty? ? "#{uri.uri_path}?#{uri.query}" : "#{uri.uri_path}?#{uri.query}&#{Utils.hash_to_param(parameters)}", headers
101
+ )
102
+ end
103
+ end
104
+
105
+ # :nodoc:
106
+ def do_request
107
+ @connection.request(@http_request, @body)
108
+ end
109
+
110
+ # :nodoc:
111
+ def execute_callback_if_any(actual_response)
112
+ @callback.execute(actual_response)
113
+ end
114
+
115
+ private
116
+
117
+ def setup_connection!
118
+ @connection ||= @uri.create_connection(timeout: timeout, verify_mode: verify_mode, ca_path: ca_path)
119
+ @connection.set_debug_output @detailed_http_logging
120
+ http_request.basic_auth(username, password) unless username.nil? || password.nil?
121
+ end
122
+
123
+ def execute_request(response)
124
+ prefix = "#{http_request.method} #{hash} #{@connection.hash} #{Thread.current.object_id}"
125
+
126
+ log_before_request(prefix)
127
+ time = Benchmark.realtime { response = Wrest::Native::Response.new(do_request) }
128
+ log_after_request(prefix, time)
129
+
130
+ response
131
+ end
132
+
133
+ def log_after_request(prefix, time)
134
+ Wrest.logger.debug "<- (#{prefix}) Time: #{time}"
135
+ end
136
+
137
+ def log_before_request(prefix)
138
+ Wrest.logger.debug "<- (#{prefix}) #{@uri.protocol}://#{@uri.host}:#{@uri.port}#{@http_request.path}"
139
+ Wrest.logger.debug "<- (#{prefix}) Body: #{@body}"
140
+ end
141
+
142
+ def setup_request_state!(body, headers, parameters, wrest_uri)
143
+ @uri = wrest_uri
144
+ @headers = headers.transform_keys(&:to_s)
145
+ @parameters = parameters
146
+ @body = body
147
+ end
148
+
149
+ def setup_options_state!(options)
150
+ @options = options.clone
151
+ @username = @options[:username]
152
+ @password = @options[:password]
153
+ @timeout = @options[:timeout]
154
+ @connection = @options[:connection]
155
+ @cache_store = @options[:cache_store]
156
+ @verify_mode = @options[:verify_mode]
157
+ @ca_path = @options[:ca_path]
158
+ end
159
+ end
160
+ end
161
+ end