Module: JSONModel::HTTP

Defined in:
common/jsonmodel_client.rb

Class Method Summary collapse

Class Method Details

.backend_urlObject



105
106
107
108
109
110
111
# File 'common/jsonmodel_client.rb', line 105

def self.backend_url
  if Module.const_defined?(:BACKEND_SERVICE_URL)
    BACKEND_SERVICE_URL
  else
    JSONModel::init_args[:url]
  end
end

.current_backend_sessionObject

Returns the session token to be sent to the backend when making requests.



196
197
198
199
# File 'common/jsonmodel_client.rb', line 196

def self.current_backend_session
  # Set by the ApplicationController
  Thread.current[:backend_session]
end

.current_backend_session=(val) ⇒ Object



202
203
204
# File 'common/jsonmodel_client.rb', line 202

def self.current_backend_session=(val)
  Thread.current[:backend_session] = val
end

.delete_request(url) ⇒ Object



272
273
274
275
276
# File 'common/jsonmodel_client.rb', line 272

def self.delete_request(url)
  req = Net::HTTP::Delete.new(url.request_uri)

  do_http_request(url, req)
end

.do_http_request(url, req, &block) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'common/jsonmodel_client.rb', line 223

def self.do_http_request(url, req, &block)
  req['X-ArchivesSpace-Session'] = current_backend_session

  if high_priority?
    req['X-ArchivesSpace-Priority'] = "high"
  end

  response = http_conn.request(url, req, &block)

  if response.code =~ /^4/
    JSONModel::handle_error(ASUtils.json_parse(response.body))
  end

  response
end

.form_urlencoded(uri, params) ⇒ Object



121
122
123
124
125
# File 'common/jsonmodel_client.rb', line 121

def self.form_urlencoded(uri, params)
  request = Net::HTTP::Post.new(uri)
  request.form_data = params
  request
end

.get_json(uri, params = {}) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'common/jsonmodel_client.rb', line 178

def self.get_json(uri, params = {})
  params = process_params(params)

  uri = URI("#{backend_url}#{uri}")
  uri.query = URI.encode_www_form(params)

  response = get_response(uri)

  if response.is_a?(Net::HTTPSuccess) || response.code == '200'
    ASUtils.json_parse(response.body)
  else
    nil
  end
end

.get_response(url) ⇒ Object



279
280
281
282
283
# File 'common/jsonmodel_client.rb', line 279

def self.get_response(url)
  req = Net::HTTP::Get.new(url.request_uri)

  do_http_request(url, req)
end

.high_priority?Boolean

Returns:

  • (Boolean)


207
208
209
210
211
212
213
# File 'common/jsonmodel_client.rb', line 207

def self.high_priority?
  if Thread.current[:request_priority]
    Thread.current[:request_priority] == :high
  else
    JSONModel::init_args[:priority] == :high
  end
end

.http_connObject



216
217
218
219
220
# File 'common/jsonmodel_client.rb', line 216

def self.http_conn
  @http ||= Net::HTTP::Persistent.new 'jsonmodel_client'
  @http.read_timeout = 1200
  @http
end

.multipart_request(uri, params) ⇒ Object

We override this in the backend’s spec_helper since Rack::Test::Methods doesn’t support multipart requests.



116
117
118
# File 'common/jsonmodel_client.rb', line 116

def self.multipart_request(uri, params)
  Net::HTTP::Post::Multipart.new(uri, params)
end

.post_form(uri, params = {}, encoding = :x_www_form_urlencoded) ⇒ Object

Perform a HTTP POST request against the backend with form parameters

`encoding’ is either :x_www_form_urlencoded or :multipart_form_data. The latter is useful if you’re providing a file upload.



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'common/jsonmodel_client.rb', line 132

def self.post_form(uri, params = {}, encoding = :x_www_form_urlencoded)
  url = URI("#{backend_url}#{uri}")

  req = if encoding == :x_www_form_urlencoded
          self.form_urlencoded(url.request_uri, params)
        elsif encoding == :multipart_form_data
          self.multipart_request(url.request_uri, params)
        else
          raise "Unknown form encoding: #{encoding.inspect}"
        end

  do_http_request(url, req)
end

.post_json(url, json) ⇒ Object



251
252
253
254
255
256
257
# File 'common/jsonmodel_client.rb', line 251

def self.post_json(url, json)
  req = Net::HTTP::Post.new(url.request_uri)
  req['Content-Type'] = 'text/json'
  req.body = json

  do_http_request(url, req)
end

.post_json_file(url, path, &block) ⇒ Object



260
261
262
263
264
265
266
267
268
269
# File 'common/jsonmodel_client.rb', line 260

def self.post_json_file(url, path, &block)
  File.open(path) do |fh|
    req = Net::HTTP::Post.new(url.request_uri)
    req['Content-Type'] = 'text/json'
    req['Content-Length'] = File.size(path)
    req.body_stream = fh

    do_http_request(url, req, &block)
  end
end

.process_params(params) ⇒ Object



147
148
149
# File 'common/jsonmodel_client.rb', line 147

def self.process_params(params)
  params.respond_to?(:to_unsafe_hash) ? params.to_unsafe_hash : params
end

.stream(uri, params = {}, &block) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'common/jsonmodel_client.rb', line 152

def self.stream(uri, params = {}, &block)
  params = process_params(params)
  uri = URI("#{backend_url}#{uri}")
  uri.query = URI.encode_www_form(params)

  req = Net::HTTP::Get.new(uri.request_uri)

  req['X-ArchivesSpace-Session'] = current_backend_session

  if high_priority?
    req['X-ArchivesSpace-Priority'] = "high"
  end

  ASHTTP.start_uri(uri) do |http|
    http.request(req, nil) do |response|
      if response.code =~ /^4/
        JSONModel::handle_error(ASUtils.json_parse(response.body))
        raise response.body
      end

      block.call(response)
    end
  end
end

.with_request_priority(priority) ⇒ Object



240
241
242
243
244
245
246
247
248
# File 'common/jsonmodel_client.rb', line 240

def self.with_request_priority(priority)
  old = Thread.current[:request_priority]
  Thread.current[:request_priority] = priority
  begin
    yield
  ensure
    Thread.current[:request_priority] = old
  end
end