Fix media upload parameter names. Reviewed in http://codereview.appspot.com/6374062/
diff --git a/apiclient/discovery.py b/apiclient/discovery.py
index b142ff1..84227ae 100644
--- a/apiclient/discovery.py
+++ b/apiclient/discovery.py
@@ -506,7 +506,6 @@
actual_query_params['key'] = self._developerKey
model = self._model
- # If there is no schema for the response then presume a binary blob.
if methodName.endswith('_media'):
model = MediaModel()
elif 'response' not in methodDesc:
diff --git a/apiclient/http.py b/apiclient/http.py
index 4375c4a..1dfe36c 100644
--- a/apiclient/http.py
+++ b/apiclient/http.py
@@ -324,12 +324,12 @@
media_body=media).execute()
"""
- def __init__(self, fh, mimetype, chunksize=DEFAULT_CHUNK_SIZE,
+ def __init__(self, fd, mimetype, chunksize=DEFAULT_CHUNK_SIZE,
resumable=False):
"""Constructor.
Args:
- fh: io.Base or file object, The source of the bytes to upload. MUST be
+ fd: io.Base or file object, The source of the bytes to upload. MUST be
opened in blocking mode, do not use streams opened in non-blocking mode.
mimetype: string, Mime-type of the file. If None then a mime-type will be
guessed from the file extension.
@@ -338,14 +338,14 @@
resumable: bool, True if this is a resumable upload. False means upload
in a single request.
"""
- self._fh = fh
+ self._fd = fd
self._mimetype = mimetype
self._chunksize = chunksize
self._resumable = resumable
self._size = None
try:
- if hasattr(self._fh, 'fileno'):
- fileno = self._fh.fileno()
+ if hasattr(self._fd, 'fileno'):
+ fileno = self._fd.fileno()
# Pipes and such show up as 0 length files.
size = os.fstat(fileno).st_size
@@ -397,8 +397,8 @@
A string of bytes read. May be shorted than length if EOF was reached
first.
"""
- self._fh.seek(begin)
- return self._fh.read(length)
+ self._fd.seek(begin)
+ return self._fd.read(length)
def to_json(self):
"""This upload type is not serializable."""
@@ -518,23 +518,23 @@
print "Download Complete!"
"""
- def __init__(self, fh, request, chunksize=DEFAULT_CHUNK_SIZE):
+ def __init__(self, fd, request, chunksize=DEFAULT_CHUNK_SIZE):
"""Constructor.
Args:
- fh: io.Base or file object, The stream in which to write the downloaded
+ fd: io.Base or file object, The stream in which to write the downloaded
bytes.
request: apiclient.http.HttpRequest, the media request to perform in
chunks.
chunksize: int, File will be downloaded in chunks of this many bytes.
"""
- self.fh_ = fh
- self.request_ = request
- self.uri_ = request.uri
- self.chunksize_ = chunksize
- self.progress_ = 0
- self.total_size_ = None
- self.done_ = False
+ self._fd = fd
+ self._request = request
+ self._uri = request.uri
+ self._chunksize = chunksize
+ self._progress = 0
+ self._total_size = None
+ self._done = False
def next_chunk(self):
"""Get the next chunk of the download.
@@ -550,29 +550,29 @@
"""
headers = {
'range': 'bytes=%d-%d' % (
- self.progress_, self.progress_ + self.chunksize_)
+ self._progress, self._progress + self._chunksize)
}
- http = self.request_.http
+ http = self._request.http
http.follow_redirects = False
- resp, content = http.request(self.uri_, headers=headers)
+ resp, content = http.request(self._uri, headers=headers)
if resp.status in [301, 302, 303, 307, 308] and 'location' in resp:
- self.uri_ = resp['location']
- resp, content = http.request(self.uri_, headers=headers)
+ self._uri = resp['location']
+ resp, content = http.request(self._uri, headers=headers)
if resp.status in [200, 206]:
- self.progress_ += len(content)
- self.fh_.write(content)
+ self._progress += len(content)
+ self._fd.write(content)
if 'content-range' in resp:
content_range = resp['content-range']
length = content_range.rsplit('/', 1)[1]
- self.total_size_ = int(length)
+ self._total_size = int(length)
- if self.progress_ == self.total_size_:
- self.done_ = True
- return MediaDownloadProgress(self.progress_, self.total_size_), self.done_
+ if self._progress == self._total_size:
+ self._done = True
+ return MediaDownloadProgress(self._progress, self._total_size), self._done
else:
- raise HttpError(resp, content, self.uri_)
+ raise HttpError(resp, content, self._uri)
class HttpRequest(object):
diff --git a/tests/test_discovery.py b/tests/test_discovery.py
index 8d8effd..bec272c 100644
--- a/tests/test_discovery.py
+++ b/tests/test_discovery.py
@@ -562,11 +562,11 @@
self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
zoo = build('zoo', 'v1', self.http)
- fh = StringIO.StringIO('data goes here')
+ fd = StringIO.StringIO('data goes here')
# Create an upload that doesn't know the full size of the media.
upload = MediaIoBaseUpload(
- fh=fh, mimetype='image/png', chunksize=10, resumable=True)
+ fd=fd, mimetype='image/png', chunksize=10, resumable=True)
request = zoo.animals().insert(media_body=upload, body=None)
status, body = request.next_chunk(http)
@@ -583,11 +583,11 @@
self.http = HttpMock(datafile('zoo.json'), {'status': '200'})
zoo = build('zoo', 'v1', self.http)
- fh = StringIO.StringIO('data goes here')
+ fd = StringIO.StringIO('data goes here')
# Create an upload that doesn't know the full size of the media.
upload = MediaIoBaseUpload(
- fh=fh, mimetype='image/png', chunksize=15, resumable=True)
+ fd=fd, mimetype='image/png', chunksize=15, resumable=True)
request = zoo.animals().insert(media_body=upload, body=None)
status, body = request.next_chunk(http)
@@ -604,10 +604,10 @@
zoo = build('zoo', 'v1', self.http)
# Create an upload that doesn't know the full size of the media.
- fh = StringIO.StringIO('data goes here')
+ fd = StringIO.StringIO('data goes here')
upload = MediaIoBaseUpload(
- fh=fh, mimetype='image/png', chunksize=500, resumable=True)
+ fd=fd, mimetype='image/png', chunksize=500, resumable=True)
request = zoo.animals().insert(media_body=upload, body=None)
diff --git a/tests/test_http.py b/tests/test_http.py
index 74d84db..3b4e7f8 100644
--- a/tests/test_http.py
+++ b/tests/test_http.py
@@ -192,9 +192,9 @@
try:
import io
- fh = io.FileIO(datafile('small.png'), 'r')
+ fd = io.FileIO(datafile('small.png'), 'r')
upload = MediaIoBaseUpload(
- fh=fh, mimetype='image/png', chunksize=500, resumable=True)
+ fd=fd, mimetype='image/png', chunksize=500, resumable=True)
self.assertEqual('image/png', upload.mimetype())
self.assertEqual(190, upload.size())
self.assertEqual(True, upload.resumable())
@@ -206,7 +206,7 @@
def test_media_io_base_upload_from_file_object(self):
f = open(datafile('small.png'), 'r')
upload = MediaIoBaseUpload(
- fh=f, mimetype='image/png', chunksize=500, resumable=True)
+ fd=f, mimetype='image/png', chunksize=500, resumable=True)
self.assertEqual('image/png', upload.mimetype())
self.assertEqual(190, upload.size())
self.assertEqual(True, upload.resumable())
@@ -216,7 +216,7 @@
def test_media_io_base_upload_serializable(self):
f = open(datafile('small.png'), 'r')
- upload = MediaIoBaseUpload(fh=f, mimetype='image/png')
+ upload = MediaIoBaseUpload(fd=f, mimetype='image/png')
try:
json = upload.to_json()
@@ -226,11 +226,11 @@
def test_media_io_base_upload_from_string_io(self):
f = open(datafile('small.png'), 'r')
- fh = StringIO.StringIO(f.read())
+ fd = StringIO.StringIO(f.read())
f.close()
upload = MediaIoBaseUpload(
- fh=fh, mimetype='image/png', chunksize=500, resumable=True)
+ fd=fd, mimetype='image/png', chunksize=500, resumable=True)
self.assertEqual('image/png', upload.mimetype())
self.assertEqual(None, upload.size())
self.assertEqual(True, upload.resumable())
@@ -243,9 +243,9 @@
import io
f = open(datafile('small.png'), 'r')
- fh = io.BytesIO(f.read())
+ fd = io.BytesIO(f.read())
upload = MediaIoBaseUpload(
- fh=fh, mimetype='image/png', chunksize=500, resumable=True)
+ fd=fd, mimetype='image/png', chunksize=500, resumable=True)
self.assertEqual('image/png', upload.mimetype())
self.assertEqual(None, upload.size())
self.assertEqual(True, upload.resumable())
@@ -261,7 +261,7 @@
http = HttpMock(datafile('zoo.json'), {'status': '200'})
zoo = build('zoo', 'v1', http)
self.request = zoo.animals().get_media(name='Lion')
- self.fh = StringIO.StringIO()
+ self.fd = StringIO.StringIO()
def test_media_io_base_download(self):
self.request.http = HttpMockSequence([
@@ -272,29 +272,29 @@
])
download = MediaIoBaseDownload(
- fh=self.fh, request=self.request, chunksize=3)
+ fd=self.fd, request=self.request, chunksize=3)
- self.assertEqual(self.fh, download.fh_)
- self.assertEqual(3, download.chunksize_)
- self.assertEqual(0, download.progress_)
- self.assertEqual(None, download.total_size_)
- self.assertEqual(False, download.done_)
- self.assertEqual(self.request.uri, download.uri_)
+ self.assertEqual(self.fd, download._fd)
+ self.assertEqual(3, download._chunksize)
+ self.assertEqual(0, download._progress)
+ self.assertEqual(None, download._total_size)
+ self.assertEqual(False, download._done)
+ self.assertEqual(self.request.uri, download._uri)
status, done = download.next_chunk()
- self.assertEqual(self.fh.getvalue(), '123')
+ self.assertEqual(self.fd.getvalue(), '123')
self.assertEqual(False, done)
- self.assertEqual(3, download.progress_)
- self.assertEqual(5, download.total_size_)
+ self.assertEqual(3, download._progress)
+ self.assertEqual(5, download._total_size)
self.assertEqual(3, status.resumable_progress)
status, done = download.next_chunk()
- self.assertEqual(self.fh.getvalue(), '12345')
+ self.assertEqual(self.fd.getvalue(), '12345')
self.assertEqual(True, done)
- self.assertEqual(5, download.progress_)
- self.assertEqual(5, download.total_size_)
+ self.assertEqual(5, download._progress)
+ self.assertEqual(5, download._total_size)
def test_media_io_base_download_handle_redirects(self):
self.request.http = HttpMockSequence([
@@ -305,15 +305,15 @@
])
download = MediaIoBaseDownload(
- fh=self.fh, request=self.request, chunksize=3)
+ fd=self.fd, request=self.request, chunksize=3)
status, done = download.next_chunk()
- self.assertEqual('https://secure.example.net/lion', download.uri_)
- self.assertEqual(self.fh.getvalue(), 'abc')
+ self.assertEqual('https://secure.example.net/lion', download._uri)
+ self.assertEqual(self.fd.getvalue(), 'abc')
self.assertEqual(False, done)
- self.assertEqual(3, download.progress_)
- self.assertEqual(5, download.total_size_)
+ self.assertEqual(3, download._progress)
+ self.assertEqual(5, download._total_size)
def test_media_io_base_download_handle_4xx(self):
self.request.http = HttpMockSequence([
@@ -321,7 +321,7 @@
])
download = MediaIoBaseDownload(
- fh=self.fh, request=self.request, chunksize=3)
+ fd=self.fd, request=self.request, chunksize=3)
try:
status, done = download.next_chunk()
@@ -337,7 +337,7 @@
status, done = download.next_chunk()
- self.assertEqual(self.fh.getvalue(), '123')
+ self.assertEqual(self.fd.getvalue(), '123')
EXPECTED = """POST /someapi/v1/collection/?foo=bar HTTP/1.1
Content-Type: application/json