1# -*- coding: utf-8 -*-
2# Copyright (c) 2012 Thomas Parslow http://almostobsolete.net/
3#
4# Permission is hereby granted, free of charge, to any person obtaining a
5# copy of this software and associated documentation files (the
6# "Software"), to deal in the Software without restriction, including
7# without limitation the rights to use, copy, modify, merge, publish, dis-
8# tribute, sublicense, and/or sell copies of the Software, and to permit
9# persons to whom the Software is furnished to do so, subject to the fol-
10# lowing conditions:
11#
12# The above copyright notice and this permission notice shall be included
13# in all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
18# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21# IN THE SOFTWARE.
22#
23from boto.compat import json
24
25
26class GlacierResponse(dict):
27    """
28    Represents a response from Glacier layer1. It acts as a dictionary
29    containing the combined keys received via JSON in the body (if
30    supplied) and headers.
31    """
32    def __init__(self, http_response, response_headers):
33        self.http_response = http_response
34        self.status = http_response.status
35        self[u'RequestId'] = http_response.getheader('x-amzn-requestid')
36        if response_headers:
37            for header_name, item_name in response_headers:
38                self[item_name] = http_response.getheader(header_name)
39        if http_response.status != 204:
40            if http_response.getheader('Content-Type') == 'application/json':
41                body = json.loads(http_response.read().decode('utf-8'))
42                self.update(body)
43        size = http_response.getheader('Content-Length', None)
44        if size is not None:
45            self.size = size
46
47    def read(self, amt=None):
48        "Reads and returns the response body, or up to the next amt bytes."
49        return self.http_response.read(amt)
50