Class: Io::Flow::V0::HttpClient::Request
- Inherits:
-
Object
- Object
- Io::Flow::V0::HttpClient::Request
- Defined in:
- lib/flow_commerce/flow_api_v0_client.rb
Defined Under Namespace
Classes: PATCH
Instance Method Summary collapse
-
#configure_ssl(http) ⇒ Object
If HTTPS is required, this method accepts an HTTP Client and configures SSL.
- #delete(&block) ⇒ Object
- #do_request(klass) ⇒ Object
- #get(&block) ⇒ Object
-
#initialize(uri) ⇒ Request
constructor
A new instance of Request.
-
#new_http_client ⇒ Object
Creates a new Net:HTTP client.
- #options(&block) ⇒ Object
- #patch(&block) ⇒ Object
- #post(&block) ⇒ Object
- #put(&block) ⇒ Object
- #with_auth(auth) ⇒ Object
- #with_body(body) ⇒ Object
- #with_header(name, value) ⇒ Object
-
#with_json(json) ⇒ Object
Wrapper to set Content-Type header to application/json and set the provided json document as the body.
- #with_query(params) ⇒ Object
Constructor Details
#initialize(uri) ⇒ Request
Returns a new instance of Request.
15317 15318 15319 15320 15321 15322 15323 15324 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15317 def initialize(uri) @uri = Preconditions.assert_class('uri', uri, URI) @params = nil @body = nil @auth = nil @headers = {} @header_keys_lower_case = [] end |
Instance Method Details
#configure_ssl(http) ⇒ Object
If HTTPS is required, this method accepts an HTTP Client and configures SSL
15379 15380 15381 15382 15383 15384 15385 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15379 def configure_ssl(http) Preconditions.assert_class('http', http, Net::HTTP) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.cert_store = OpenSSL::X509::Store.new http.cert_store.set_default_paths end |
#delete(&block) ⇒ Object
15391 15392 15393 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15391 def delete(&block) do_request(Net::HTTP::Delete, &block) end |
#do_request(klass) ⇒ Object
15415 15416 15417 15418 15419 15420 15421 15422 15423 15424 15425 15426 15427 15428 15429 15430 15431 15432 15433 15434 15435 15436 15437 15438 15439 15440 15441 15442 15443 15444 15445 15446 15447 15448 15449 15450 15451 15452 15453 15454 15455 15456 15457 15458 15459 15460 15461 15462 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15415 def do_request(klass) Preconditions.assert_class('klass', klass, Class) uri = @uri.to_s if q = to_query(@params) uri += "?%s" % q end request = klass.send(:new, uri) curl = ['curl'] if klass != Net::HTTP::Get curl << "-X%s" % klass.name.split("::").last.upcase end if @body # DEBUG path = "/tmp/rest_client.tmp" # DEBUG File.open(path, "w") { |os| os << @body.to_s } # DEBUG curl << "-d@%s" % path request.body = @body end if @auth curl << "-u \"%s:%s\"" % [@auth.username, @auth.password] Preconditions.check_state(!@header_keys_lower_case.include?("authorization"), "Cannot specify both an Authorization header and an auth instance") user_pass = "%s:%s" % [@auth.username, @auth.password] encoded = Base64.encode64(user_pass).to_s.split("\n").map(&:strip).join request.add_field("Authorization", "Basic %s" % encoded) end @headers.each { |key, value| curl << "-H \"%s: %s\"" % [key, value] request.add_field(key, value) } curl << "'%s'" % uri # DEBUG puts curl.join(" ") raw_response = http_request(request) response = raw_response.to_s == "" ? nil : JSON.parse(raw_response) if block_given? yield response else response end end |
#get(&block) ⇒ Object
15387 15388 15389 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15387 def get(&block) do_request(Net::HTTP::Get, &block) end |
#new_http_client ⇒ Object
Creates a new Net:HTTP client. The client returned should be fully configured to make a request.
15370 15371 15372 15373 15374 15375 15376 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15370 def new_http_client client = Net::HTTP.new(@uri.host, @uri.port) if @uri.scheme == "https" configure_ssl(client) end client end |
#options(&block) ⇒ Object
15395 15396 15397 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15395 def (&block) do_request(Net::HTTP::Options, &block) end |
#patch(&block) ⇒ Object
15411 15412 15413 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15411 def patch(&block) do_request(PATCH, &block) end |
#post(&block) ⇒ Object
15399 15400 15401 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15399 def post(&block) do_request(Net::HTTP::Post, &block) end |
#put(&block) ⇒ Object
15403 15404 15405 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15403 def put(&block) do_request(Net::HTTP::Put, &block) end |
#with_auth(auth) ⇒ Object
15336 15337 15338 15339 15340 15341 15342 15343 15344 15345 15346 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15336 def with_auth(auth) Preconditions.assert_class('auth', auth, HttpClient::Authorization) Preconditions.check_state(@auth.nil?, "auth previously set") if auth.scheme.name == AuthScheme::BASIC.name @auth = auth else raise "Auth Scheme[#{auth.scheme.name}] not supported" end self end |
#with_body(body) ⇒ Object
15362 15363 15364 15365 15366 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15362 def with_body(body) Preconditions.check_not_blank('body', body) @body = body self end |
#with_header(name, value) ⇒ Object
15326 15327 15328 15329 15330 15331 15332 15333 15334 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15326 def with_header(name, value) Preconditions.check_not_blank('name', name, "Header name is required") Preconditions.check_not_blank('value', value, "Header value is required") Preconditions.check_state(!@headers.has_key?(name), "Duplicate header named[%s]" % name) @headers[name] = value @header_keys_lower_case << name.downcase self end |
#with_json(json) ⇒ Object
Wrapper to set Content-Type header to application/json and set the provided json document as the body
15357 15358 15359 15360 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15357 def with_json(json) @headers['Content-Type'] ||= 'application/json; charset=UTF-8' with_body(json) end |
#with_query(params) ⇒ Object
15348 15349 15350 15351 15352 15353 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15348 def with_query(params) Preconditions.assert_class('params', params, Hash) Preconditions.check_state(@params.nil?, "Already have query parameters") @params = params self end |