Class: FusionAuth::RESTClient

Inherits:
Object
  • Object
show all
Defined in:
lib/fusionauth/rest_client.rb

Instance Method Summary collapse

Constructor Details

#initializeRESTClient

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 authorization(authorization)
  @headers['Authorization'] = 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 basic_authorization(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

Parameters:

  • body_handler (BodyHandler)

    The body handler.

Returns:



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

#deleteObject



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

#getObject



88
89
90
91
# File 'lib/fusionauth/rest_client.rb', line 88

def get
  @method = 'GET'
  self
end

#goObject



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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# 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

    # Temporary hack. In version 1.43.0 of FusionAuth, we will no longer validate the Content-Type when there does not appear to be a payload.
    if @body_handler == nil && (@method == 'POST' || @method == "PUT")
      hasContentType = false
      @headers.each_key do |k|
        if k.casecmp("Content-Type") == 0
          hasContentType = true
          break
        end
      end

      unless hasContentType
        @headers['Content-Type'] = 'application/json'
      end
    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



212
213
214
215
# File 'lib/fusionauth/rest_client.rb', line 212

def header(name, value)
  @headers[name] = value
  self
end

#headers(headers) ⇒ Object



217
218
219
220
# File 'lib/fusionauth/rest_client.rb', line 217

def headers(headers)
  @headers.merge!(headers)
  self
end

#patchObject



222
223
224
225
# File 'lib/fusionauth/rest_client.rb', line 222

def patch
  @method = 'PATCH'
  self
end

#postObject



227
228
229
230
# File 'lib/fusionauth/rest_client.rb', line 227

def post
  @method = 'POST'
  self
end

#putObject



232
233
234
235
# File 'lib/fusionauth/rest_client.rb', line 232

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



237
238
239
240
# File 'lib/fusionauth/rest_client.rb', line 237

def success_response_handler(success_response_handler)
  @success_response_handler = success_response_handler
  self
end

#uri(uri) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/fusionauth/rest_client.rb', line 242

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



258
259
260
261
# File 'lib/fusionauth/rest_client.rb', line 258

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>

Parameters:

  • name (String)

    The URL parameter name.

  • value (String)

    ]The url parameter value. The .toString()</ code> method will be used to get the <code>String used in the URL parameter. If the object type is a

Returns:

  • This.



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/fusionauth/rest_client.rb', line 274

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

/

Parameters:

  • value

    The url path segment. A nil value will be ignored.

Returns:

  • This.



304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/fusionauth/rest_client.rb', line 304

def url_segment(value)
  if value == nil
    return self
  end

  if @url[@url.size - 1] != '/'
    @url<<'/'
  end

  @url<<value
  self
end