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.



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

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

.current_backend_session=(val) ⇒ Object



206
207
208
# File 'common/jsonmodel_client.rb', line 206

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

.delete_request(url) ⇒ Object



276
277
278
279
280
# File 'common/jsonmodel_client.rb', line 276

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



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'common/jsonmodel_client.rb', line 227

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
192
193
194
195
# 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)
  elsif response.code == '403'
    raise AccessDeniedException.new
  elsif response.code == '404'
    raise RecordNotFound.new
  else
    raise response.body
  end
end

.get_response(url) ⇒ Object



283
284
285
286
287
# File 'common/jsonmodel_client.rb', line 283

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

  do_http_request(url, req)
end

.high_priority?Boolean

Returns:

  • (Boolean)


211
212
213
214
215
216
217
# File 'common/jsonmodel_client.rb', line 211

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

.http_connObject



220
221
222
223
224
# File 'common/jsonmodel_client.rb', line 220

def self.http_conn
  @http ||= Net::HTTP::Persistent.new name: '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



255
256
257
258
259
260
261
# File 'common/jsonmodel_client.rb', line 255

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



264
265
266
267
268
269
270
271
272
273
# File 'common/jsonmodel_client.rb', line 264

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



244
245
246
247
248
249
250
251
252
# File 'common/jsonmodel_client.rb', line 244

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