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.
22954 22955 22956 22957 22958 22959 22960 22961 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 22954 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
23016 23017 23018 23019 23020 23021 23022 23023 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 23016 def configure_ssl(http) Preconditions.assert_class('http', http, Net::HTTP) http.use_ssl = true http.ssl_version = :TLSv1_2 http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.cert_store = OpenSSL::X509::Store.new http.cert_store.set_default_paths end |
#delete(&block) ⇒ Object
23029 23030 23031 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 23029 def delete(&block) do_request(Net::HTTP::Delete, &block) end |
#do_request(klass) ⇒ Object
23053 23054 23055 23056 23057 23058 23059 23060 23061 23062 23063 23064 23065 23066 23067 23068 23069 23070 23071 23072 23073 23074 23075 23076 23077 23078 23079 23080 23081 23082 23083 23084 23085 23086 23087 23088 23089 23090 23091 23092 23093 23094 23095 23096 23097 23098 23099 23100 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 23053 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
23025 23026 23027 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 23025 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.
23007 23008 23009 23010 23011 23012 23013 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 23007 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
23033 23034 23035 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 23033 def (&block) do_request(Net::HTTP::Options, &block) end |
#patch(&block) ⇒ Object
23049 23050 23051 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 23049 def patch(&block) do_request(PATCH, &block) end |
#post(&block) ⇒ Object
23037 23038 23039 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 23037 def post(&block) do_request(Net::HTTP::Post, &block) end |
#put(&block) ⇒ Object
23041 23042 23043 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 23041 def put(&block) do_request(Net::HTTP::Put, &block) end |
#with_auth(auth) ⇒ Object
22973 22974 22975 22976 22977 22978 22979 22980 22981 22982 22983 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 22973 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
22999 23000 23001 23002 23003 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 22999 def with_body(body) Preconditions.check_not_blank('body', body) @body = body self end |
#with_header(name, value) ⇒ Object
22963 22964 22965 22966 22967 22968 22969 22970 22971 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 22963 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
22994 22995 22996 22997 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 22994 def with_json(json) @headers['Content-Type'] ||= 'application/json; charset=UTF-8' with_body(json) end |
#with_query(params) ⇒ Object
22985 22986 22987 22988 22989 22990 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 22985 def with_query(params) Preconditions.assert_class('params', params, Hash) Preconditions.check_state(@params.nil?, "Already have query parameters") @params = params self end |