Class: DynamoDB::Streams::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamodb/streams/client/version.rb,
lib/dynamodb/streams/client.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'0.0.1'
SERVICE_NAME =
'dynamodb'
API_VERSION =
'2012-08-10'
USER_AGENT =
"dynamodb-streams-client/#{DynamoDB::Streams::Client::VERSION}"
DEFAULT_TIMEOUT =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



33
34
35
36
37
38
39
40
41
42
# File 'lib/dynamodb/streams/client.rb', line 33

def initialize(options)
  @accessKeyId = options.fetch(:access_key_id)
  @secretAccessKey = options.fetch(:secret_access_key)
  @endpoint = URI.parse(options.fetch(:endpoint))
  @region = options[:region] || /([^.]+)\.amazonaws\.com\z/.match(@endpoint.host)[1]
  @timeout = DEFAULT_TIMEOUT
  @debug = false
  @retry_num = 3
  @retry_intvl = 10
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



31
32
33
# File 'lib/dynamodb/streams/client.rb', line 31

def debug
  @debug
end

#retry_intvlObject

Returns the value of attribute retry_intvl.



30
31
32
# File 'lib/dynamodb/streams/client.rb', line 30

def retry_intvl
  @retry_intvl
end

#retry_numObject

Returns the value of attribute retry_num.



29
30
31
# File 'lib/dynamodb/streams/client.rb', line 29

def retry_num
  @retry_num
end

#timeoutObject

Returns the value of attribute timeout.



28
29
30
# File 'lib/dynamodb/streams/client.rb', line 28

def timeout
  @timeout
end

Instance Method Details

#query(action, hash = {}) ⇒ Object



44
45
46
47
48
# File 'lib/dynamodb/streams/client.rb', line 44

def query(action, hash = {})
  retry_query do
    query0(action, hash)
  end
end

#query0(action, hash) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/dynamodb/streams/client.rb', line 50

def query0(action, hash)
  req_body = JSON.dump(hash)
  date = Time.now.getutc

  headers = {
    'Content-Type'         => 'application/x-amz-json-1.0',
    'X-Amz-Target'         => "DynamoDBStreams_#{API_VERSION.gsub('-', '')}.#{action}",
    'Content-Length'       => req_body.length.to_s,
    'User-Agent'           => USER_AGENT,
    'Host'                 => @endpoint.host,
    'X-Amz-Date'           => iso8601(date),
    'X-Amz-Content-Sha256' => hexhash(req_body),
    'Accept'               => '*/*',
    'Accept-Encoding'      => 'gzip',
  }

  headers['Authorization'] = authorization(date, headers, req_body)

  Net::HTTP.version_1_2
  http = Net::HTTP.new(@endpoint.host, @endpoint.port)

  if @endpoint.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  if @debug
    http.set_debug_output($stderr)
  end

  http.open_timeout = @timeout
  http.read_timeout = @timeout

  res_code = nil
  res_msg  = nil

  res_body = http.start do |w|
    req = Net::HTTP::Post.new('/', headers)
    req.body = req_body
    res = w.request(req)

    res_code = res.code.to_i
    res_msg  = res.message

    if res['Content-Encoding'] == 'gzip'
      StringIO.open(res.body, 'rb') do |f|
        Zlib::GzipReader.wrap(f).read
      end
    else
      res.body
    end
  end

  res_data = JSON.parse(res_body)
  __type = res_data['__type']

  if res_code != 200 or __type
    errmsg = if __type
               if @debug
                 "#{__type}: #{res_data['message'] || res_data['Message']}"
               else
                 "#{res_data['message'] || res_data['Message']}"
               end
             else
               "#{res_code} #{res_msg}"
             end

    raise DynamoDB::Streams::Client::Error.new(errmsg, res_data)
  end

  res_data
end