4242import atom .http_interface
4343import socket
4444import base64
45+ import atom .http_core
4546
4647
4748class ProxyError (atom .http_interface .Error ):
4849 pass
4950
5051
52+ class TestConfigurationError (Exception ):
53+ pass
54+
55+
5156DEFAULT_CONTENT_TYPE = 'application/atom+xml'
5257
5358
5459class HttpClient (atom .http_interface .GenericHttpClient ):
60+ # Added to allow old v1 HttpClient objects to use the new
61+ # http_code.HttpClient. Used in unit tests to inject a mock client.
62+ v2_http_client = None
63+
5564 def __init__ (self , headers = None ):
5665 self .debug = False
5766 self .headers = headers or {}
@@ -79,17 +88,39 @@ def request(self, operation, url, data=None, headers=None):
7988 headers: dict of strings. HTTP headers which should be sent
8089 in the request.
8190 """
91+ all_headers = self .headers .copy ()
92+ if headers :
93+ all_headers .update (headers )
94+
95+ # If the list of headers does not include a Content-Length, attempt to
96+ # calculate it based on the data object.
97+ if data and 'Content-Length' not in all_headers :
98+ if isinstance (data , types .StringTypes ):
99+ all_headers ['Content-Length' ] = len (data )
100+ else :
101+ raise atom .http_interface .ContentLengthRequired ('Unable to calculate '
102+ 'the length of the data parameter. Specify a value for '
103+ 'Content-Length' )
104+
105+ # Set the content type to the default value if none was set.
106+ if 'Content-Type' not in all_headers :
107+ all_headers ['Content-Type' ] = DEFAULT_CONTENT_TYPE
108+
109+ if self .v2_http_client is not None :
110+ http_request = atom .http_core .HttpRequest (method = operation )
111+ atom .http_core .Uri .parse_uri (str (url )).modify_request (http_request )
112+ http_request .headers = all_headers
113+ if data :
114+ http_request ._body_parts .append (data )
115+ return self .v2_http_client .request (http_request = http_request )
116+
82117 if not isinstance (url , atom .url .Url ):
83118 if isinstance (url , types .StringTypes ):
84119 url = atom .url .parse_url (url )
85120 else :
86121 raise atom .http_interface .UnparsableUrlObject ('Unable to parse url '
87122 'parameter because it was not a string or atom.url.Url' )
88123
89- all_headers = self .headers .copy ()
90- if headers :
91- all_headers .update (headers )
92-
93124 connection = self ._prepare_connection (url , all_headers )
94125
95126 if self .debug :
@@ -115,20 +146,6 @@ def request(self, operation, url, data=None, headers=None):
115146 except ValueError : # header_line missing from connection._buffer
116147 pass
117148
118- # If the list of headers does not include a Content-Length, attempt to
119- # calculate it based on the data object.
120- if data and 'Content-Length' not in all_headers :
121- if isinstance (data , types .StringTypes ):
122- all_headers ['Content-Length' ] = len (data )
123- else :
124- raise atom .http_interface .ContentLengthRequired ('Unable to calculate '
125- 'the length of the data parameter. Specify a value for '
126- 'Content-Length' )
127-
128- # Set the content type to the default value if none was set.
129- if 'Content-Type' not in all_headers :
130- all_headers ['Content-Type' ] = DEFAULT_CONTENT_TYPE
131-
132149 # Send the HTTP headers.
133150 for header_name in all_headers :
134151 connection .putheader (header_name , all_headers [header_name ])
0 commit comments