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.



44048
44049
44050
44051
44052
44053
44054
44055
44056
44057
44058
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 44048

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.



44046
44047
44048
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 44046

def base_uri
  @base_uri
end

#full_uriObject (readonly)

Returns the value of attribute full_uri.



44046
44047
44048
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 44046

def full_uri
  @full_uri
end

#pathObject (readonly)

Returns the value of attribute path.



44046
44047
44048
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 44046

def path
  @path
end

Instance Method Details

#delete(&block) ⇒ Object



44106
44107
44108
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 44106

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

#do_request(klass) ⇒ Object



44130
44131
44132
44133
44134
44135
44136
44137
44138
44139
44140
44141
44142
44143
44144
44145
44146
44147
44148
44149
44150
44151
44152
44153
44154
44155
44156
44157
44158
44159
44160
44161
44162
44163
44164
44165
44166
44167
44168
44169
44170
44171
44172
44173
44174
44175
44176
44177
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 44130

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)

  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



44102
44103
44104
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 44102

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

#options(&block) ⇒ Object



44110
44111
44112
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 44110

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

#patch(&block) ⇒ Object



44126
44127
44128
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 44126

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

#post(&block) ⇒ Object



44114
44115
44116
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 44114

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

#put(&block) ⇒ Object



44118
44119
44120
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 44118

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

#with_auth(auth) ⇒ Object



44070
44071
44072
44073
44074
44075
44076
44077
44078
44079
44080
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 44070

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



44096
44097
44098
44099
44100
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 44096

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

#with_header(name, value) ⇒ Object



44060
44061
44062
44063
44064
44065
44066
44067
44068
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 44060

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



44091
44092
44093
44094
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 44091

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

#with_query(params) ⇒ Object



44082
44083
44084
44085
44086
44087
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 44082

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