Skip to content

Commit 66a2403

Browse files
committed
Merge pull request #164 from emgerner-msft/callback
request and response callbacks
2 parents 4b231ca + b31ba7b commit 66a2403

File tree

12 files changed

+134
-47
lines changed

12 files changed

+134
-47
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
55
## Version 0.32.0:
66

7+
### All:
8+
- request_callback and response_callback functions may be set on the service clients. These callbacks will be run before the request is executed and after the response is received, respectively. They maybe used to add custom headers to the request and for logging, among other purposes.
9+
710
### Blob:
811
- Get requests taking the start_range parameter incorrectly sent an x-ms-range header when start_range was not specified.
912
- get_blob_to_* will do an initial get request of size 32 MB. If it then finds the blob is larger than this size, it will parallelize by default.

azure/storage/_http/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class HTTPError(Exception):
2222
the status code of the response
2323
:ivar str message:
2424
the message
25+
:ivar list headers:
26+
the returned headers, as a list of (name, value) pairs
2527
:ivar bytes body:
2628
the body of the response
2729
'''

azure/storage/_http/httpclient.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,4 @@ def perform_request(self, request):
131131
for key, name in response.headers.items():
132132
respheaders.append((key.lower(), name))
133133

134-
# Construct an error or a response based on status code
135-
if status >= 300:
136-
# This exception will be caught by the general error handler
137-
# and raised as an azure http exception
138-
raise HTTPError(status, response.reason, respheaders, response.content)
139-
else:
140-
return HTTPResponse(status, response.reason, respheaders, response.content)
134+
return HTTPResponse(status, response.reason, respheaders, response.content)

azure/storage/_serialization.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,9 @@ def _update_request(request):
6363
request.headers.append(('Content-Length', str(len(request.body))))
6464

6565
# append addtional headers based on the service
66-
current_time = format_date_time(time())
67-
request.headers.append(('x-ms-date', current_time))
6866
request.headers.append(('x-ms-version', X_MS_VERSION))
6967
request.headers.append(('User-Agent', _USER_AGENT_STRING))
7068

71-
# append x-ms-meta name, values to header
72-
for name, value in request.headers:
73-
if 'x-ms-meta-name-values' in name and value:
74-
for meta_name, meta_value in value.items():
75-
request.headers.append(('x-ms-meta-' + meta_name, meta_value))
76-
request.headers.remove((name, value))
77-
break
78-
7969
# If the host has a path component (ex local storage), move it
8070
path = request.host.split('/', 1)
8171
if len(path) == 2:
@@ -85,6 +75,17 @@ def _update_request(request):
8575
# Encode and optionally add local storage prefix to path
8676
request.path = url_quote(request.path, '/()$=\',~')
8777

78+
def _add_metadata_headers(metadata, request):
79+
if metadata:
80+
if not request.headers:
81+
request.headers = []
82+
for name, value in metadata.items():
83+
request.headers.append(('x-ms-meta-' + name, value))
84+
85+
def _add_date_header(request):
86+
current_time = format_date_time(time())
87+
request.headers.append(('x-ms-date', current_time))
88+
8889
def _get_request_body_bytes_only(param_name, param_value):
8990
'''Validates the request body passed in and converts it to bytes
9091
if our policy allows it.'''

azure/storage/blob/appendblobservice.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
)
2626
from .._serialization import (
2727
_get_request_body_bytes_only,
28+
_add_metadata_headers,
2829
)
2930
from .._http import HTTPRequest
3031
from ._upload_chunking import (
@@ -172,13 +173,13 @@ def create_blob(self, container_name, blob_name, content_settings=None,
172173
request.query = [('timeout', _int_to_str(timeout))]
173174
request.headers = [
174175
('x-ms-blob-type', _to_str(self.blob_type)),
175-
('x-ms-meta-name-values', metadata),
176176
('x-ms-lease-id', _to_str(lease_id)),
177177
('If-Modified-Since', _datetime_to_utc_string(if_modified_since)),
178178
('If-Unmodified-Since', _datetime_to_utc_string(if_unmodified_since)),
179179
('If-Match', _to_str(if_match)),
180180
('If-None-Match', _to_str(if_none_match))
181181
]
182+
_add_metadata_headers(metadata, request)
182183
if content_settings is not None:
183184
request.headers += content_settings._to_headers()
184185

azure/storage/blob/baseblobservice.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
_get_request_body,
3636
_convert_signed_identifiers_to_xml,
3737
_convert_service_properties_to_xml,
38+
_add_metadata_headers,
3839
)
3940
from .._http import HTTPRequest
4041
from ._download_chunking import _download_blob_chunks
@@ -550,9 +551,9 @@ def create_container(self, container_name, metadata=None,
550551
('restype', 'container'),
551552
('timeout', _int_to_str(timeout)),]
552553
request.headers = [
553-
('x-ms-meta-name-values', metadata),
554554
('x-ms-blob-public-access', _to_str(public_access))
555555
]
556+
_add_metadata_headers(metadata, request)
556557

557558
if not fail_on_exist:
558559
try:
@@ -663,10 +664,10 @@ def set_container_metadata(self, container_name, metadata=None,
663664
('timeout', _int_to_str(timeout)),
664665
]
665666
request.headers = [
666-
('x-ms-meta-name-values', metadata),
667667
('If-Modified-Since', _datetime_to_utc_string(if_modified_since)),
668668
('x-ms-lease-id', _to_str(lease_id)),
669669
]
670+
_add_metadata_headers(metadata, request)
670671

671672
response = self._perform_request(request)
672673
return _parse_base_properties(response)
@@ -2333,13 +2334,13 @@ def set_blob_metadata(self, container_name, blob_name,
23332334
('timeout', _int_to_str(timeout)),
23342335
]
23352336
request.headers = [
2336-
('x-ms-meta-name-values', metadata),
23372337
('If-Modified-Since', _datetime_to_utc_string(if_modified_since)),
23382338
('If-Unmodified-Since', _datetime_to_utc_string(if_unmodified_since)),
23392339
('If-Match', _to_str(if_match)),
23402340
('If-None-Match', _to_str(if_none_match)),
23412341
('x-ms-lease-id', _to_str(lease_id)),
23422342
]
2343+
_add_metadata_headers(metadata, request)
23432344

23442345
response = self._perform_request(request)
23452346
return _parse_base_properties(response)
@@ -2809,13 +2810,13 @@ def snapshot_blob(self, container_name, blob_name,
28092810
('timeout', _int_to_str(timeout)),
28102811
]
28112812
request.headers = [
2812-
('x-ms-meta-name-values', metadata),
28132813
('If-Modified-Since', _datetime_to_utc_string(if_modified_since)),
28142814
('If-Unmodified-Since', _datetime_to_utc_string(if_unmodified_since)),
28152815
('If-Match', _to_str(if_match)),
28162816
('If-None-Match', _to_str(if_none_match)),
28172817
('x-ms-lease-id', _to_str(lease_id))
28182818
]
2819+
_add_metadata_headers(metadata, request)
28192820

28202821
response = self._perform_request(request)
28212822
return _parse_snapshot_blob(blob_name, response)
@@ -2974,7 +2975,6 @@ def copy_blob(self, container_name, blob_name, copy_source,
29742975
request.query = [('timeout', _int_to_str(timeout))]
29752976
request.headers = [
29762977
('x-ms-copy-source', _to_str(copy_source)),
2977-
('x-ms-meta-name-values', metadata),
29782978
('x-ms-source-if-modified-since',
29792979
_to_str(source_if_modified_since)),
29802980
('x-ms-source-if-unmodified-since',
@@ -2989,6 +2989,7 @@ def copy_blob(self, container_name, blob_name, copy_source,
29892989
('x-ms-lease-id', _to_str(destination_lease_id)),
29902990
('x-ms-source-lease-id', _to_str(source_lease_id))
29912991
]
2992+
_add_metadata_headers(metadata, request)
29922993

29932994
response = self._perform_request(request)
29942995
props = _parse_properties(response, BlobProperties)

azure/storage/blob/blockblobservice.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,13 @@ def _put_blob(self, container_name, blob_name, blob, content_settings=None,
191191
request.query = [('timeout', _int_to_str(timeout))]
192192
request.headers = [
193193
('x-ms-blob-type', _to_str(self.blob_type)),
194-
('x-ms-meta-name-values', metadata),
195194
('x-ms-lease-id', _to_str(lease_id)),
196195
('If-Modified-Since', _datetime_to_utc_string(if_modified_since)),
197196
('If-Unmodified-Since', _datetime_to_utc_string(if_unmodified_since)),
198197
('If-Match', _to_str(if_match)),
199198
('If-None-Match', _to_str(if_none_match))
200199
]
200+
_metadata_to_headers(metadata, request)
201201
if content_settings is not None:
202202
request.headers += content_settings._to_headers()
203203
request.body = _get_request_body_bytes_only('blob', blob)
@@ -338,13 +338,13 @@ def put_block_list(
338338
('timeout', _int_to_str(timeout)),
339339
]
340340
request.headers = [
341-
('x-ms-meta-name-values', metadata),
342341
('x-ms-lease-id', _to_str(lease_id)),
343342
('If-Modified-Since', _datetime_to_utc_string(if_modified_since)),
344343
('If-Unmodified-Since', _datetime_to_utc_string(if_unmodified_since)),
345344
('If-Match', _to_str(if_match)),
346345
('If-None-Match', _to_str(if_none_match)),
347346
]
347+
_metadata_to_headers(metadata, request)
348348
if content_settings is not None:
349349
request.headers += content_settings._to_headers()
350350
request.body = _get_request_body(

azure/storage/blob/pageblobservice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ def create_blob(
192192
request.query = [('timeout', _int_to_str(timeout))]
193193
request.headers = [
194194
('x-ms-blob-type', _to_str(self.blob_type)),
195-
('x-ms-meta-name-values', metadata),
196195
('x-ms-blob-content-length', _to_str(content_length)),
197196
('x-ms-lease-id', _to_str(lease_id)),
198197
('x-ms-blob-sequence-number', _to_str(sequence_number)),
@@ -201,6 +200,7 @@ def create_blob(
201200
('If-Match', _to_str(if_match)),
202201
('If-None-Match', _to_str(if_none_match))
203202
]
203+
_metadata_to_headers(metadata, request)
204204
if content_settings is not None:
205205
request.headers += content_settings._to_headers()
206206

azure/storage/file/fileservice.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
_get_request_body_bytes_only,
3535
_convert_signed_identifiers_to_xml,
3636
_convert_service_properties_to_xml,
37+
_add_metadata_headers,
3738
)
3839
from .._deserialization import (
3940
_convert_xml_to_service_properties,
@@ -630,8 +631,8 @@ def create_share(self, share_name, metadata=None, quota=None,
630631
('timeout', _int_to_str(timeout)),
631632
]
632633
request.headers = [
633-
('x-ms-meta-name-values', metadata),
634634
('x-ms-share-quota', _int_to_str(quota))]
635+
_add_metadata_headers(metadata, request)
635636

636637
if not fail_on_exist:
637638
try:
@@ -749,7 +750,7 @@ def set_share_metadata(self, share_name, metadata=None, timeout=None):
749750
('comp', 'metadata'),
750751
('timeout', _int_to_str(timeout)),
751752
]
752-
request.headers = [('x-ms-meta-name-values', metadata)]
753+
_add_metadata_headers(metadata, request)
753754

754755
self._perform_request(request)
755756

@@ -910,7 +911,7 @@ def create_directory(self, share_name, directory_name, metadata=None,
910911
('restype', 'directory'),
911912
('timeout', _int_to_str(timeout)),
912913
]
913-
request.headers = [('x-ms-meta-name-values', metadata)]
914+
_add_metadata_headers(metadata, request)
914915

915916
if not fail_on_exist:
916917
try:
@@ -1056,7 +1057,7 @@ def set_directory_metadata(self, share_name, directory_name, metadata=None, time
10561057
('comp', 'metadata'),
10571058
('timeout', _int_to_str(timeout)),
10581059
]
1059-
request.headers = [('x-ms-meta-name-values', metadata)]
1060+
_add_metadata_headers(metadata, request)
10601061

10611062
self._perform_request(request)
10621063

@@ -1325,7 +1326,7 @@ def set_file_metadata(self, share_name, directory_name,
13251326
('comp', 'metadata'),
13261327
('timeout', _int_to_str(timeout)),
13271328
]
1328-
request.headers = [('x-ms-meta-name-values', metadata)]
1329+
_add_metadata_headers(metadata, request)
13291330

13301331
self._perform_request(request)
13311332

@@ -1378,8 +1379,8 @@ def copy_file(self, share_name, directory_name, file_name, copy_source,
13781379
request.query = [('timeout', _int_to_str(timeout))]
13791380
request.headers = [
13801381
('x-ms-copy-source', _to_str(copy_source)),
1381-
('x-ms-meta-name-values', metadata),
13821382
]
1383+
_add_metadata_headers(metadata, request)
13831384

13841385
response = self._perform_request(request)
13851386
props = _parse_properties(response, FileProperties)
@@ -1479,10 +1480,10 @@ def create_file(self, share_name, directory_name, file_name,
14791480
request.path = _get_path(share_name, directory_name, file_name)
14801481
request.query = [('timeout', _int_to_str(timeout))]
14811482
request.headers = [
1482-
('x-ms-meta-name-values', metadata),
14831483
('x-ms-content-length', _to_str(content_length)),
14841484
('x-ms-type', 'file')
14851485
]
1486+
_add_metadata_headers(metadata, request)
14861487
if content_settings is not None:
14871488
request.headers += content_settings._to_headers()
14881489

azure/storage/queue/queueservice.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
)
3030
from .._serialization import (
3131
_get_request_body,
32+
_add_metadata_headers,
3233
)
3334
from .._common_conversion import (
3435
_int_to_str,
@@ -484,7 +485,8 @@ def create_queue(self, queue_name, metadata=None, fail_on_exist=False, timeout=N
484485
request.host = self._get_host()
485486
request.path = _get_path(queue_name)
486487
request.query = [('timeout', _int_to_str(timeout))]
487-
request.headers = [('x-ms-meta-name-values', metadata)]
488+
_add_metadata_headers(metadata, request)
489+
488490
if not fail_on_exist:
489491
try:
490492
response = self._perform_request(request)
@@ -591,7 +593,8 @@ def set_queue_metadata(self, queue_name, metadata=None, timeout=None):
591593
('comp', 'metadata'),
592594
('timeout', _int_to_str(timeout)),
593595
]
594-
request.headers = [('x-ms-meta-name-values', metadata)]
596+
_add_metadata_headers(metadata, request)
597+
595598
self._perform_request(request)
596599

597600
def exists(self, queue_name, timeout=None):

0 commit comments

Comments
 (0)