Skip to content
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.

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

BigQuery: expose 'to_api_repr' method for jobs. #6176

Merged
merged 1 commit into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bigquery/google/cloud/bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ def load_table_from_file(

job_ref = job._JobReference(job_id, project=project, location=location)
load_job = job.LoadJob(job_ref, None, destination, self, job_config)
job_resource = load_job._build_resource()
job_resource = load_job.to_api_repr()

if rewind:
file_obj.seek(0, os.SEEK_SET)
Expand Down
16 changes: 9 additions & 7 deletions bigquery/google/cloud/bigquery/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,12 @@ def _get_resource_config(cls, resource):
'["configuration"]["%s"]' % cls._JOB_TYPE)
return job_id, resource['configuration']

def _build_resource(self):
"""Helper: Generate a resource for :meth:`_begin`."""
def to_api_repr(self):
"""Generate a resource for the job."""
raise NotImplementedError("Abstract")

_build_resource = to_api_repr # backward-compatibility alias

def _begin(self, client=None, retry=DEFAULT_RETRY):
"""API call: begin the job via a POST request

Expand All @@ -549,7 +551,7 @@ def _begin(self, client=None, retry=DEFAULT_RETRY):
# job has an ID.
api_response = client._call_api(
retry,
method='POST', path=path, data=self._build_resource())
method='POST', path=path, data=self.to_api_repr())
self._set_properties(api_response)

def exists(self, client=None, retry=DEFAULT_RETRY):
Expand Down Expand Up @@ -1367,7 +1369,7 @@ def output_rows(self):
if statistics is not None:
return int(statistics['load']['outputRows'])

def _build_resource(self):
def to_api_repr(self):
"""Generate a resource for :meth:`_begin`."""
configuration = self._configuration.to_api_repr()
if self.source_uris is not None:
Expand Down Expand Up @@ -1543,7 +1545,7 @@ def destination_encryption_configuration(self):
"""
return self._configuration.destination_encryption_configuration

def _build_resource(self):
def to_api_repr(self):
"""Generate a resource for :meth:`_begin`."""

source_refs = [{
Expand Down Expand Up @@ -1761,7 +1763,7 @@ def destination_uri_file_counts(self):
return [int(count) for count in counts]
return None

def _build_resource(self):
def to_api_repr(self):
"""Generate a resource for :meth:`_begin`."""

source_ref = {
Expand Down Expand Up @@ -2367,7 +2369,7 @@ def schema_update_options(self):
"""
return self._configuration.schema_update_options

def _build_resource(self):
def to_api_repr(self):
"""Generate a resource for :meth:`_begin`."""
configuration = self._configuration.to_api_repr()

Expand Down
4 changes: 2 additions & 2 deletions bigquery/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2214,7 +2214,7 @@ def _initiate_resumable_upload_helper(self, num_retries=None):
config = LoadJobConfig()
config.source_format = SourceFormat.CSV
job = LoadJob(None, None, self.TABLE_REF, client, job_config=config)
metadata = job._build_resource()
metadata = job.to_api_repr()
upload, transport = client._initiate_resumable_upload(
stream, metadata, num_retries)

Expand Down Expand Up @@ -2279,7 +2279,7 @@ def _do_multipart_upload_success_helper(
config = LoadJobConfig()
config.source_format = SourceFormat.CSV
job = LoadJob(None, None, self.TABLE_REF, client, job_config=config)
metadata = job._build_resource()
metadata = job.to_api_repr()
size = len(data)
response = client._do_multipart_upload(
stream, metadata, size, num_retries)
Expand Down
10 changes: 8 additions & 2 deletions bigquery/tests/unit/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,12 @@ def test__build_resource(self):
with self.assertRaises(NotImplementedError):
job._build_resource()

def test_to_api_repr(self):
client = _make_client(project=self.PROJECT)
job = self._make_one(self.JOB_ID, client)
with self.assertRaises(NotImplementedError):
job.to_api_repr()

def test__begin_already(self):
job = self._set_properties_job()
job._properties['status'] = {'state': 'WHATEVER'}
Expand All @@ -543,7 +549,7 @@ def test__begin_defaults(self):
}
}
job = self._set_properties_job()
builder = job._build_resource = mock.Mock()
builder = job.to_api_repr = mock.Mock()
builder.return_value = resource
call_api = job._client._call_api = mock.Mock()
call_api.return_value = resource
Expand Down Expand Up @@ -573,7 +579,7 @@ def test__begin_explicit(self):
}
}
job = self._set_properties_job()
builder = job._build_resource = mock.Mock()
builder = job.to_api_repr = mock.Mock()
builder.return_value = resource
client = _make_client(project=other_project)
call_api = client._call_api = mock.Mock()
Expand Down