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.



25877
25878
25879
25880
25881
25882
25883
25884
25885
25886
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 25877

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)
  @params = nil
  @body = nil
  @auth = nil
  @headers = {}
  @header_keys_lower_case = []
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



25875
25876
25877
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 25875

def path
  @path
end

Instance Method Details

#delete(&block) ⇒ Object



25934
25935
25936
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 25934

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

#do_request(klass) ⇒ Object



25958
25959
25960
25961
25962
25963
25964
25965
25966
25967
25968
25969
25970
25971
25972
25973
25974
25975
25976
25977
25978
25979
25980
25981
25982
25983
25984
25985
25986
25987
25988
25989
25990
25991
25992
25993
25994
25995
25996
25997
25998
25999
26000
26001
26002
26003
26004
26005
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 25958

def do_request(klass)
  Preconditions.assert_class('klass', klass, Class)

  uri = path.dup
  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%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



25930
25931
25932
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 25930

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

#options(&block) ⇒ Object



25938
25939
25940
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 25938

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

#patch(&block) ⇒ Object



25954
25955
25956
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 25954

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

#post(&block) ⇒ Object



25942
25943
25944
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 25942

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

#put(&block) ⇒ Object



25946
25947
25948
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 25946

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

#with_auth(auth) ⇒ Object



25898
25899
25900
25901
25902
25903
25904
25905
25906
25907
25908
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 25898

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



25924
25925
25926
25927
25928
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 25924

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

#with_header(name, value) ⇒ Object



25888
25889
25890
25891
25892
25893
25894
25895
25896
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 25888

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



25919
25920
25921
25922
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 25919

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

#with_query(params) ⇒ Object



25910
25911
25912
25913
25914
25915
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 25910

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