typhoeus 0.4.2 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.rspec +4 -0
- data/.travis.yml +26 -0
- data/CHANGELOG.md +341 -28
- data/CONTRIBUTING.md +20 -0
- data/Gemfile +31 -2
- data/Guardfile +9 -0
- data/LICENSE +1 -1
- data/README.md +486 -357
- data/Rakefile +21 -12
- data/UPGRADE.md +55 -0
- data/lib/rack/typhoeus/middleware/params_decoder/helper.rb +76 -0
- data/lib/rack/typhoeus/middleware/params_decoder.rb +57 -0
- data/lib/rack/typhoeus.rb +1 -0
- data/lib/typhoeus/adapters/faraday.rb +180 -0
- data/lib/typhoeus/cache/dalli.rb +28 -0
- data/lib/typhoeus/cache/rails.rb +28 -0
- data/lib/typhoeus/cache/redis.rb +35 -0
- data/lib/typhoeus/config.rb +69 -0
- data/lib/typhoeus/easy_factory.rb +180 -0
- data/lib/typhoeus/errors/no_stub.rb +12 -0
- data/lib/typhoeus/errors/typhoeus_error.rb +8 -0
- data/lib/typhoeus/errors.rb +9 -0
- data/lib/typhoeus/expectation.rb +217 -0
- data/lib/typhoeus/hydra/addable.rb +23 -0
- data/lib/typhoeus/hydra/before.rb +31 -0
- data/lib/typhoeus/hydra/block_connection.rb +35 -0
- data/lib/typhoeus/hydra/cacheable.rb +15 -0
- data/lib/typhoeus/hydra/memoizable.rb +56 -0
- data/lib/typhoeus/hydra/queueable.rb +83 -0
- data/lib/typhoeus/hydra/runnable.rb +19 -0
- data/lib/typhoeus/hydra/stubbable.rb +28 -0
- data/lib/typhoeus/hydra.rb +84 -236
- data/lib/typhoeus/pool.rb +70 -0
- data/lib/typhoeus/railtie.rb +12 -0
- data/lib/typhoeus/request/actions.rb +125 -0
- data/lib/typhoeus/request/before.rb +30 -0
- data/lib/typhoeus/request/block_connection.rb +52 -0
- data/lib/typhoeus/request/cacheable.rb +38 -0
- data/lib/typhoeus/request/callbacks.rb +151 -0
- data/lib/typhoeus/request/marshal.rb +22 -0
- data/lib/typhoeus/request/memoizable.rb +38 -0
- data/lib/typhoeus/request/operations.rb +40 -0
- data/lib/typhoeus/request/responseable.rb +29 -0
- data/lib/typhoeus/request/streamable.rb +34 -0
- data/lib/typhoeus/request/stubbable.rb +30 -0
- data/lib/typhoeus/request.rb +186 -231
- data/lib/typhoeus/response/cacheable.rb +14 -0
- data/lib/typhoeus/response/header.rb +105 -0
- data/lib/typhoeus/response/informations.rb +248 -0
- data/lib/typhoeus/response/status.rb +106 -0
- data/lib/typhoeus/response.rb +60 -115
- data/lib/typhoeus/version.rb +3 -1
- data/lib/typhoeus.rb +126 -39
- data/perf/profile.rb +14 -0
- data/perf/vs_nethttp.rb +64 -0
- data/spec/rack/typhoeus/middleware/params_decoder/helper_spec.rb +156 -0
- data/spec/rack/typhoeus/middleware/params_decoder_spec.rb +31 -0
- data/spec/spec_helper.rb +29 -0
- data/spec/support/localhost_server.rb +94 -0
- data/spec/support/memory_cache.rb +15 -0
- data/spec/support/server.rb +116 -0
- data/spec/typhoeus/adapters/faraday_spec.rb +339 -0
- data/spec/typhoeus/cache/dalli_spec.rb +41 -0
- data/spec/typhoeus/cache/redis_spec.rb +41 -0
- data/spec/typhoeus/config_spec.rb +15 -0
- data/spec/typhoeus/easy_factory_spec.rb +143 -0
- data/spec/typhoeus/errors/no_stub_spec.rb +13 -0
- data/spec/typhoeus/expectation_spec.rb +280 -0
- data/spec/typhoeus/hydra/addable_spec.rb +22 -0
- data/spec/typhoeus/hydra/before_spec.rb +98 -0
- data/spec/typhoeus/hydra/block_connection_spec.rb +18 -0
- data/spec/typhoeus/hydra/cacheable_spec.rb +88 -0
- data/spec/typhoeus/hydra/memoizable_spec.rb +53 -0
- data/spec/typhoeus/hydra/queueable_spec.rb +98 -0
- data/spec/typhoeus/hydra/runnable_spec.rb +137 -0
- data/spec/typhoeus/hydra/stubbable_spec.rb +48 -0
- data/spec/typhoeus/hydra_spec.rb +22 -0
- data/spec/typhoeus/pool_spec.rb +137 -0
- data/spec/typhoeus/request/actions_spec.rb +19 -0
- data/spec/typhoeus/request/before_spec.rb +93 -0
- data/spec/typhoeus/request/block_connection_spec.rb +75 -0
- data/spec/typhoeus/request/cacheable_spec.rb +94 -0
- data/spec/typhoeus/request/callbacks_spec.rb +91 -0
- data/spec/typhoeus/request/marshal_spec.rb +60 -0
- data/spec/typhoeus/request/memoizable_spec.rb +34 -0
- data/spec/typhoeus/request/operations_spec.rb +101 -0
- data/spec/typhoeus/request/responseable_spec.rb +13 -0
- data/spec/typhoeus/request/stubbable_spec.rb +45 -0
- data/spec/typhoeus/request_spec.rb +232 -0
- data/spec/typhoeus/response/header_spec.rb +147 -0
- data/spec/typhoeus/response/informations_spec.rb +283 -0
- data/spec/typhoeus/response/status_spec.rb +256 -0
- data/spec/typhoeus/response_spec.rb +100 -0
- data/spec/typhoeus_spec.rb +105 -0
- data/typhoeus.gemspec +25 -0
- metadata +146 -158
- data/lib/typhoeus/curl.rb +0 -453
- data/lib/typhoeus/easy/auth.rb +0 -14
- data/lib/typhoeus/easy/callbacks.rb +0 -33
- data/lib/typhoeus/easy/ffi_helper.rb +0 -61
- data/lib/typhoeus/easy/infos.rb +0 -90
- data/lib/typhoeus/easy/options.rb +0 -115
- data/lib/typhoeus/easy/proxy.rb +0 -20
- data/lib/typhoeus/easy/ssl.rb +0 -82
- data/lib/typhoeus/easy.rb +0 -115
- data/lib/typhoeus/filter.rb +0 -28
- data/lib/typhoeus/form.rb +0 -61
- data/lib/typhoeus/header.rb +0 -54
- data/lib/typhoeus/hydra/callbacks.rb +0 -24
- data/lib/typhoeus/hydra/connect_options.rb +0 -61
- data/lib/typhoeus/hydra/stubbing.rb +0 -68
- data/lib/typhoeus/hydra_mock.rb +0 -131
- data/lib/typhoeus/multi.rb +0 -146
- data/lib/typhoeus/param_processor.rb +0 -43
- data/lib/typhoeus/remote.rb +0 -306
- data/lib/typhoeus/remote_method.rb +0 -108
- data/lib/typhoeus/remote_proxy_object.rb +0 -50
- data/lib/typhoeus/utils.rb +0 -50
@@ -0,0 +1,151 @@
|
|
1
|
+
module Typhoeus
|
2
|
+
class Request
|
3
|
+
|
4
|
+
# This module contains the logic for the response callbacks.
|
5
|
+
#
|
6
|
+
# You can set multiple callbacks, which are then executed
|
7
|
+
# in the same order.
|
8
|
+
#
|
9
|
+
# request.on_complete { |response| p 1 }
|
10
|
+
# request.on_complete { |response| p 2 }
|
11
|
+
# request.execute_callbacks
|
12
|
+
# #=> 1
|
13
|
+
# #=> 2
|
14
|
+
#
|
15
|
+
# You can clear the callbacks:
|
16
|
+
#
|
17
|
+
# request.on_complete { |response| p 1 }
|
18
|
+
# request.on_complete { |response| p 2 }
|
19
|
+
# request.on_complete.clear
|
20
|
+
# request.execute_callbacks
|
21
|
+
# #=> nil
|
22
|
+
#
|
23
|
+
# @note If you're using the Hydra to execute multiple
|
24
|
+
# requests, then callbacks are delaying the
|
25
|
+
# request execution.
|
26
|
+
module Callbacks
|
27
|
+
|
28
|
+
module Types # :nodoc:
|
29
|
+
# Set on_complete callback.
|
30
|
+
#
|
31
|
+
# @example Set on_complete.
|
32
|
+
# request.on_complete { |response| p "yay" }
|
33
|
+
#
|
34
|
+
# @param [ Block ] block The block to execute.
|
35
|
+
#
|
36
|
+
# @yield [ Typhoeus::Response ]
|
37
|
+
#
|
38
|
+
# @return [ Array<Block> ] All on_complete blocks.
|
39
|
+
def on_complete(&block)
|
40
|
+
@on_complete ||= []
|
41
|
+
@on_complete << block if block_given?
|
42
|
+
@on_complete
|
43
|
+
end
|
44
|
+
|
45
|
+
# Set on_success callback.
|
46
|
+
#
|
47
|
+
# @example Set on_success.
|
48
|
+
# request.on_success { |response| p "yay" }
|
49
|
+
#
|
50
|
+
# @param [ Block ] block The block to execute.
|
51
|
+
#
|
52
|
+
# @yield [ Typhoeus::Response ]
|
53
|
+
#
|
54
|
+
# @return [ Array<Block> ] All on_success blocks.
|
55
|
+
def on_success(&block)
|
56
|
+
@on_success ||= []
|
57
|
+
@on_success << block if block_given?
|
58
|
+
@on_success
|
59
|
+
end
|
60
|
+
|
61
|
+
# Set on_failure callback.
|
62
|
+
#
|
63
|
+
# @example Set on_failure.
|
64
|
+
# request.on_failure { |response| p "yay" }
|
65
|
+
#
|
66
|
+
# @param [ Block ] block The block to execute.
|
67
|
+
#
|
68
|
+
# @yield [ Typhoeus::Response ]
|
69
|
+
#
|
70
|
+
# @return [ Array<Block> ] All on_failure blocks.
|
71
|
+
def on_failure(&block)
|
72
|
+
@on_failure ||= []
|
73
|
+
@on_failure << block if block_given?
|
74
|
+
@on_failure
|
75
|
+
end
|
76
|
+
|
77
|
+
# Set on_headers callback.
|
78
|
+
#
|
79
|
+
# @example Set on_headers.
|
80
|
+
# request.on_headers { |response| p "yay" }
|
81
|
+
#
|
82
|
+
# @param [ Block ] block The block to execute.
|
83
|
+
#
|
84
|
+
# @yield [ Typhoeus::Response ]
|
85
|
+
#
|
86
|
+
# @return [ Array<Block> ] All on_headers blocks.
|
87
|
+
def on_headers(&block)
|
88
|
+
@on_headers ||= []
|
89
|
+
@on_headers << block if block_given?
|
90
|
+
@on_headers
|
91
|
+
end
|
92
|
+
|
93
|
+
# Set on_progress callback.
|
94
|
+
#
|
95
|
+
# @example Set on_progress.
|
96
|
+
# request.on_progress do |dltotal, dlnow, ultotal, ulnow|
|
97
|
+
# puts "dltotal (#{dltotal}), dlnow (#{dlnow}), ultotal (#{ultotal}), ulnow (#{ulnow})"
|
98
|
+
# end
|
99
|
+
#
|
100
|
+
# @param [ Block ] block The block to execute.
|
101
|
+
#
|
102
|
+
# @yield [ Typhoeus::Response ]
|
103
|
+
#
|
104
|
+
# @return [ Array<Block> ] All on_progress blocks.
|
105
|
+
def on_progress(&block)
|
106
|
+
@on_progress ||= []
|
107
|
+
@on_progress << block if block_given?
|
108
|
+
@on_progress
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
# Execute the headers callbacks and yields response.
|
113
|
+
#
|
114
|
+
# @example Execute callbacks.
|
115
|
+
# request.execute_headers_callbacks
|
116
|
+
#
|
117
|
+
# @return [ Array<Object> ] The results of the on_headers callbacks.
|
118
|
+
#
|
119
|
+
# @api private
|
120
|
+
def execute_headers_callbacks(response)
|
121
|
+
(Typhoeus.on_headers + on_headers).map do |callback|
|
122
|
+
callback.call(response)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# Execute necessary callback and yields response. This
|
127
|
+
# include in every case on_complete and on_progress, on_success
|
128
|
+
# if successful and on_failure if not.
|
129
|
+
#
|
130
|
+
# @example Execute callbacks.
|
131
|
+
# request.execute_callbacks
|
132
|
+
#
|
133
|
+
# @return [ void ]
|
134
|
+
#
|
135
|
+
# @api private
|
136
|
+
def execute_callbacks
|
137
|
+
callbacks = Typhoeus.on_complete + Typhoeus.on_progress + on_complete + on_progress
|
138
|
+
|
139
|
+
if response && response.success?
|
140
|
+
callbacks += Typhoeus.on_success + on_success
|
141
|
+
elsif response
|
142
|
+
callbacks += Typhoeus.on_failure + on_failure
|
143
|
+
end
|
144
|
+
|
145
|
+
callbacks.each do |callback|
|
146
|
+
self.response.handled_response = callback.call(self.response)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Typhoeus
|
2
|
+
class Request
|
3
|
+
|
4
|
+
# This module contains custom serializer.
|
5
|
+
module Marshal
|
6
|
+
|
7
|
+
# Return the important data needed to serialize this Request, except the
|
8
|
+
# request callbacks and `hydra`, since they cannot be marshalled.
|
9
|
+
def marshal_dump
|
10
|
+
unmarshallable = %w(@on_complete @on_success @on_failure @on_progress @on_headers @on_body @hydra)
|
11
|
+
(instance_variables - unmarshallable - unmarshallable.map(&:to_sym)).map do |name|
|
12
|
+
[name, instance_variable_get(name)]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Load.
|
17
|
+
def marshal_load(attributes)
|
18
|
+
attributes.each { |name, value| instance_variable_set(name, value) }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Typhoeus
|
2
|
+
class Request
|
3
|
+
|
4
|
+
# This module handles the GET request memoization
|
5
|
+
# on the request side. Memoization needs to be turned
|
6
|
+
# on:
|
7
|
+
# Typhoeus.configure do |config|
|
8
|
+
# config.memoize = true
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
# @api private
|
12
|
+
module Memoizable
|
13
|
+
|
14
|
+
# Override response setter and memoizes response
|
15
|
+
# if the request is memoizable.
|
16
|
+
#
|
17
|
+
# @param [ Response ] response The response to set.
|
18
|
+
#
|
19
|
+
# @example Set response.
|
20
|
+
# request.response = response
|
21
|
+
def response=(response)
|
22
|
+
hydra.memory[self] = response if memoizable?
|
23
|
+
super
|
24
|
+
end
|
25
|
+
|
26
|
+
# Return whether a request is memoizable.
|
27
|
+
#
|
28
|
+
# @example Is request memoizable?
|
29
|
+
# request.memoizable?
|
30
|
+
#
|
31
|
+
# @return [ Boolean ] Return true if memoizable, false else.
|
32
|
+
def memoizable?
|
33
|
+
Typhoeus::Config.memoize &&
|
34
|
+
(options[:method].nil? || options[:method] == :get)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Typhoeus
|
2
|
+
class Request
|
3
|
+
|
4
|
+
# This module contains everything what is necessary
|
5
|
+
# to make a single request.
|
6
|
+
module Operations
|
7
|
+
|
8
|
+
# Run a request.
|
9
|
+
#
|
10
|
+
# @example Run a request.
|
11
|
+
# Typhoeus::Request.new("www.example.com").run
|
12
|
+
#
|
13
|
+
# @return [ Response ] The response.
|
14
|
+
def run
|
15
|
+
easy = EasyFactory.new(self).get
|
16
|
+
easy.perform
|
17
|
+
response
|
18
|
+
end
|
19
|
+
|
20
|
+
# Sets a response, the request on the response
|
21
|
+
# and executes the callbacks.
|
22
|
+
#
|
23
|
+
# @param [Typhoeus::Response] response The response.
|
24
|
+
# @param [Boolean] bypass_memoization Wether to bypass
|
25
|
+
# memoization or not. Decides how the response is set.
|
26
|
+
#
|
27
|
+
# @return [Typhoeus::Response] The response.
|
28
|
+
def finish(response, bypass_memoization = nil)
|
29
|
+
if bypass_memoization
|
30
|
+
@response = response
|
31
|
+
else
|
32
|
+
self.response = response
|
33
|
+
end
|
34
|
+
self.response.request = self
|
35
|
+
execute_callbacks
|
36
|
+
response
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Typhoeus
|
2
|
+
class Request
|
3
|
+
|
4
|
+
# This module contains logic for having a reponse
|
5
|
+
# getter and setter.
|
6
|
+
module Responseable
|
7
|
+
|
8
|
+
# Set the response.
|
9
|
+
#
|
10
|
+
# @example Set response.
|
11
|
+
# request.response = response
|
12
|
+
#
|
13
|
+
# @param [ Response ] value The response to set.
|
14
|
+
def response=(value)
|
15
|
+
@response = value
|
16
|
+
end
|
17
|
+
|
18
|
+
# Return the response.
|
19
|
+
#
|
20
|
+
# @example Return response.
|
21
|
+
# request.response
|
22
|
+
#
|
23
|
+
# @return [ Response ] The response.
|
24
|
+
def response
|
25
|
+
@response ||= nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Typhoeus
|
2
|
+
class Request
|
3
|
+
|
4
|
+
# This module contians the logic for response streaming.
|
5
|
+
module Streamable
|
6
|
+
|
7
|
+
# Set on_body callback.
|
8
|
+
#
|
9
|
+
# This callback will be called each time a portion of the body is read from the socket.
|
10
|
+
# Setting an on_body callback will cause the response body to be empty.
|
11
|
+
#
|
12
|
+
# @example Set on_body.
|
13
|
+
# request.on_body { |body_chunk, response| puts "Got #{body_chunk.bytesize} bytes" }
|
14
|
+
#
|
15
|
+
# @param [ Block ] block The block to execute.
|
16
|
+
#
|
17
|
+
# @yield [ Typhoeus::Response, String ]
|
18
|
+
#
|
19
|
+
# @return [ Array<Block> ] All on_body blocks.
|
20
|
+
def on_body(&block)
|
21
|
+
@on_body ||= []
|
22
|
+
@on_body << block if block_given?
|
23
|
+
@on_body
|
24
|
+
end
|
25
|
+
|
26
|
+
# Is this request using streaming?
|
27
|
+
#
|
28
|
+
# @return [ Boolean ] True if any on_body blocks have been set.
|
29
|
+
def streaming?
|
30
|
+
defined?(@on_body) && @on_body.any?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Typhoeus
|
2
|
+
class Request
|
3
|
+
|
4
|
+
# This module handles stubbing on the request side.
|
5
|
+
# It plays well with the block_connection configuration,
|
6
|
+
# which raises when you make a request which is not stubbed.
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
module Stubbable
|
10
|
+
|
11
|
+
# Override run in order to check for matching expectations.
|
12
|
+
# When an expectation is found, super is not called. Instead a
|
13
|
+
# canned response is assigned to the request.
|
14
|
+
#
|
15
|
+
# @example Run the request.
|
16
|
+
# request.run
|
17
|
+
#
|
18
|
+
# @return [ Response ] The response.
|
19
|
+
def run
|
20
|
+
if response = Expectation.response_for(self)
|
21
|
+
execute_headers_callbacks(response)
|
22
|
+
self.on_body.each{ |callback| callback.call(response.body, response) }
|
23
|
+
finish(response)
|
24
|
+
else
|
25
|
+
super
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|