Module: JSONModel::HTTP
- Defined in:
- common/jsonmodel_client.rb
Class Method Summary (collapse)
-
+ (Object) backend_url
-
+ (Object) current_backend_session
Returns the session token to be sent to the backend when making requests.
-
+ (Object) current_backend_session=(val)
-
+ (Object) delete_request(url)
-
+ (Object) do_http_request(url, req, &block)
-
+ (Object) form_urlencoded(uri, params)
-
+ (Object) get_json(uri, params = {})
-
+ (Object) get_response(url)
-
+ (Boolean) high_priority?
-
+ (Object) http_conn
-
+ (Object) multipart_request(uri, params)
We override this in the backend’s spec_helper since Rack::Test::Methods doesn’t support multipart requests.
-
+ (Object) post_form(uri, params = {}, encoding = :x_www_form_urlencoded)
Perform a HTTP POST request against the backend with form parameters.
-
+ (Object) post_json(url, json)
-
+ (Object) post_json_file(url, path, &block)
-
+ (Object) stream(uri, params = {}, &block)
-
+ (Object) with_request_priority(priority)
Class Method Details
+ (Object) backend_url
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 |
+ (Object) current_backend_session
Returns the session token to be sent to the backend when making requests.
189 190 191 192 |
# File 'common/jsonmodel_client.rb', line 189 def self.current_backend_session # Set by the ApplicationController Thread.current[:backend_session] end |
+ (Object) current_backend_session=(val)
195 196 197 |
# File 'common/jsonmodel_client.rb', line 195 def self.current_backend_session=(val) Thread.current[:backend_session] = val end |
+ (Object) delete_request(url)
265 266 267 268 269 |
# File 'common/jsonmodel_client.rb', line 265 def self.delete_request(url) req = Net::HTTP::Delete.new(url.request_uri) do_http_request(url, req) end |
+ (Object) do_http_request(url, req, &block)
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'common/jsonmodel_client.rb', line 216 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 |
+ (Object) form_urlencoded(uri, params)
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 |
+ (Object) get_json(uri, params = {})
172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'common/jsonmodel_client.rb', line 172 def self.get_json(uri, 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 |
+ (Object) get_response(url)
272 273 274 275 276 |
# File 'common/jsonmodel_client.rb', line 272 def self.get_response(url) req = Net::HTTP::Get.new(url.request_uri) do_http_request(url, req) end |
+ (Boolean) high_priority?
200 201 202 203 204 205 206 |
# File 'common/jsonmodel_client.rb', line 200 def self.high_priority? if Thread.current[:request_priority] Thread.current[:request_priority] == :high else JSONModel::init_args[:priority] == :high end end |
+ (Object) http_conn
209 210 211 212 213 |
# File 'common/jsonmodel_client.rb', line 209 def self.http_conn @http ||= Net::HTTP::Persistent.new 'jsonmodel_client' @http.read_timeout = 1200 @http end |
+ (Object) multipart_request(uri, params)
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 |
+ (Object) post_form(uri, params = {}, encoding = :x_www_form_urlencoded)
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 |
+ (Object) post_json(url, json)
244 245 246 247 248 249 250 |
# File 'common/jsonmodel_client.rb', line 244 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 |
+ (Object) post_json_file(url, path, &block)
253 254 255 256 257 258 259 260 261 262 |
# File 'common/jsonmodel_client.rb', line 253 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 |
+ (Object) stream(uri, params = {}, &block)
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'common/jsonmodel_client.rb', line 147 def self.stream(uri, params = {}, &block) 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 Net::HTTP.start(uri.host, uri.port) 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 |
+ (Object) with_request_priority(priority)
233 234 235 236 237 238 239 240 241 |
# File 'common/jsonmodel_client.rb', line 233 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 |