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 Attribute Summary collapse
-
#base_uri ⇒ Object
readonly
Returns the value of attribute base_uri.
-
#full_uri ⇒ Object
readonly
Returns the value of attribute full_uri.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #delete(&block) ⇒ Object
- #do_request(klass) ⇒ Object
- #get(&block) ⇒ Object
-
#initialize(http_handler, base_uri, path) ⇒ Request
constructor
A new instance of Request.
- #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(http_handler, base_uri, path) ⇒ Request
Returns a new instance of Request.
34614 34615 34616 34617 34618 34619 34620 34621 34622 34623 34624 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 34614 def initialize(http_handler, base_uri, path) @http_handler = http_handler @base_uri = Preconditions.assert_class('base_uri', base_uri, URI) @path = Preconditions.assert_class('path', path, String) @full_uri = @base_uri.to_s + @path @params = nil @body = nil @auth = nil @headers = {} @header_keys_lower_case = [] end |
Instance Attribute Details
#base_uri ⇒ Object (readonly)
Returns the value of attribute base_uri.
34612 34613 34614 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 34612 def base_uri @base_uri end |
#full_uri ⇒ Object (readonly)
Returns the value of attribute full_uri.
34612 34613 34614 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 34612 def full_uri @full_uri end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
34612 34613 34614 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 34612 def path @path end |
Instance Method Details
#delete(&block) ⇒ Object
34672 34673 34674 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 34672 def delete(&block) do_request(Net::HTTP::Delete, &block) end |
#do_request(klass) ⇒ Object
34696 34697 34698 34699 34700 34701 34702 34703 34704 34705 34706 34707 34708 34709 34710 34711 34712 34713 34714 34715 34716 34717 34718 34719 34720 34721 34722 34723 34724 34725 34726 34727 34728 34729 34730 34731 34732 34733 34734 34735 34736 34737 34738 34739 34740 34741 34742 34743 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 34696 def do_request(klass) Preconditions.assert_class('klass', klass, Class) uri = @full_uri.dup 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%s'" % [@base_uri, path] # DEBUG puts curl.join(" ") raw_response = @http_handler.instance(@base_uri, request.path).execute(request) response = raw_response.to_s == "" ? nil : JSON.parse(raw_response) if block_given? yield response else response end end |
#get(&block) ⇒ Object
34668 34669 34670 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 34668 def get(&block) do_request(Net::HTTP::Get, &block) end |
#options(&block) ⇒ Object
34676 34677 34678 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 34676 def (&block) do_request(Net::HTTP::Options, &block) end |
#patch(&block) ⇒ Object
34692 34693 34694 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 34692 def patch(&block) do_request(PATCH, &block) end |
#post(&block) ⇒ Object
34680 34681 34682 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 34680 def post(&block) do_request(Net::HTTP::Post, &block) end |
#put(&block) ⇒ Object
34684 34685 34686 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 34684 def put(&block) do_request(Net::HTTP::Put, &block) end |
#with_auth(auth) ⇒ Object
34636 34637 34638 34639 34640 34641 34642 34643 34644 34645 34646 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 34636 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
34662 34663 34664 34665 34666 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 34662 def with_body(body) Preconditions.check_not_blank('body', body) @body = body self end |
#with_header(name, value) ⇒ Object
34626 34627 34628 34629 34630 34631 34632 34633 34634 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 34626 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
34657 34658 34659 34660 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 34657 def with_json(json) @headers['Content-Type'] ||= 'application/json; charset=UTF-8' with_body(json) end |
#with_query(params) ⇒ Object
34648 34649 34650 34651 34652 34653 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 34648 def with_query(params) Preconditions.assert_class('params', params, Hash) Preconditions.check_state(@params.nil?, "Already have query parameters") @params = params self end |