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.
19825 19826 19827 19828 19829 19830 19831 19832 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 19825 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
19887 19888 19889 19890 19891 19892 19893 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 19887 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
19899 19900 19901 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 19899 def delete(&block) do_request(Net::HTTP::Delete, &block) end |
#do_request(klass) ⇒ Object
19923 19924 19925 19926 19927 19928 19929 19930 19931 19932 19933 19934 19935 19936 19937 19938 19939 19940 19941 19942 19943 19944 19945 19946 19947 19948 19949 19950 19951 19952 19953 19954 19955 19956 19957 19958 19959 19960 19961 19962 19963 19964 19965 19966 19967 19968 19969 19970 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 19923 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
19895 19896 19897 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 19895 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.
19878 19879 19880 19881 19882 19883 19884 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 19878 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
19903 19904 19905 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 19903 def (&block) do_request(Net::HTTP::Options, &block) end |
#patch(&block) ⇒ Object
19919 19920 19921 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 19919 def patch(&block) do_request(PATCH, &block) end |
#post(&block) ⇒ Object
19907 19908 19909 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 19907 def post(&block) do_request(Net::HTTP::Post, &block) end |
#put(&block) ⇒ Object
19911 19912 19913 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 19911 def put(&block) do_request(Net::HTTP::Put, &block) end |
#with_auth(auth) ⇒ Object
19844 19845 19846 19847 19848 19849 19850 19851 19852 19853 19854 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 19844 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
19870 19871 19872 19873 19874 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 19870 def with_body(body) Preconditions.check_not_blank('body', body) @body = body self end |
#with_header(name, value) ⇒ Object
19834 19835 19836 19837 19838 19839 19840 19841 19842 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 19834 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
19865 19866 19867 19868 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 19865 def with_json(json) @headers['Content-Type'] ||= 'application/json; charset=UTF-8' with_body(json) end |
#with_query(params) ⇒ Object
19856 19857 19858 19859 19860 19861 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 19856 def with_query(params) Preconditions.assert_class('params', params, Hash) Preconditions.check_state(@params.nil?, "Already have query parameters") @params = params self end |