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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_handler, base_uri, path) ⇒ Request

Returns a new instance of Request.



53324
53325
53326
53327
53328
53329
53330
53331
53332
53333
53334
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 53324

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_uriObject (readonly)

Returns the value of attribute base_uri.



53322
53323
53324
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 53322

def base_uri
  @base_uri
end

#full_uriObject (readonly)

Returns the value of attribute full_uri.



53322
53323
53324
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 53322

def full_uri
  @full_uri
end

#pathObject (readonly)

Returns the value of attribute path.



53322
53323
53324
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 53322

def path
  @path
end

Instance Method Details

#delete(&block) ⇒ Object



53382
53383
53384
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 53382

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

#do_request(klass) ⇒ Object



53406
53407
53408
53409
53410
53411
53412
53413
53414
53415
53416
53417
53418
53419
53420
53421
53422
53423
53424
53425
53426
53427
53428
53429
53430
53431
53432
53433
53434
53435
53436
53437
53438
53439
53440
53441
53442
53443
53444
53445
53446
53447
53448
53449
53450
53451
53452
53453
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 53406

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)

  # DEBUG curl = ['curl']
  # DEBUG if klass != Net::HTTP::Get
  # DEBUG  curl << "-X%s" % klass.name.split("::").last.upcase
  # DEBUG end

  if @body
    # DEBUG tmpfile = "/tmp/rest_client.tmp"
    # DEBUG File.open(tmpfile, "w") { |os| os << @body.to_s }
    # DEBUG curl << "-d@%s" % tmpfile
    request.body = @body
  end

  if @auth
    # DEBUG 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|
    # DEBUG curl <<  "-H \"%s: %s\"" % [key, value]
    request.add_field(key, value)
  }

  # DEBUG 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



53378
53379
53380
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 53378

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

#options(&block) ⇒ Object



53386
53387
53388
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 53386

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

#patch(&block) ⇒ Object



53402
53403
53404
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 53402

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

#post(&block) ⇒ Object



53390
53391
53392
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 53390

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

#put(&block) ⇒ Object



53394
53395
53396
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 53394

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

#with_auth(auth) ⇒ Object



53346
53347
53348
53349
53350
53351
53352
53353
53354
53355
53356
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 53346

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



53372
53373
53374
53375
53376
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 53372

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

#with_header(name, value) ⇒ Object



53336
53337
53338
53339
53340
53341
53342
53343
53344
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 53336

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



53367
53368
53369
53370
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 53367

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

#with_query(params) ⇒ Object



53358
53359
53360
53361
53362
53363
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 53358

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