api_client 0.5.3 → 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/api_client/connection/basic.rb +23 -15
- data/lib/api_client/errors.rb +10 -1
- data/lib/api_client/scope.rb +0 -1
- data/lib/api_client/version.rb +1 -1
- data/spec/api_client/connection/basic_spec.rb +11 -10
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f3a8502965a1402c91399c6abd5f1259374f2548
|
4
|
+
data.tar.gz: 80c0e43636c592037d8e06e30561808612206fff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c3e3cca8a0f0698db139544f118826eb3c44b4161634a303f3a6642ce849d9b2abc908977696a4807793317c531a72ba15606c63689151b1bf7a78cd5c9939f
|
7
|
+
data.tar.gz: 323466b4cf45d62c5f18b232bb9f28d4aa8b236a75287d769050d1bf583b1633d0696e6382da88aeb68cbaf3141e2150ae8dc0bb10105f0da298731d4348ab01
|
@@ -30,7 +30,7 @@ module ApiClient
|
|
30
30
|
# * headers - (optional) headers sent along with the request
|
31
31
|
#
|
32
32
|
def get(path, data = {}, headers = {})
|
33
|
-
|
33
|
+
exec_request(:get, path, data, headers)
|
34
34
|
end
|
35
35
|
|
36
36
|
#### ApiClient::Connection::Abstract#post
|
@@ -43,7 +43,7 @@ module ApiClient
|
|
43
43
|
#
|
44
44
|
# This method automatically adds the application token header
|
45
45
|
def post(path, data = {}, headers = {})
|
46
|
-
|
46
|
+
exec_request(:post, path, data, headers)
|
47
47
|
end
|
48
48
|
|
49
49
|
#### ApiClient::Connection::Abstract#put
|
@@ -56,7 +56,7 @@ module ApiClient
|
|
56
56
|
#
|
57
57
|
# This method automatically adds the application token header
|
58
58
|
def put(path, data = {}, headers = {})
|
59
|
-
|
59
|
+
exec_request(:put, path, data, headers)
|
60
60
|
end
|
61
61
|
|
62
62
|
#### FS::Connection#delete
|
@@ -69,32 +69,40 @@ module ApiClient
|
|
69
69
|
#
|
70
70
|
# This method automatically adds the application token header
|
71
71
|
def delete(path, data = {}, headers = {})
|
72
|
-
|
72
|
+
exec_request(:delete, path, data, headers)
|
73
73
|
end
|
74
74
|
|
75
75
|
private
|
76
76
|
|
77
|
-
def
|
78
|
-
|
77
|
+
def exec_request(method, path, data, headers)
|
78
|
+
response = @handler.send(method, path, data, headers)
|
79
|
+
request = {method: method, path: path, data: data, headers: headers}
|
80
|
+
handle_response(request, response)
|
81
|
+
rescue Faraday::Error::ConnectionFailed => e
|
82
|
+
raise ApiClient::Errors::ConnectionFailed.new(e.message, request, response)
|
83
|
+
end
|
84
|
+
|
85
|
+
def handle_response(request, response)
|
86
|
+
raise ApiClient::Errors::ConnectionFailed.new(request, response) unless response
|
79
87
|
case response.status
|
80
88
|
when 401
|
81
|
-
raise ApiClient::Errors::Unauthorized
|
89
|
+
raise ApiClient::Errors::Unauthorized.new(request, response)
|
82
90
|
when 403
|
83
|
-
raise ApiClient::Errors::Forbidden
|
91
|
+
raise ApiClient::Errors::Forbidden.new(request, response)
|
84
92
|
when 404
|
85
|
-
raise ApiClient::Errors::NotFound
|
93
|
+
raise ApiClient::Errors::NotFound.new(request, response)
|
86
94
|
when 400
|
87
|
-
raise ApiClient::Errors::BadRequest
|
95
|
+
raise ApiClient::Errors::BadRequest.new(request, response)
|
88
96
|
when 406
|
89
|
-
raise ApiClient::Errors::Unsupported
|
97
|
+
raise ApiClient::Errors::Unsupported.new(request, response)
|
90
98
|
when 409
|
91
|
-
raise ApiClient::Errors::Conflict
|
99
|
+
raise ApiClient::Errors::Conflict.new(request, response)
|
92
100
|
when 422
|
93
|
-
raise ApiClient::Errors::UnprocessableEntity.new(response.body)
|
101
|
+
raise ApiClient::Errors::UnprocessableEntity.new(response.body, request, response)
|
94
102
|
when 300..399
|
95
|
-
raise ApiClient::Errors::Redirect.new(response['Location'])
|
103
|
+
raise ApiClient::Errors::Redirect.new(response['Location'], request, response)
|
96
104
|
when 500..599
|
97
|
-
raise ApiClient::Errors::ServerError
|
105
|
+
raise ApiClient::Errors::ServerError.new(request, response)
|
98
106
|
else
|
99
107
|
response
|
100
108
|
end
|
data/lib/api_client/errors.rb
CHANGED
@@ -1,7 +1,16 @@
|
|
1
1
|
module ApiClient
|
2
2
|
|
3
3
|
module Errors
|
4
|
-
class ApiClientError < StandardError
|
4
|
+
class ApiClientError < StandardError
|
5
|
+
def initialize(message=nil, request, response)
|
6
|
+
super(message)
|
7
|
+
@request = request
|
8
|
+
@response = response
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_reader :request, :response
|
12
|
+
end
|
13
|
+
|
5
14
|
class ConnectionFailed < ApiClientError; end
|
6
15
|
class Config < ApiClientError; end
|
7
16
|
class Unauthorized < ApiClientError; end
|
data/lib/api_client/scope.rb
CHANGED
data/lib/api_client/version.rb
CHANGED
@@ -79,6 +79,7 @@ describe ApiClient::Connection::Basic do
|
|
79
79
|
end
|
80
80
|
|
81
81
|
describe "#handle_response" do
|
82
|
+
let(:request) { double }
|
82
83
|
|
83
84
|
before do
|
84
85
|
@instance = ApiClient::Connection::Basic.new("http://google.com")
|
@@ -87,56 +88,56 @@ describe ApiClient::Connection::Basic do
|
|
87
88
|
|
88
89
|
it "raises an ApiClient::Errors::ConnectionFailed if there is no response" do
|
89
90
|
lambda {
|
90
|
-
@instance.send :handle_response, nil
|
91
|
+
@instance.send :handle_response, request, nil
|
91
92
|
}.should raise_error(ApiClient::Errors::ConnectionFailed)
|
92
93
|
end
|
93
94
|
|
94
95
|
it "raises an ApiClient::Errors::Unauthorized if status is 401" do
|
95
96
|
@response.env[:status] = 401
|
96
97
|
lambda {
|
97
|
-
@instance.send :handle_response, @response
|
98
|
+
@instance.send :handle_response, request, @response
|
98
99
|
}.should raise_error(ApiClient::Errors::Unauthorized)
|
99
100
|
end
|
100
101
|
|
101
102
|
it "raises an ApiClient::Errors::Forbidden if status is 403" do
|
102
103
|
@response.env[:status] = 403
|
103
104
|
lambda {
|
104
|
-
@instance.send :handle_response, @response
|
105
|
+
@instance.send :handle_response, request, @response
|
105
106
|
}.should raise_error(ApiClient::Errors::Forbidden)
|
106
107
|
end
|
107
108
|
|
108
109
|
it "raises an ApiClient::Errors::NotFound if status is 404" do
|
109
110
|
@response.env[:status] = 404
|
110
111
|
lambda {
|
111
|
-
@instance.send :handle_response, @response
|
112
|
+
@instance.send :handle_response, request, @response
|
112
113
|
}.should raise_error(ApiClient::Errors::NotFound)
|
113
114
|
end
|
114
115
|
|
115
116
|
it "raises an ApiClient::Errors::BadRequest if status is 400" do
|
116
117
|
@response.env[:status] = 400
|
117
118
|
lambda {
|
118
|
-
@instance.send :handle_response, @response
|
119
|
+
@instance.send :handle_response, request, @response
|
119
120
|
}.should raise_error(ApiClient::Errors::BadRequest)
|
120
121
|
end
|
121
122
|
|
122
123
|
it "raises an ApiClient::Errors::Unsupported if status is 406" do
|
123
124
|
@response.env[:status] = 406
|
124
125
|
lambda {
|
125
|
-
@instance.send :handle_response, @response
|
126
|
+
@instance.send :handle_response, request, @response
|
126
127
|
}.should raise_error(ApiClient::Errors::Unsupported)
|
127
128
|
end
|
128
129
|
|
129
130
|
it "raises an ApiClient::Errors::Conflict if status is 409" do
|
130
131
|
@response.env[:status] = 409
|
131
132
|
lambda {
|
132
|
-
@instance.send :handle_response, @response
|
133
|
+
@instance.send :handle_response, request, @response
|
133
134
|
}.should raise_error(ApiClient::Errors::Conflict)
|
134
135
|
end
|
135
136
|
|
136
137
|
it "raises an ApiClient::Errors::Unsupported if status is 422" do
|
137
138
|
@response.env[:status] = 422
|
138
139
|
lambda {
|
139
|
-
@instance.send :handle_response, @response
|
140
|
+
@instance.send :handle_response, request, @response
|
140
141
|
}.should raise_error(ApiClient::Errors::UnprocessableEntity)
|
141
142
|
end
|
142
143
|
|
@@ -144,14 +145,14 @@ describe ApiClient::Connection::Basic do
|
|
144
145
|
@response.env[:status] = 302
|
145
146
|
@response.env[:response_headers] = { 'Location' => "https://google.com" }
|
146
147
|
lambda {
|
147
|
-
@instance.send :handle_response, @response
|
148
|
+
@instance.send :handle_response, request, @response
|
148
149
|
}.should raise_error(ApiClient::Errors::Redirect)
|
149
150
|
end
|
150
151
|
|
151
152
|
it "raises an ApiClient::Errors::ServerError if status is 500..599" do
|
152
153
|
@response.env[:status] = 502
|
153
154
|
lambda {
|
154
|
-
@instance.send :handle_response, @response
|
155
|
+
@instance.send :handle_response, request, @response
|
155
156
|
}.should raise_error(ApiClient::Errors::ServerError)
|
156
157
|
end
|
157
158
|
|