1from moto.core.responses import BaseResponse
2from .models import sdb_backends
3
4
5class SimpleDBResponse(BaseResponse):
6    @property
7    def sdb_backend(self):
8        return sdb_backends[self.region]
9
10    def create_domain(self):
11        domain_name = self._get_param("DomainName")
12        self.sdb_backend.create_domain(domain_name=domain_name,)
13        template = self.response_template(CREATE_DOMAIN_TEMPLATE)
14        return template.render()
15
16    def delete_domain(self):
17        domain_name = self._get_param("DomainName")
18        self.sdb_backend.delete_domain(domain_name=domain_name,)
19        template = self.response_template(DELETE_DOMAIN_TEMPLATE)
20        return template.render()
21
22    def list_domains(self):
23        max_number_of_domains = self._get_int_param("MaxNumberOfDomains")
24        next_token = self._get_param("NextToken")
25        domain_names, next_token = self.sdb_backend.list_domains(
26            max_number_of_domains=max_number_of_domains, next_token=next_token,
27        )
28        template = self.response_template(LIST_DOMAINS_TEMPLATE)
29        return template.render(domain_names=domain_names, next_token=next_token)
30
31    def get_attributes(self):
32        domain_name = self._get_param("DomainName")
33        item_name = self._get_param("ItemName")
34        attribute_names = self._get_multi_param("AttributeName.")
35        consistent_read = self._get_param("ConsistentRead")
36        attributes = self.sdb_backend.get_attributes(
37            domain_name=domain_name,
38            item_name=item_name,
39            attribute_names=attribute_names,
40            consistent_read=consistent_read,
41        )
42        template = self.response_template(GET_ATTRIBUTES_TEMPLATE)
43        return template.render(attributes=attributes)
44
45    def put_attributes(self):
46        domain_name = self._get_param("DomainName")
47        item_name = self._get_param("ItemName")
48        attributes = self._get_list_prefix("Attribute")
49        expected = self._get_param("Expected")
50        self.sdb_backend.put_attributes(
51            domain_name=domain_name,
52            item_name=item_name,
53            attributes=attributes,
54            expected=expected,
55        )
56        template = self.response_template(PUT_ATTRIBUTES_TEMPLATE)
57        return template.render()
58
59
60CREATE_DOMAIN_TEMPLATE = """<?xml version="1.0" encoding="UTF-8"?>
61<CreateDomainResult  xmlns="http://sdb.amazonaws.com/doc/2009-04-15/"></CreateDomainResult>
62"""
63
64
65LIST_DOMAINS_TEMPLATE = """<?xml version="1.0" encoding="UTF-8"?>
66<ListDomainsResponse  xmlns="http://sdb.amazonaws.com/doc/2009-04-15/">
67    <ListDomainsResult>
68        {% for name in domain_names %}
69            <DomainName>{{ name }}</DomainName>
70        {% endfor %}
71        <NextToken>{{ next_token }}</NextToken>
72    </ListDomainsResult>
73</ListDomainsResponse>
74"""
75
76DELETE_DOMAIN_TEMPLATE = """<?xml version="1.0"?>
77<DeleteDomainResponse xmlns="http://sdb.amazonaws.com/doc/2009-04-15/">
78  <ResponseMetadata>
79    <RequestId>64d9c3ac-ef19-2e3d-7a03-9ea46205eb71</RequestId>
80    <BoxUsage>0.0055590278</BoxUsage>
81  </ResponseMetadata>
82</DeleteDomainResponse>"""
83
84PUT_ATTRIBUTES_TEMPLATE = """<?xml version="1.0" encoding="UTF-8"?>
85<PutAttributesResult xmlns="http://sdb.amazonaws.com/doc/2009-04-15/"></PutAttributesResult>
86"""
87
88GET_ATTRIBUTES_TEMPLATE = """<GetAttributesResponse xmlns="http://sdb.amazonaws.com/doc/2009-04-15/">
89  <ResponseMetadata>
90    <RequestId>1549581b-12b7-11e3-895e-1334aEXAMPLE</RequestId>
91  </ResponseMetadata>
92  <GetAttributesResult>
93{% for attribute in attributes %}
94      <Attribute>
95        <Name>{{ attribute["name"] }}</Name>
96        <Value>{{ attribute["value"] }}</Value>
97      </Attribute>
98{% endfor %}
99  </GetAttributesResult>
100</GetAttributesResponse>"""
101