winazurestorageのフォーク
Revision | 0c22ac7e0c1b3b8cb7bd6628492f388782e688ef (tree) |
---|---|
Zeit | 2009-04-15 05:11:37 |
Autor | Steve Marx <Steve.Marx@micr...> |
Commiter | Steve Marx |
adding enqueue and dequeue support
@@ -83,7 +83,7 @@ class SharedKeyCredentials(object): | ||
83 | 83 | if not for_tables: |
84 | 84 | string_to_sign += canonicalized_headers + NEW_LINE # Canonicalized headers |
85 | 85 | string_to_sign += canonicalized_resource # Canonicalized resource |
86 | - | |
86 | + | |
87 | 87 | request.add_header('Authorization', 'SharedKey ' + self._account + ':' + base64.encodestring(hmac.new(self._key, unicode(string_to_sign).encode("utf-8"), hashlib.sha256).digest()).strip()) |
88 | 88 | return request |
89 | 89 |
@@ -126,6 +126,8 @@ class Storage(object): | ||
126 | 126 | |
127 | 127 | class TableEntity(object): pass |
128 | 128 | |
129 | +class QueueMessage(): pass | |
130 | + | |
129 | 131 | class QueueStorage(Storage): |
130 | 132 | def __init__(self, host, account_name, secret_key, use_path_style_uris = None): |
131 | 133 | super(QueueStorage, self).__init__(host, account_name, secret_key, use_path_style_uris) |
@@ -148,6 +150,44 @@ class QueueStorage(Storage): | ||
148 | 150 | return response.code |
149 | 151 | except URLError, e: |
150 | 152 | return e.code |
153 | + | |
154 | + def put_message(self, queue_name, payload): | |
155 | + data = "<QueueMessage><MessageText>%s</MessageText></QueueMessage>" % base64.encodestring(payload) | |
156 | + req = RequestWithMethod("POST", "%s/%s/messages" % (self.get_base_url(), queue_name), data=data) | |
157 | + req.add_header("Content-Type", "application/xml") | |
158 | + req.add_header("Content-Length", len(data)) | |
159 | + self._credentials.sign_request(req) | |
160 | + try: | |
161 | + response = urlopen(req) | |
162 | + return response.code | |
163 | + except URLError, e: | |
164 | + return e.code | |
165 | + | |
166 | + def get_message(self, queue_name): | |
167 | + req = Request("%s/%s/messages" % (self.get_base_url(), queue_name)) | |
168 | + self._credentials.sign_request(req) | |
169 | + response = urlopen(req) | |
170 | + dom = minidom.parseString(response.read()) | |
171 | + messages = dom.getElementsByTagName("QueueMessage") | |
172 | + result = None | |
173 | + if len(messages) == 1: | |
174 | + message = messages[0] | |
175 | + result = QueueMessage() | |
176 | + result.id = message.getElementsByTagName("MessageId")[0].firstChild.data | |
177 | + result.pop_receipt = message.getElementsByTagName("PopReceipt")[0].firstChild.data | |
178 | + result.text = base64.decodestring(message.getElementsByTagName("MessageText")[0].firstChild.data) | |
179 | + return result | |
180 | + | |
181 | + def delete_message(self, queue_name, message): | |
182 | + id = message.id | |
183 | + pop_receipt = message.pop_receipt | |
184 | + req = RequestWithMethod("DELETE", "%s/%s/messages/%s?popreceipt=%s" % (self.get_base_url(), queue_name, id, pop_receipt)) | |
185 | + self._credentials.sign_request(req) | |
186 | + try: | |
187 | + response = urlopen(req) | |
188 | + return response.code | |
189 | + except URLError, e: | |
190 | + return e.code | |
151 | 191 | |
152 | 192 | class TableStorage(Storage): |
153 | 193 | '''Due to local development storage not supporting SharedKey authentication, this class |