Class: DynamoDB::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ddbcli/ddb-client.rb

Constant Summary collapse

SERVICE_NAME =
'dynamodb'
API_VERSION =
'2012-08-10'
USER_AGENT =
"ddbcli/#{DynamoDB::VERSION}"
DEFAULT_TIMEOUT =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(accessKeyId, secretAccessKey, endpoint_or_region) ⇒ Client

Returns a new instance of Client.



28
29
30
31
32
33
34
35
36
# File 'lib/ddbcli/ddb-client.rb', line 28

def initialize(accessKeyId, secretAccessKey, endpoint_or_region)
  @accessKeyId = accessKeyId
  @secretAccessKey = secretAccessKey
  set_endpoint_and_region(endpoint_or_region)
  @timeout = DEFAULT_TIMEOUT
  @debug = false
  @retry_num = 3
  @retry_intvl = 10
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



26
27
28
# File 'lib/ddbcli/ddb-client.rb', line 26

def debug
  @debug
end

#regionObject (readonly)

Returns the value of attribute region.



22
23
24
# File 'lib/ddbcli/ddb-client.rb', line 22

def region
  @region
end

#retry_intvlObject

Returns the value of attribute retry_intvl.



25
26
27
# File 'lib/ddbcli/ddb-client.rb', line 25

def retry_intvl
  @retry_intvl
end

#retry_numObject

Returns the value of attribute retry_num.



24
25
26
# File 'lib/ddbcli/ddb-client.rb', line 24

def retry_num
  @retry_num
end

#timeoutObject

Returns the value of attribute timeout.



23
24
25
# File 'lib/ddbcli/ddb-client.rb', line 23

def timeout
  @timeout
end

Instance Method Details

#query(action, hash) ⇒ Object



57
58
59
60
61
# File 'lib/ddbcli/ddb-client.rb', line 57

def query(action, hash)
  retry_query do
    query0(action, hash)
  end
end

#query0(action, hash) ⇒ Object



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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ddbcli/ddb-client.rb', line 63

def query0(action, hash)
  if @debug
    $stderr.puts(<<EOS)
---request begin---
Action: #{action}
#{JSON.pretty_generate(hash)}
---request end---
EOS
  end

  req_body = JSON.dump(hash)
  date = Time.now.getutc

  headers = {
    'Content-Type'         => 'application/x-amz-json-1.0',
    'X-Amz-Target'         => "DynamoDB_#{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

  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)

  if @debug
    $stderr.puts(<<EOS)
---response begin---
#{JSON.pretty_generate(res_data)}
---response end---
EOS
  end

  __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::Error.new(errmsg, res_data)
  end

  res_data
end

#set_endpoint_and_region(endpoint_or_region) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ddbcli/ddb-client.rb', line 38

def set_endpoint_and_region(endpoint_or_region)
  if endpoint_or_region.kind_of?(URI)
    @endpoint = endpoint_or_region

    aws_endpoint, region = DynamoDB::Endpoint::ENDPOINTS.find do |k, v|
      @endpoint.host[k]
    end

    if region
      @region = region
    else
      @region = [@endpoint.host, @endpoint.port].join(':')
    end
  else
    host, @region = DynamoDB::Endpoint.endpoint_and_region(endpoint_or_region)
    @endpoint = URI.parse("https://#{host}")
  end
end