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.
15175 15176 15177 15178 15179 15180 15181 15182 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15175 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
15237 15238 15239 15240 15241 15242 15243 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15237 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
15249 15250 15251 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15249 def delete(&block) do_request(Net::HTTP::Delete, &block) end |
#do_request(klass) ⇒ Object
15273 15274 15275 15276 15277 15278 15279 15280 15281 15282 15283 15284 15285 15286 15287 15288 15289 15290 15291 15292 15293 15294 15295 15296 15297 15298 15299 15300 15301 15302 15303 15304 15305 15306 15307 15308 15309 15310 15311 15312 15313 15314 15315 15316 15317 15318 15319 15320 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15273 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
15245 15246 15247 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15245 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.
15228 15229 15230 15231 15232 15233 15234 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15228 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
15253 15254 15255 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15253 def (&block) do_request(Net::HTTP::Options, &block) end |
#patch(&block) ⇒ Object
15269 15270 15271 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15269 def patch(&block) do_request(PATCH, &block) end |
#post(&block) ⇒ Object
15257 15258 15259 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15257 def post(&block) do_request(Net::HTTP::Post, &block) end |
#put(&block) ⇒ Object
15261 15262 15263 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15261 def put(&block) do_request(Net::HTTP::Put, &block) end |
#with_auth(auth) ⇒ Object
15194 15195 15196 15197 15198 15199 15200 15201 15202 15203 15204 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15194 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
15220 15221 15222 15223 15224 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15220 def with_body(body) Preconditions.check_not_blank('body', body) @body = body self end |
#with_header(name, value) ⇒ Object
15184 15185 15186 15187 15188 15189 15190 15191 15192 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15184 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
15215 15216 15217 15218 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15215 def with_json(json) @headers['Content-Type'] ||= 'application/json; charset=UTF-8' with_body(json) end |
#with_query(params) ⇒ Object
15206 15207 15208 15209 15210 15211 |
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 15206 def with_query(params) Preconditions.assert_class('params', params, Hash) Preconditions.check_state(@params.nil?, "Already have query parameters") @params = params self end |