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.



17102
17103
17104
17105
17106
17107
17108
17109
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 17102

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



17164
17165
17166
17167
17168
17169
17170
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 17164

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



17176
17177
17178
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 17176

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

#do_request(klass) ⇒ Object



17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 17200

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



17172
17173
17174
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 17172

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.



17155
17156
17157
17158
17159
17160
17161
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 17155

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



17180
17181
17182
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 17180

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

#patch(&block) ⇒ Object



17196
17197
17198
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 17196

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

#post(&block) ⇒ Object



17184
17185
17186
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 17184

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

#put(&block) ⇒ Object



17188
17189
17190
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 17188

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

#with_auth(auth) ⇒ Object



17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 17121

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



17147
17148
17149
17150
17151
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 17147

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

#with_header(name, value) ⇒ Object



17111
17112
17113
17114
17115
17116
17117
17118
17119
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 17111

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



17142
17143
17144
17145
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 17142

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

#with_query(params) ⇒ Object



17133
17134
17135
17136
17137
17138
# File 'lib/flow_commerce/flow_api_v0_client.rb', line 17133

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