1
1
import json
2
2
import re
3
- from email import message_from_bytes
3
+ from office365 . runtime . compat import message_from_bytes_or_string , message_as_bytes_or_string
4
4
from email .message import Message
5
5
6
6
from office365 .runtime .client_request import ClientRequest
@@ -25,7 +25,7 @@ def build_request(self, query):
25
25
media_type = "multipart/mixed"
26
26
content_type = "; " .join ([media_type , "boundary={0}" .format (query .current_boundary )])
27
27
request .ensure_header ('Content-Type' , content_type )
28
- request .data = self ._prepare_payload (query ). as_bytes ()
28
+ request .data = self ._prepare_payload (query )
29
29
return request
30
30
31
31
def process_response (self , response , query ):
@@ -54,7 +54,7 @@ def _read_response(self, response):
54
54
+ response .content
55
55
)
56
56
57
- message = message_from_bytes (http_body ) # type: Message
57
+ message = message_from_bytes_or_string (http_body ) # type: Message
58
58
for raw_response in message .get_payload ():
59
59
if raw_response .get_content_type () == "application/http" :
60
60
yield self ._deserialize_response (raw_response )
@@ -85,7 +85,7 @@ def _prepare_payload(self, query):
85
85
message = self ._serialize_request (request )
86
86
main_message .attach (message )
87
87
88
- return main_message
88
+ return message_as_bytes_or_string ( main_message )
89
89
90
90
@staticmethod
91
91
def _normalize_headers (headers_raw ):
@@ -114,7 +114,9 @@ def _deserialize_response(self, raw_response):
114
114
"content" : None
115
115
}
116
116
else :
117
- * headers_raw , content = lines [1 :]
117
+ #*headers_raw, content = lines[1:]
118
+ headers_raw = lines [1 :- 1 ]
119
+ content = lines [- 1 ]
118
120
content = json .loads (content )
119
121
return {
120
122
"status" : status_info ,
@@ -133,13 +135,13 @@ def _serialize_request(request):
133
135
method = request .method
134
136
if "X-HTTP-Method" in request .headers :
135
137
method = request .headers ["X-HTTP-Method" ]
136
- lines = ["{method} {url} HTTP/1.1" .format (method = method , url = request .url ),
137
- * [':' .join (h ) for h in request .headers .items ()] ]
138
+ lines = ["{method} {url} HTTP/1.1" .format (method = method , url = request .url )] + \
139
+ [':' .join (h ) for h in request .headers .items ()]
138
140
if request .data :
139
141
lines .append (eol )
140
142
lines .append (json .dumps (request .data ))
141
- buffer = eol + eol .join (lines ) + eol
142
- payload = buffer .encode ('utf-8' ).lstrip ()
143
+ raw_content = eol + eol .join (lines ) + eol
144
+ payload = raw_content .encode ('utf-8' ).lstrip ()
143
145
144
146
message = Message ()
145
147
message .add_header ("Content-Type" , "application/http" )
0 commit comments