rest-client-next 1.1.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +122 -7
- data/Rakefile +4 -4
- data/VERSION +1 -1
- data/history.md +28 -0
- data/lib/restclient.rb +83 -39
- data/lib/restclient/exceptions.rb +114 -82
- data/lib/restclient/mixin/response.rb +56 -40
- data/lib/restclient/net_http_ext.rb +11 -11
- data/lib/restclient/payload.rb +175 -168
- data/lib/restclient/raw_response.rb +22 -22
- data/lib/restclient/request.rb +272 -248
- data/lib/restclient/resource.rb +133 -132
- data/lib/restclient/response.rb +13 -13
- data/spec/exceptions_spec.rb +48 -50
- data/spec/integration_spec.rb +38 -0
- data/spec/mixin/response_spec.rb +38 -38
- data/spec/payload_spec.rb +98 -98
- data/spec/raw_response_spec.rb +11 -11
- data/spec/request_spec.rb +542 -493
- data/spec/resource_spec.rb +95 -71
- data/spec/response_spec.rb +68 -17
- data/spec/restclient_spec.rb +59 -49
- metadata +9 -5
data/lib/restclient/resource.rb
CHANGED
@@ -1,146 +1,147 @@
|
|
1
1
|
module RestClient
|
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
|
-
|
2
|
+
# A class that can be instantiated for access to a RESTful resource,
|
3
|
+
# including authentication.
|
4
|
+
#
|
5
|
+
# Example:
|
6
|
+
#
|
7
|
+
# resource = RestClient::Resource.new('http://some/resource')
|
8
|
+
# jpg = resource.get(:accept => 'image/jpg')
|
9
|
+
#
|
10
|
+
# With HTTP basic authentication:
|
11
|
+
#
|
12
|
+
# resource = RestClient::Resource.new('http://protected/resource', :user => 'user', :password => 'password')
|
13
|
+
# resource.delete
|
14
|
+
#
|
15
|
+
# With a timeout (seconds):
|
16
|
+
#
|
17
|
+
# RestClient::Resource.new('http://slow', :timeout => 10)
|
18
|
+
#
|
19
|
+
# With an open timeout (seconds):
|
20
|
+
#
|
21
|
+
# RestClient::Resource.new('http://behindfirewall', :open_timeout => 10)
|
22
|
+
#
|
23
|
+
# You can also use resources to share common headers. For headers keys,
|
24
|
+
# symbols are converted to strings. Example:
|
25
|
+
#
|
26
|
+
# resource = RestClient::Resource.new('http://some/resource', :headers => { :client_version => 1 })
|
27
|
+
#
|
28
|
+
# This header will be transported as X-Client-Version (notice the X prefix,
|
29
|
+
# capitalization and hyphens)
|
30
|
+
#
|
31
|
+
# Use the [] syntax to allocate subresources:
|
32
|
+
#
|
33
|
+
# site = RestClient::Resource.new('http://example.com', :user => 'adam', :password => 'mypasswd')
|
34
|
+
# site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
|
35
|
+
#
|
36
|
+
class Resource
|
37
|
+
attr_reader :url, :options, :block
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
39
|
+
def initialize(url, options={}, backwards_compatibility=nil, &block)
|
40
|
+
@url = url
|
41
|
+
@block = block
|
42
|
+
if options.class == Hash
|
43
|
+
@options = options
|
44
|
+
else # compatibility with previous versions
|
45
|
+
@options = { :user => options, :password => backwards_compatibility }
|
46
|
+
end
|
47
|
+
end
|
47
48
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
49
|
+
def get(additional_headers={}, &block)
|
50
|
+
headers = (options[:headers] || {}).merge(additional_headers)
|
51
|
+
Request.execute(options.merge(
|
52
|
+
:method => :get,
|
53
|
+
:url => url,
|
54
|
+
:headers => headers), &(block || @block))
|
55
|
+
end
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
57
|
+
def post(payload, additional_headers={}, &block)
|
58
|
+
headers = (options[:headers] || {}).merge(additional_headers)
|
59
|
+
Request.execute(options.merge(
|
60
|
+
:method => :post,
|
61
|
+
:url => url,
|
62
|
+
:payload => payload,
|
63
|
+
:headers => headers), &(block || @block))
|
64
|
+
end
|
64
65
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
66
|
+
def put(payload, additional_headers={}, &block)
|
67
|
+
headers = (options[:headers] || {}).merge(additional_headers)
|
68
|
+
Request.execute(options.merge(
|
69
|
+
:method => :put,
|
70
|
+
:url => url,
|
71
|
+
:payload => payload,
|
72
|
+
:headers => headers), &(block || @block))
|
73
|
+
end
|
73
74
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
75
|
+
def delete(additional_headers={}, &block)
|
76
|
+
headers = (options[:headers] || {}).merge(additional_headers)
|
77
|
+
Request.execute(options.merge(
|
78
|
+
:method => :delete,
|
79
|
+
:url => url,
|
80
|
+
:headers => headers), &(block || @block))
|
81
|
+
end
|
81
82
|
|
82
|
-
|
83
|
-
|
84
|
-
|
83
|
+
def to_s
|
84
|
+
url
|
85
|
+
end
|
85
86
|
|
86
|
-
|
87
|
-
|
88
|
-
|
87
|
+
def user
|
88
|
+
options[:user]
|
89
|
+
end
|
89
90
|
|
90
|
-
|
91
|
-
|
92
|
-
|
91
|
+
def password
|
92
|
+
options[:password]
|
93
|
+
end
|
93
94
|
|
94
|
-
|
95
|
-
|
96
|
-
|
95
|
+
def headers
|
96
|
+
options[:headers] || {}
|
97
|
+
end
|
97
98
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
def open_timeout
|
103
|
-
options[:open_timeout]
|
104
|
-
end
|
99
|
+
def timeout
|
100
|
+
options[:timeout]
|
101
|
+
end
|
105
102
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
#
|
110
|
-
# site = RestClient::Resource.new('http://example.com', 'adam', 'mypasswd')
|
111
|
-
# site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
|
112
|
-
#
|
113
|
-
# This is especially useful if you wish to define your site in one place and
|
114
|
-
# call it in multiple locations:
|
115
|
-
#
|
116
|
-
# def orders
|
117
|
-
# RestClient::Resource.new('http://example.com/orders', 'admin', 'mypasswd')
|
118
|
-
# end
|
119
|
-
#
|
120
|
-
# orders.get # GET http://example.com/orders
|
121
|
-
# orders['1'].get # GET http://example.com/orders/1
|
122
|
-
# orders['1/items'].delete # DELETE http://example.com/orders/1/items
|
123
|
-
#
|
124
|
-
# Nest resources as far as you want:
|
125
|
-
#
|
126
|
-
# site = RestClient::Resource.new('http://example.com')
|
127
|
-
# posts = site['posts']
|
128
|
-
# first_post = posts['1']
|
129
|
-
# comments = first_post['comments']
|
130
|
-
# comments.post 'Hello', :content_type => 'text/plain'
|
131
|
-
#
|
132
|
-
def [](suburl)
|
133
|
-
self.class.new(concat_urls(url, suburl), options)
|
134
|
-
end
|
103
|
+
def open_timeout
|
104
|
+
options[:open_timeout]
|
105
|
+
end
|
135
106
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
107
|
+
# Construct a subresource, preserving authentication.
|
108
|
+
#
|
109
|
+
# Example:
|
110
|
+
#
|
111
|
+
# site = RestClient::Resource.new('http://example.com', 'adam', 'mypasswd')
|
112
|
+
# site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
|
113
|
+
#
|
114
|
+
# This is especially useful if you wish to define your site in one place and
|
115
|
+
# call it in multiple locations:
|
116
|
+
#
|
117
|
+
# def orders
|
118
|
+
# RestClient::Resource.new('http://example.com/orders', 'admin', 'mypasswd')
|
119
|
+
# end
|
120
|
+
#
|
121
|
+
# orders.get # GET http://example.com/orders
|
122
|
+
# orders['1'].get # GET http://example.com/orders/1
|
123
|
+
# orders['1/items'].delete # DELETE http://example.com/orders/1/items
|
124
|
+
#
|
125
|
+
# Nest resources as far as you want:
|
126
|
+
#
|
127
|
+
# site = RestClient::Resource.new('http://example.com')
|
128
|
+
# posts = site['posts']
|
129
|
+
# first_post = posts['1']
|
130
|
+
# comments = first_post['comments']
|
131
|
+
# comments.post 'Hello', :content_type => 'text/plain'
|
132
|
+
#
|
133
|
+
def [](suburl)
|
134
|
+
self.class.new(concat_urls(url, suburl), options)
|
135
|
+
end
|
136
|
+
|
137
|
+
def concat_urls(url, suburl) # :nodoc:
|
138
|
+
url = url.to_s
|
139
|
+
suburl = suburl.to_s
|
140
|
+
if url.slice(-1, 1) == '/' or suburl.slice(0, 1) == '/'
|
141
|
+
url + suburl
|
142
|
+
else
|
143
|
+
"#{url}/#{suburl}"
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
146
147
|
end
|
data/lib/restclient/response.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/mixin/response'
|
2
2
|
|
3
3
|
module RestClient
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
# The response from RestClient looks like a string, but is actually one of
|
5
|
+
# these. 99% of the time you're making a rest call all you care about is
|
6
|
+
# the body, but on the occassion you want to fetch the headers you can:
|
7
|
+
#
|
8
|
+
# RestClient.get('http://example.com').headers[:content_type]
|
9
|
+
#
|
10
|
+
class Response < String
|
11
11
|
|
12
|
-
|
12
|
+
include RestClient::Mixin::Response
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
def initialize(string, net_http_res)
|
15
|
+
@net_http_res = net_http_res
|
16
|
+
super(string || "")
|
17
|
+
end
|
18
18
|
|
19
|
-
|
19
|
+
end
|
20
20
|
end
|
data/spec/exceptions_spec.rb
CHANGED
@@ -1,65 +1,63 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/base'
|
2
2
|
|
3
3
|
describe RestClient::Exception do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
4
|
+
it "sets the exception message to ErrorMessage" do
|
5
|
+
RestClient::ResourceNotFound.new.message.should == 'Resource Not Found'
|
6
|
+
end
|
7
|
+
|
8
|
+
it "contains exceptions in RestClient" do
|
9
|
+
RestClient::Unauthorized.new.should be_a_kind_of(RestClient::Exception)
|
10
|
+
RestClient::ServerBrokeConnection.new.should be_a_kind_of(RestClient::Exception)
|
11
|
+
end
|
12
12
|
end
|
13
13
|
|
14
14
|
describe RestClient::RequestFailed do
|
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
|
-
RestClient::RequestFailed.new(@response).to_s.should match(/502/)
|
40
|
-
end
|
15
|
+
before do
|
16
|
+
@response = mock('HTTP Response', :code => '502')
|
17
|
+
end
|
18
|
+
|
19
|
+
it "stores the http response on the exception" do
|
20
|
+
begin
|
21
|
+
raise RestClient::RequestFailed, :response
|
22
|
+
rescue RestClient::RequestFailed => e
|
23
|
+
e.response.should == :response
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it "http_code convenience method for fetching the code as an integer" do
|
28
|
+
RestClient::RequestFailed.new(@response).http_code.should == 502
|
29
|
+
end
|
30
|
+
|
31
|
+
it "http_body convenience method for fetching the body (decoding when necessary)" do
|
32
|
+
RestClient::RequestFailed.new(@response).http_code.should == 502
|
33
|
+
RestClient::RequestFailed.new(@response).message.should == 'HTTP status code 502'
|
34
|
+
end
|
35
|
+
|
36
|
+
it "shows the status code in the message" do
|
37
|
+
RestClient::RequestFailed.new(@response).to_s.should match(/502/)
|
38
|
+
end
|
41
39
|
end
|
42
40
|
|
43
41
|
describe RestClient::ResourceNotFound do
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
42
|
+
it "also has the http response attached" do
|
43
|
+
begin
|
44
|
+
raise RestClient::ResourceNotFound, :response
|
45
|
+
rescue RestClient::ResourceNotFound => e
|
46
|
+
e.response.should == :response
|
47
|
+
end
|
48
|
+
end
|
51
49
|
end
|
52
50
|
|
53
51
|
describe "backwards compatibility" do
|
54
|
-
|
55
|
-
|
56
|
-
|
52
|
+
it "alias RestClient::Request::Redirect to RestClient::Redirect" do
|
53
|
+
RestClient::Request::Redirect.should == RestClient::Redirect
|
54
|
+
end
|
57
55
|
|
58
|
-
|
59
|
-
|
60
|
-
|
56
|
+
it "alias RestClient::Request::Unauthorized to RestClient::Unauthorized" do
|
57
|
+
RestClient::Request::Unauthorized.should == RestClient::Unauthorized
|
58
|
+
end
|
61
59
|
|
62
|
-
|
63
|
-
|
64
|
-
|
60
|
+
it "alias RestClient::Request::RequestFailed to RestClient::RequestFailed" do
|
61
|
+
RestClient::Request::RequestFailed.should == RestClient::RequestFailed
|
62
|
+
end
|
65
63
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/base'
|
2
|
+
|
3
|
+
require 'webmock/rspec'
|
4
|
+
include WebMock
|
5
|
+
|
6
|
+
describe RestClient do
|
7
|
+
|
8
|
+
it "a simple request" do
|
9
|
+
body = 'abc'
|
10
|
+
stub_request(:get, "www.example.com").to_return(:body => body, :status => 200)
|
11
|
+
response = RestClient.get "www.example.com"
|
12
|
+
response.code.should == 200
|
13
|
+
response.should == body
|
14
|
+
end
|
15
|
+
|
16
|
+
it "a simple request with gzipped content" do
|
17
|
+
stub_request(:get, "www.example.com").with(:headers => { 'Accept-Encoding' => 'gzip, deflate' }).to_return(:body => "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000", :status => 200, :headers => { 'Content-Encoding' => 'gzip' } )
|
18
|
+
response = RestClient.get "www.example.com"
|
19
|
+
response.code.should == 200
|
20
|
+
response.should == "i'm gziped\n"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "a 404" do
|
24
|
+
body = "Ho hai ! I'm not here !"
|
25
|
+
stub_request(:get, "www.example.com").to_return(:body => body, :status => 404)
|
26
|
+
begin
|
27
|
+
RestClient.get "www.example.com"
|
28
|
+
raise
|
29
|
+
rescue RestClient::ResourceNotFound => e
|
30
|
+
e.http_code.should == 404
|
31
|
+
e.response.code.should == 404
|
32
|
+
e.response.should == body
|
33
|
+
e.http_body.should == body
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
end
|