Class: Distelli::ClientBase

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/distelli/clientframework.rb

Instance Method Summary collapse

Methods included from Logging

#logger, logger

Constructor Details

#initialize(credentials, endpoint = nil) ⇒ ClientBase

Returns a new instance of ClientBase.



115
116
117
118
119
120
121
# File 'lib/distelli/clientframework.rb', line 115

def initialize(credentials, endpoint=nil)
  @credentials = credentials
  @endpoint = endpoint
  @json_marshaller = JsonMarshaller.new
  @xml_marshaller = XmlMarshaller.new
  @logger = nil
end

Instance Method Details

#execute(request_info) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/distelli/clientframework.rb', line 135

def execute(request_info)
  if @endpoint == nil
    raise ClientException.new("Invalid endpoint: "+@endpoint.to_s)
  end
  logger.debug("Executing request to endpoint: "+@endpoint.to_s)
  # uri_str = @endpoint
  # if request_info.resource_uri != nil
  #   uri_str = uri_str+request_info.resource_uri
  # end

  request_info.add_header(ServiceConstants::OPERATION_HEADER, request_info.operation)
  request_info.add_header(ServiceConstants::CONTENT_TYPE_HEADER, request_info.content_type)

  if not request_info.contains_header(ServiceConstants::RESPONSE_TYPE_HEADER)
      request_info.add_header(ServiceConstants::RESPONSE_TYPE_HEADER, request_info.response_type)
  end

  request_info.remove_query_params(ServiceConstants::OPERATION_PARAM)
  request_info.remove_query_params(ServiceConstants::RESPONSE_TYPE_PARAM)

  if request_info.request_id == nil
      request_info.request_id = SecureRandom.uuid
  end

  request_info.add_header(ServiceConstants::REQUEST_ID_HEADER, request_info.request_id)
  request_info.remove_query_params(ServiceConstants::REQUEST_ID_PARAM)

  # Set the Date header
  cur_time = Time.now.utc
  request_info.add_header(ServiceConstants::DATE_HEADER, cur_time.strftime("%a, %d %b %Y %H:%M:%S %z"))

  # Create the URI
  uri = URI.parse(request_info.get_uri(@endpoint))

  # TODO: Set the Content-MD5 header

  # Set the authentication headers
  if @credentials != nil
    signer = RequestSigner.new
    signature = signer.get_signature(@credentials.secret_key, request_info)
    request_info.add_header(ServiceConstants::AUTHORIZATION_HEADER, "DWS "+@credentials.key_id+":"+signature)
  end

  response = nil

  http_method = request_info.http_method
  http_client = Net::HTTP.new(uri.host, uri.port)
  http_client.set_debug_output($stdout)
  if http_method == ServiceConstants::HTTP_METHOD_GET
    logger.debug("Executing GET: "+request_info.to_s)
    # Create the http request with the URI, Query Params and Headers
    http_request = Net::HTTP::Get.new(uri.request_uri, request_info.headers)
    # Execute the request to get the response
    response = http_client.request(http_request)
  elsif http_method == ServiceConstants::HTTP_METHOD_POST
    logger.debug("Executing POST: "+request_info.to_s)
    payload = get_payload(request_info)
    # Create the http request with the URI, Query Params and Headers
    http_request = Net::HTTP::Post.new(uri.request_uri, request_info.headers)
    # Set the body
    http_request.body = payload
    # Execute the request to get the response
    response = http_client.request(http_request)
  elsif http_method == ServiceConstants::HTTP_METHOD_PUT
    logger.debug("Executing PUT: "+request_info.to_s)
    payload = get_payload(request_info)
    # Create the http request with the URI, Query Params and Headers
    http_request = Net::HTTP::Put.new(uri.request_uri, request_info.headers)
    # Set the body
    http_request.body= payload
    # Execute the request to get the response
    response = http_client.request(http_request)
  elsif http_method == ServiceConstants::HTTP_METHOD_DELETE
    logger.debug("Executing DELETE: "+request_info.to_s)
    # Create the http request with the URI, Query Params and Headers
    http_request = Net::HTTP::Delete.new(uri.request_uri, request_info.headers)
    # Execute the request to get the response
    response = http_client.request(http_request)
  else
      raise ClientException.new("Invalid HTTP Method: "+http_method)
  end
  return handle_response(response)
end

#set_endpoint(endpoint) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/distelli/clientframework.rb', line 123

def set_endpoint(endpoint)
  if endpoint == nil
    return
  end
  if endpoint.start_with?("http://") or endpoint.start_with?("https://")
    @endpoint = endpoint
  else
    @endpoint = "http://"+endpoint
  end

end