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.
- #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.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/fusionauth/rest_client.rb', line 7 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
22 23 24 25 |
# File 'lib/fusionauth/rest_client.rb', line 22 def () @headers['Authorization'] = self end |
#basic_authorization(username, password) ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/fusionauth/rest_client.rb', line 27 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
44 45 46 47 |
# File 'lib/fusionauth/rest_client.rb', line 44 def body_handler(body_handler) @body_handler = body_handler self end |
#certificate(certificate) ⇒ Object
49 50 51 52 |
# File 'lib/fusionauth/rest_client.rb', line 49 def certificate(certificate) @certificate = certificate self end |
#connect_timeout(connect_timeout) ⇒ Object
54 55 56 57 |
# File 'lib/fusionauth/rest_client.rb', line 54 def connect_timeout(connect_timeout) @connect_timeout = connect_timeout self end |
#delete ⇒ Object
64 65 66 67 |
# File 'lib/fusionauth/rest_client.rb', line 64 def delete @method = 'DELETE' self end |
#error_response_handler(error_response_handler) ⇒ Object
69 70 71 72 |
# File 'lib/fusionauth/rest_client.rb', line 69 def error_response_handler(error_response_handler) @error_response_handler = error_response_handler self end |
#get ⇒ Object
74 75 76 77 |
# File 'lib/fusionauth/rest_client.rb', line 74 def get @method = 'GET' self end |
#go ⇒ Object
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 |
# File 'lib/fusionauth/rest_client.rb', line 79 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.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 request.body = response.request 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
178 179 180 181 |
# File 'lib/fusionauth/rest_client.rb', line 178 def header(name, value) @headers[name] = value self end |
#headers(headers) ⇒ Object
183 184 185 186 |
# File 'lib/fusionauth/rest_client.rb', line 183 def headers(headers) @headers.merge!(headers) self end |
#post ⇒ Object
188 189 190 191 |
# File 'lib/fusionauth/rest_client.rb', line 188 def post @method = 'POST' self end |
#put ⇒ Object
193 194 195 196 |
# File 'lib/fusionauth/rest_client.rb', line 193 def put @method = 'PUT' self end |
#read_timeout(read_timeout) ⇒ Object
59 60 61 62 |
# File 'lib/fusionauth/rest_client.rb', line 59 def read_timeout(read_timeout) @read_timeout = read_timeout self end |
#success_response_handler(success_response_handler) ⇒ Object
198 199 200 201 |
# File 'lib/fusionauth/rest_client.rb', line 198 def success_response_handler(success_response_handler) @success_response_handler = success_response_handler self end |
#uri(uri) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/fusionauth/rest_client.rb', line 203 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
219 220 221 222 |
# File 'lib/fusionauth/rest_client.rb', line 219 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>
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/fusionauth/rest_client.rb', line 235 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
/
265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/fusionauth/rest_client.rb', line 265 def url_segment(value) if value == nil return self end if @url[@url.size - 1] != '/' @url<<'/' end @url<<value self end |