Class: Io::Flow::V0::HttpClient::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/flow_commerce/flow_api_v0_client.rb

Defined Under Namespace

Classes: PATCH

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Request

Returns a new instance of Request.



18477
18478
18479
18480
18481
18482
18483
18484
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 18477

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



18539
18540
18541
18542
18543
18544
18545
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 18539

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



18551
18552
18553
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 18551

def delete(&block)
  do_request(Net::HTTP::Delete, &block)
end

#do_request(klass) ⇒ Object



18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 18575

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



18547
18548
18549
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 18547

def get(&block)
  do_request(Net::HTTP::Get, &block)
end

#new_http_clientObject

Creates a new Net:HTTP client. The client returned should be fully configured to make a request.



18530
18531
18532
18533
18534
18535
18536
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 18530

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



18555
18556
18557
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 18555

def options(&block)
  do_request(Net::HTTP::Options, &block)
end

#patch(&block) ⇒ Object



18571
18572
18573
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 18571

def patch(&block)
  do_request(PATCH, &block)
end

#post(&block) ⇒ Object



18559
18560
18561
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 18559

def post(&block)
  do_request(Net::HTTP::Post, &block)
end

#put(&block) ⇒ Object



18563
18564
18565
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 18563

def put(&block)
  do_request(Net::HTTP::Put, &block)
end

#with_auth(auth) ⇒ Object



18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 18496

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



18522
18523
18524
18525
18526
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 18522

def with_body(body)
  Preconditions.check_not_blank('body', body)
  @body = body
  self
end

#with_header(name, value) ⇒ Object



18486
18487
18488
18489
18490
18491
18492
18493
18494
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 18486

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



18517
18518
18519
18520
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 18517

def with_json(json)
  @headers['Content-Type'] ||= 'application/json; charset=UTF-8'
  with_body(json)
end

#with_query(params) ⇒ Object



18508
18509
18510
18511
18512
18513
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 18508

def with_query(params)
  Preconditions.assert_class('params', params, Hash)
  Preconditions.check_state(@params.nil?, "Already have query parameters")
  @params = params
  self
end