-
Notifications
You must be signed in to change notification settings - Fork 150
/
httpi.rb
195 lines (169 loc) · 5.11 KB
/
httpi.rb
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
require "uri"
require "httpi/version"
require "httpi/logger"
require "httpi/request"
require "httpi/query_builder"
require "httpi/utils"
require "httpi/adapter/httpclient"
require "httpi/adapter/curb"
require "httpi/adapter/excon"
require "httpi/adapter/net_http"
require "httpi/adapter/net_http_persistent"
require "httpi/adapter/em_http"
require "httpi/adapter/rack"
require "httpi/adapter/http"
# = HTTPI
#
# Executes HTTP requests using a predefined adapter.
# All request methods accept an <tt>HTTPI::Request</tt> and an optional adapter.
# They may also offer shortcut methods for executing basic requests.
# Also they all return an <tt>HTTPI::Response</tt>.
#
# == GET
#
# request = HTTPI::Request.new("http://example.com")
# HTTPI.get(request, :httpclient)
#
# === Shortcuts
#
# HTTPI.get("http://example.com", :curb)
#
# == POST
#
# request = HTTPI::Request.new
# request.url = "http://example.com"
# request.body = "<some>xml</some>"
#
# HTTPI.post(request, :httpclient)
#
# === Shortcuts
#
# HTTPI.post("http://example.com", "<some>xml</some>", :curb)
#
# == HEAD
#
# request = HTTPI::Request.new("http://example.com")
# HTTPI.head(request, :httpclient)
#
# === Shortcuts
#
# HTTPI.head("http://example.com", :curb)
#
# == PUT
#
# request = HTTPI::Request.new
# request.url = "http://example.com"
# request.body = "<some>xml</some>"
#
# HTTPI.put(request, :httpclient)
#
# === Shortcuts
#
# HTTPI.put("http://example.com", "<some>xml</some>", :curb)
#
# == DELETE
#
# request = HTTPI::Request.new("http://example.com")
# HTTPI.delete(request, :httpclient)
#
# === Shortcuts
#
# HTTPI.delete("http://example.com", :curb)
#
# == More control
#
# If you need more control over your request, you can access the HTTP client
# instance represented by your adapter in a block.
#
# HTTPI.get request do |http|
# http.follow_redirect_count = 3 # HTTPClient example
# end
module HTTPI
REQUEST_METHODS = [:get, :post, :head, :put, :delete]
DEFAULT_LOG_LEVEL = :debug
class Error < StandardError; end
class TimeoutError < Error; end
class NotSupportedError < Error; end
class NotImplementedError < Error; end
module ConnectionError; end
class SSLError < Error
def initialize(message = nil, original = $!)
super(message || original.message)
@original = original
end
attr_reader :original
end
class << self
def query_builder
@query_builder ||= HTTPI::QueryBuilder::Flat
end
def query_builder=(builder)
if builder.is_a?(Symbol)
builder_name = builder.to_s.capitalize
begin
builder = HTTPI::QueryBuilder.const_get(builder_name)
rescue NameError
raise ArgumentError, "Invalid builder. Available builders are: [:flat, :nested]"
end
end
unless builder.respond_to?(:build)
raise ArgumentError, "Query builder object should respond to build method"
end
@query_builder = builder
end
# Executes an HTTP GET request.
def get(request, adapter = nil, &block)
request = Request.new(request) if request.kind_of? String
request(:get, request, adapter, &block)
end
# Executes an HTTP POST request.
def post(*args, &block)
request, adapter = request_and_adapter_from(args)
request(:post, request, adapter, &block)
end
# Executes an HTTP HEAD request.
def head(request, adapter = nil, &block)
request = Request.new(request) if request.kind_of? String
request(:head, request, adapter, &block)
end
# Executes an HTTP PUT request.
def put(*args, &block)
request, adapter = request_and_adapter_from(args)
request(:put, request, adapter, &block)
end
# Executes an HTTP DELETE request.
def delete(request, adapter = nil, &block)
request = Request.new(request) if request.kind_of? String
request(:delete, request, adapter, &block)
end
# Executes an HTTP request for the given +method+.
def request(method, request, adapter = nil, redirects = 0)
adapter_class = load_adapter(adapter, request)
Adapter.client_setup_block.call(adapter_class.client) if Adapter.client_setup_block
yield adapter_class.client if block_given?
log_request(method, request, Adapter.identify(adapter_class.class))
response = adapter_class.request(method)
if response && HTTPI::Response::RedirectResponseCodes.member?(response.code) && request.follow_redirect? && redirects < request.redirect_limit
request.url = URI.join(request.url, response.headers['location'])
log("Following redirect: '#{request.url}'.")
return request(method, request, adapter, redirects + 1)
end
response
end
# Shortcut for setting the default adapter to use.
def adapter=(adapter)
Adapter.use = adapter
end
def adapter_client_setup=(block)
Adapter.client_setup_block = block
end
private
def request_and_adapter_from(args)
return args if args[0].kind_of? Request
[Request.new(:url => args[0], :body => args[1]), args[2]]
end
def load_adapter(adapter, request)
Adapter.load(adapter).new(request)
end
end
end