Class: FusionAuth::RESTClient
- Inherits:
-
Object
- Object
- FusionAuth::RESTClient
- Defined in:
- lib/fusionauth/rest_client.rb
Instance Method Summary collapse
- #authorization(authorization) ⇒ Object
- #basic_authorization(username, password) ⇒ Object
-
#body_handler(body_handler) ⇒ RESTClient
Sets the BodyHandler for this RESTClient.
- #certificate(certificate) ⇒ Object
- #connect_timeout(connect_timeout) ⇒ Object
- #delete ⇒ Object
- #error_response_handler(error_response_handler) ⇒ Object
- #get ⇒ Object
- #go ⇒ Object
- #header(name, value) ⇒ Object
- #headers(headers) ⇒ Object
-
#initialize ⇒ RESTClient
constructor
A new instance of RESTClient.
- #patch ⇒ Object
- #post ⇒ Object
- #put ⇒ Object
- #read_timeout(read_timeout) ⇒ Object
- #success_response_handler(success_response_handler) ⇒ Object
- #uri(uri) ⇒ Object
- #url(url) ⇒ Object
-
#url_parameter(name, value) ⇒ Object
Add a URL parameter as a key value pair.
-
#url_segment(value) ⇒ Object
Append a url path segment.
Constructor Details
#initialize ⇒ RESTClient
Returns a new instance of RESTClient.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/fusionauth/rest_client.rb', line 21 def initialize @url = ''.dup @parameters = {} @proxy = {} @headers = {} @body_handler = nil @certificate = nil @connect_timeout = 1000 @read_timeout = 2000 @error_response_handler = nil @error_type = nil @method = nil @success_response_handler = nil end |
Instance Method Details
#authorization(authorization) ⇒ Object
36 37 38 39 |
# File 'lib/fusionauth/rest_client.rb', line 36 def () @headers['Authorization'] = self end |
#basic_authorization(username, password) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/fusionauth/rest_client.rb', line 41 def (username, password) if username != nil && password != nil credentials = username + ':' + password encoded = Base64.encode64(credentials) @headers['Authorization'] = "Basic #{encoded}" end self end |
#body_handler(body_handler) ⇒ RESTClient
Sets the BodyHandler for this RESTClient
58 59 60 61 |
# File 'lib/fusionauth/rest_client.rb', line 58 def body_handler(body_handler) @body_handler = body_handler self end |
#certificate(certificate) ⇒ Object
63 64 65 66 |
# File 'lib/fusionauth/rest_client.rb', line 63 def certificate(certificate) @certificate = certificate self end |
#connect_timeout(connect_timeout) ⇒ Object
68 69 70 71 |
# File 'lib/fusionauth/rest_client.rb', line 68 def connect_timeout(connect_timeout) @connect_timeout = connect_timeout self end |
#delete ⇒ Object
78 79 80 81 |
# File 'lib/fusionauth/rest_client.rb', line 78 def delete @method = 'DELETE' self end |
#error_response_handler(error_response_handler) ⇒ Object
83 84 85 86 |
# File 'lib/fusionauth/rest_client.rb', line 83 def error_response_handler(error_response_handler) @error_response_handler = error_response_handler self end |
#get ⇒ Object
88 89 90 91 |
# File 'lib/fusionauth/rest_client.rb', line 88 def get @method = 'GET' self end |
#go ⇒ Object
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 |
# File 'lib/fusionauth/rest_client.rb', line 93 def go if @url.size == 0 raise ArgumentError, 'You must specify a URL' end if @method == nil raise ArgumentError, 'You must specify a HTTP method' end response = ClientResponse.new response.request = (@body_handler != nil) ? @body_handler.body_object : nil response.method = @method begin if @parameters.length > 0 if @url.index('?') == nil @url<<'?' end list = [] @parameters.each { |key, values| values.each { |value| list<<"#{URI.encode_www_form_component(key)}=#{URI.encode_www_form_component(value)}" } } @url<<list.join('&') end response.url = URI(@url) opts = {:p_addr => @proxy[:p_addr], :p_port => @proxy[:p_port], :p_user => @proxy[:p_user], :p_pass => @proxy[:p_pass], :open_timeout => @connect_timeout, :read_timeout => @read_timeout} if @certificate != nil opts[:cert] = @certificate end if response.url.scheme == 'https' opts[:use_ssl] = true end if @body_handler != nil && @body_handler.type != "FormData" @body_handler.set_headers(@headers) end http_response = nil Net::HTTP.start(response.url.hostname, response.url.port, opts) { |http| request = nil if @method == 'COPY' request = Net::HTTP::Copy.new(response.url, @headers) elsif @method == 'DELETE' request = Net::HTTP::Delete.new(response.url, @headers) elsif @method == 'GET' request = Net::HTTP::Get.new(response.url, @headers) elsif @method == 'HEAD' request = Net::HTTP::Head.new(response.url, @headers) elsif @method == 'LOCK' request = Net::HTTP::Lock.new(response.url, @headers) elsif @method == 'MKCOL' request = Net::HTTP::Mkcol.new(response.url, @headers) elsif @method == 'MOVE' request = Net::HTTP::Move.new(response.url, @headers) elsif @method == 'OPTIONS' request = Net::HTTP::Options.new(response.url, @headers) elsif @method == 'PATCH' request = Net::HTTP::Patch.new(response.url, @headers) elsif @method == 'POST' request = Net::HTTP::Post.new(response.url, @headers) elsif @method == 'PROPFIND' request = Net::HTTP::Propfind.new(response.url, @headers) elsif @method == 'PROPPATCH' request = Net::HTTP::Proppatch.new(response.url, @headers) elsif @method == 'PUT' request = Net::HTTP::Put.new(response.url, @headers) elsif @method == 'TRACE' request = Net::HTTP::Trace.new(response.url, @headers) elsif @method == 'UNLOCK' request = Net::HTTP::Unlock.new(response.url, @headers) else raise ArgumentError, "Invalid HTTP method #{@method}" end if @body_handler != nil && @body_handler.type == "FormData" request.set_form_data(response.request) else request.body = response.request end http_response = http.request(request) } response.status = http_response.code.to_i if response.status < 200 || response.status > 299 if http_response.class.body_permitted? && !http_response.body.nil? && http_response.body.size > 0 && @error_response_handler != nil response.error_response = @error_response_handler.call(http_response.body) end elsif http_response.class.body_permitted? && !http_response.body.nil? && http_response.body.size > 0 && @success_response_handler != nil response.success_response = @success_response_handler.call(http_response.body) end rescue Exception => e response.status = -1 response.exception = e # e.backtrace.each {|l| p l} end response end |
#header(name, value) ⇒ Object
197 198 199 200 |
# File 'lib/fusionauth/rest_client.rb', line 197 def header(name, value) @headers[name] = value self end |
#headers(headers) ⇒ Object
202 203 204 205 |
# File 'lib/fusionauth/rest_client.rb', line 202 def headers(headers) @headers.merge!(headers) self end |
#patch ⇒ Object
207 208 209 210 |
# File 'lib/fusionauth/rest_client.rb', line 207 def patch @method = 'PATCH' self end |
#post ⇒ Object
212 213 214 215 |
# File 'lib/fusionauth/rest_client.rb', line 212 def post @method = 'POST' self end |
#put ⇒ Object
217 218 219 220 |
# File 'lib/fusionauth/rest_client.rb', line 217 def put @method = 'PUT' self end |
#read_timeout(read_timeout) ⇒ Object
73 74 75 76 |
# File 'lib/fusionauth/rest_client.rb', line 73 def read_timeout(read_timeout) @read_timeout = read_timeout self end |
#success_response_handler(success_response_handler) ⇒ Object
222 223 224 225 |
# File 'lib/fusionauth/rest_client.rb', line 222 def success_response_handler(success_response_handler) @success_response_handler = success_response_handler self end |
#uri(uri) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/fusionauth/rest_client.rb', line 227 def uri(uri) if @url.size == 0 self end if @url[@url.size - 1] == '/' && uri[0] == '/' @url<<uri[1..uri.size] elsif @url[@url.size - 1] != '/' && uri[0] != '/' @url<<"/#{uri}" else @url<<uri end self end |
#url(url) ⇒ Object
243 244 245 246 |
# File 'lib/fusionauth/rest_client.rb', line 243 def url(url) @url = url.dup self end |
#url_parameter(name, value) ⇒ Object
Add a URL parameter as a key value pair.
@link Collection} a key value pair will be added for each value in the collection.
@link ZonedDateTime} will also be handled uniquely in that the <code>long</ code> will
be used to set in the request using <code>ZonedDateTime.toInstant().toEpochMilli()</code>
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/fusionauth/rest_client.rb', line 259 def url_parameter(name, value) if value == nil return self end if value.is_a? Array @parameters[name] = value else values = @parameters[name] if values == nil values = [] @parameters[name] = values end values<<value end self end |
#url_segment(value) ⇒ Object
Append a url path segment. <p> For Example: <pre>
.url("http://www.foo.com ")
.urlSegment(" bar ")
</pre>
This will result in a url of http://www.foo.com/bar
/
289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/fusionauth/rest_client.rb', line 289 def url_segment(value) if value == nil return self end if @url[@url.size - 1] != '/' @url<<'/' end @url<<value self end |