GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Handle error responses in 'Table.upload_from_file'. by tseaver · Pull Request #2365 · googleapis/google-cloud-python

Handle error responses in 'Table.upload_from_file'. #2365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
tseaver merged 1 commit into googleapis:master from tseaver:2132-bigquery-check-upload-error-response
Sep 20, 2016

Uh oh!

There was an error while loading. Please reload this page.

Merged

Handle error responses in 'Table.upload_from_file'. #2365

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
3dc975c
Handle error responses in 'Table.upload_from_file'.
tseaver Sep 20, 2016
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

23 changes: 22 additions & 1 deletion 23 google/cloud/bigquery/table.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
import json
import os

import httplib2
import six

from google.cloud._helpers import _datetime_from_microseconds
from google.cloud._helpers import _microseconds_from_datetime
from google.cloud._helpers import _millis_from_datetime
from google.cloud.exceptions import NotFound
from google.cloud.exceptions import make_exception
from google.cloud.streaming.exceptions import HttpError
from google.cloud.streaming.http_wrapper import Request
from google.cloud.streaming.http_wrapper import make_api_request
from google.cloud.streaming.transfer import RESUMABLE_UPLOAD
Expand Down Expand Up @@ -776,6 +779,16 @@ def insert_data(self,

return errors

@staticmethod
Comment thread

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This comment was marked as spam.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

def _check_response_error(request, http_response):
"""Helper for :meth:`upload_from_file`."""
info = http_response.info
status = int(info['status'])
if not 200 <= status < 300:
faux_response = httplib2.Response({'status': status})
raise make_exception(faux_response, http_response.content,
error_info=request.url)

# pylint: disable=too-many-arguments,too-many-locals
def upload_from_file(self,
file_obj,
Expand Down Expand Up @@ -947,13 +960,21 @@ def upload_from_file(self,
request.url = connection.build_api_url(api_base_url=base_url,
path=path,
query_params=query_params)
upload.initialize_upload(request, connection.http)
try:
upload.initialize_upload(request, connection.http)
except HttpError as err_response:
faux_response = httplib2.Response(err_response.response)
raise make_exception(faux_response, err_response.content,
error_info=request.url)

if upload.strategy == RESUMABLE_UPLOAD:
http_response = upload.stream_file(use_chunks=True)
else:
http_response = make_api_request(connection.http, request,
retries=num_retries)

self._check_response_error(request, http_response)

response_content = http_response.content
if not isinstance(response_content,
six.string_types): # pragma: NO COVER Python3
Expand Down
69 changes: 69 additions & 0 deletions 69 unit_tests/bigquery/test_table.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,35 @@ def test_upload_from_file_size_failure(self):
with self.assertRaises(ValueError):
table.upload_from_file(file_obj, 'CSV', size=None)

def test_upload_from_file_multipart_w_400(self):
import csv
import datetime
from six.moves.http_client import BAD_REQUEST
from unit_tests._testing import _NamedTemporaryFile
from google.cloud._helpers import UTC
from google.cloud.exceptions import BadRequest
WHEN_TS = 1437767599.006
WHEN = datetime.datetime.utcfromtimestamp(WHEN_TS).replace(
tzinfo=UTC)
response = {'status': BAD_REQUEST}
conn = _Connection(
(response, b'{}'),
)
client = _Client(project=self.PROJECT, connection=conn)
dataset = _Dataset(client)
table = self._makeOne(self.TABLE_NAME, dataset=dataset)

with _NamedTemporaryFile() as temp:
with open(temp.name, 'w') as file_obj:
writer = csv.writer(file_obj)
writer.writerow(('full_name', 'age', 'joined'))
writer.writerow(('Phred Phlyntstone', 32, WHEN))

with open(temp.name, 'rb') as file_obj:
with self.assertRaises(BadRequest):
table.upload_from_file(
file_obj, 'CSV', rewind=True)

def _upload_from_file_helper(self, **kw):
import csv
import datetime
Expand Down Expand Up @@ -1564,6 +1593,46 @@ def test_upload_from_file_w_bound_client_multipart(self):
payload_lines = app_msg._payload.rstrip().splitlines()
self.assertEqual(payload_lines, body_lines)

def test_upload_from_file_resumable_with_400(self):
import csv
import datetime
from six.moves.http_client import BAD_REQUEST
from google.cloud.bigquery import table as MUT
from google.cloud.exceptions import BadRequest
from google.cloud._helpers import UTC
from unit_tests._testing import _Monkey
from unit_tests._testing import _NamedTemporaryFile
WHEN_TS = 1437767599.006
WHEN = datetime.datetime.utcfromtimestamp(WHEN_TS).replace(
tzinfo=UTC)
initial_response = {'status': BAD_REQUEST}
conn = _Connection(
(initial_response, b'{}'),
)
client = _Client(project=self.PROJECT, connection=conn)

class _UploadConfig(object):
accept = ['*/*']
max_size = None
resumable_multipart = True
resumable_path = u'/upload/bigquery/v2/projects/{project}/jobs'
simple_multipart = True
simple_path = u'' # force resumable
dataset = _Dataset(client)
table = self._makeOne(self.TABLE_NAME, dataset=dataset)

with _Monkey(MUT, _UploadConfig=_UploadConfig):
with _NamedTemporaryFile() as temp:
with open(temp.name, 'w') as file_obj:
writer = csv.writer(file_obj)
writer.writerow(('full_name', 'age', 'joined'))
writer.writerow(('Phred Phlyntstone', 32, WHEN))

with open(temp.name, 'rb') as file_obj:
with self.assertRaises(BadRequest):
table.upload_from_file(
file_obj, 'CSV', rewind=True)

# pylint: disable=too-many-statements
def test_upload_from_file_w_explicit_client_resumable(self):
import json
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.