We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
ConnectionError in Client.insert_rows_json()
We have a http Cloud Function that does some data processing and then streams to BQ. The function errors out sometimes because of either the bq client losing connection or it is the insert_rows that can't connect. 
See below an example of a stack trace captured in the GCP logs.


```
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/layers/google.python.functions-framework/functions-framework/lib/python3.8/site-packages/functions_framework/__init__.py", line 66, in view_func
    return function(request._get_current_object())
  File "/workspace/main.py", line 162, in stream_tax
    errors = bq.insert_rows_json(table=dataset_table,
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/bigquery/client.py", line 3013, in insert_rows_json
    response = self._call_api(
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/bigquery/client.py", line 636, in _call_api
    return call()
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/api_core/retry.py", line 281, in retry_wrapped_func
    return retry_target(
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/api_core/retry.py", line 184, in retry_target
    return target()
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/_http.py", line 427, in api_request
    response = self._make_request(
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/_http.py", line 291, in _make_request
    return self._do_request(
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/_http.py", line 329, in _do_request
    return self.http.request(
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/auth/transport/requests.py", line 464, in request
    response = super(AuthorizedSession, self).request(
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/requests/adapters.py", line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
```


- `bq` (=`bigquery.Client()`) in the trace is instantiated as a global variable as recommended here: https://cloud.google.com/functions/docs/bestpractices/networking#accessing_google_apis

- error is logged 30 secs after function is invoked - so can't be the 60s default timeout in `-http`

Thoughts ?

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `setup.py`
Content:
```
1 # Copyright 2018 Google LLC
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 import io
16 import os
17 
18 import setuptools
19 
20 
21 # Package metadata.
22 
23 name = "google-cloud-bigquery"
24 description = "Google BigQuery API client library"
25 
26 # Should be one of:
27 # 'Development Status :: 3 - Alpha'
28 # 'Development Status :: 4 - Beta'
29 # 'Development Status :: 5 - Production/Stable'
30 release_status = "Development Status :: 5 - Production/Stable"
31 dependencies = [
32     "google-api-core[grpc] >= 1.23.0, < 2.0.0dev",
33     "proto-plus >= 1.10.0",
34     "google-cloud-core >= 1.4.1, < 2.0dev",
35     "google-resumable-media >= 0.6.0, < 2.0dev",
36     "packaging >= 14.3",
37     "protobuf >= 3.12.0",
38 ]
39 extras = {
40     "bqstorage": [
41         "google-cloud-bigquery-storage >= 2.0.0, <3.0.0dev",
42         # Due to an issue in pip's dependency resolver, the `grpc` extra is not
43         # installed, even though `google-cloud-bigquery-storage` specifies it
44         # as `google-api-core[grpc]`. We thus need to explicitly specify it here.
45         # See: https://github.com/googleapis/python-bigquery/issues/83 The
46         # grpc.Channel.close() method isn't added until 1.32.0.
47         # https://github.com/grpc/grpc/pull/15254
48         "grpcio >= 1.32.0, < 2.0dev",
49         "pyarrow >= 1.0.0, < 4.0dev",
50     ],
51     "pandas": ["pandas>=0.23.0", "pyarrow >= 1.0.0, < 4.0dev"],
52     "bignumeric_type": ["pyarrow >= 3.0.0, < 4.0dev"],
53     "tqdm": ["tqdm >= 4.7.4, <5.0.0dev"],
54     "opentelemetry": [
55         "opentelemetry-api==0.11b0",
56         "opentelemetry-sdk==0.11b0",
57         "opentelemetry-instrumentation==0.11b0",
58     ],
59 }
60 
61 all_extras = []
62 
63 for extra in extras:
64     # Exclude this extra from all to avoid overly strict dependencies on core
65     # libraries such as pyarrow.
66     # https://github.com/googleapis/python-bigquery/issues/563
67     if extra in {"bignumeric_type"}:
68         continue
69     all_extras.extend(extras[extra])
70 
71 extras["all"] = all_extras
72 
73 # Setup boilerplate below this line.
74 
75 package_root = os.path.abspath(os.path.dirname(__file__))
76 
77 readme_filename = os.path.join(package_root, "README.rst")
78 with io.open(readme_filename, encoding="utf-8") as readme_file:
79     readme = readme_file.read()
80 
81 version = {}
82 with open(os.path.join(package_root, "google/cloud/bigquery/version.py")) as fp:
83     exec(fp.read(), version)
84 version = version["__version__"]
85 
86 # Only include packages under the 'google' namespace. Do not include tests,
87 # benchmarks, etc.
88 packages = [
89     package
90     for package in setuptools.PEP420PackageFinder.find()
91     if package.startswith("google")
92 ]
93 
94 # Determine which namespaces are needed.
95 namespaces = ["google"]
96 if "google.cloud" in packages:
97     namespaces.append("google.cloud")
98 
99 
100 setuptools.setup(
101     name=name,
102     version=version,
103     description=description,
104     long_description=readme,
105     author="Google LLC",
106     author_email="googleapis-packages@google.com",
107     license="Apache 2.0",
108     url="https://github.com/googleapis/python-bigquery",
109     classifiers=[
110         release_status,
111         "Intended Audience :: Developers",
112         "License :: OSI Approved :: Apache Software License",
113         "Programming Language :: Python",
114         "Programming Language :: Python :: 3",
115         "Programming Language :: Python :: 3.6",
116         "Programming Language :: Python :: 3.7",
117         "Programming Language :: Python :: 3.8",
118         "Programming Language :: Python :: 3.9",
119         "Operating System :: OS Independent",
120         "Topic :: Internet",
121     ],
122     platforms="Posix; MacOS X; Windows",
123     packages=packages,
124     namespace_packages=namespaces,
125     install_requires=dependencies,
126     extras_require=extras,
127     python_requires=">=3.6, <3.10",
128     include_package_data=True,
129     zip_safe=False,
130 )
131
```

Path: `google/cloud/bigquery/retry.py`
Content:
```
1 # Copyright 2018 Google LLC
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 from google.api_core import exceptions
16 from google.api_core import retry
17 
18 
19 _RETRYABLE_REASONS = frozenset(
20     ["rateLimitExceeded", "backendError", "internalError", "badGateway"]
21 )
22 
23 _UNSTRUCTURED_RETRYABLE_TYPES = (
24     exceptions.TooManyRequests,
25     exceptions.InternalServerError,
26     exceptions.BadGateway,
27 )
28 
29 
30 def _should_retry(exc):
31     """Predicate for determining when to retry.
32 
33     We retry if and only if the 'reason' is 'backendError'
34     or 'rateLimitExceeded'.
35     """
36     if not hasattr(exc, "errors"):
37         return False
38 
39     if len(exc.errors) == 0:
40         # Check for unstructured error returns, e.g. from GFE
41         return isinstance(exc, _UNSTRUCTURED_RETRYABLE_TYPES)
42 
43     reason = exc.errors[0]["reason"]
44     return reason in _RETRYABLE_REASONS
45 
46 
47 DEFAULT_RETRY = retry.Retry(predicate=_should_retry)
48 """The default retry object.
49 
50 Any method with a ``retry`` parameter will be retried automatically,
51 with reasonable defaults. To disable retry, pass ``retry=None``.
52 To modify the default retry behavior, call a ``with_XXX`` method
53 on ``DEFAULT_RETRY``. For example, to change the deadline to 30 seconds,
54 pass ``retry=bigquery.DEFAULT_RETRY.with_deadline(30)``.
55 """
56
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/google/cloud/bigquery/retry.py b/google/cloud/bigquery/retry.py
--- a/google/cloud/bigquery/retry.py
+++ b/google/cloud/bigquery/retry.py
@@ -14,6 +14,7 @@
 
 from google.api_core import exceptions
 from google.api_core import retry
+import requests.exceptions
 
 
 _RETRYABLE_REASONS = frozenset(
@@ -21,9 +22,11 @@
 )
 
 _UNSTRUCTURED_RETRYABLE_TYPES = (
+    ConnectionError,
     exceptions.TooManyRequests,
     exceptions.InternalServerError,
     exceptions.BadGateway,
+    requests.exceptions.ConnectionError,
 )
 
 
@@ -33,10 +36,7 @@
     We retry if and only if the 'reason' is 'backendError'
     or 'rateLimitExceeded'.
     """
-    if not hasattr(exc, "errors"):
-        return False
-
-    if len(exc.errors) == 0:
+    if not hasattr(exc, "errors") or len(exc.errors) == 0:
         # Check for unstructured error returns, e.g. from GFE
         return isinstance(exc, _UNSTRUCTURED_RETRYABLE_TYPES)
 
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -35,6 +35,7 @@
     "google-resumable-media >= 0.6.0, < 2.0dev",
     "packaging >= 14.3",
     "protobuf >= 3.12.0",
+    "requests >= 2.18.0, < 3.0.0dev",
 ]
 extras = {
     "bqstorage": [

{"golden_diff": "diff --git a/google/cloud/bigquery/retry.py b/google/cloud/bigquery/retry.py\n--- a/google/cloud/bigquery/retry.py\n+++ b/google/cloud/bigquery/retry.py\n@@ -14,6 +14,7 @@\n \n from google.api_core import exceptions\n from google.api_core import retry\n+import requests.exceptions\n \n \n _RETRYABLE_REASONS = frozenset(\n@@ -21,9 +22,11 @@\n )\n \n _UNSTRUCTURED_RETRYABLE_TYPES = (\n+    ConnectionError,\n     exceptions.TooManyRequests,\n     exceptions.InternalServerError,\n     exceptions.BadGateway,\n+    requests.exceptions.ConnectionError,\n )\n \n \n@@ -33,10 +36,7 @@\n     We retry if and only if the 'reason' is 'backendError'\n     or 'rateLimitExceeded'.\n     \"\"\"\n-    if not hasattr(exc, \"errors\"):\n-        return False\n-\n-    if len(exc.errors) == 0:\n+    if not hasattr(exc, \"errors\") or len(exc.errors) == 0:\n         # Check for unstructured error returns, e.g. from GFE\n         return isinstance(exc, _UNSTRUCTURED_RETRYABLE_TYPES)\n \ndiff --git a/setup.py b/setup.py\n--- a/setup.py\n+++ b/setup.py\n@@ -35,6 +35,7 @@\n     \"google-resumable-media >= 0.6.0, < 2.0dev\",\n     \"packaging >= 14.3\",\n     \"protobuf >= 3.12.0\",\n+    \"requests >= 2.18.0, < 3.0.0dev\",\n ]\n extras = {\n     \"bqstorage\": [\n", "issue": "ConnectionError in Client.insert_rows_json()\nWe have a http Cloud Function that does some data processing and then streams to BQ. The function errors out sometimes because of either the bq client losing connection or it is the insert_rows that can't connect. \r\nSee below an example of a stack trace captured in the GCP logs.\r\n\r\n\r\n```\r\n  File \"/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py\", line 2447, in wsgi_app\r\n    response = self.full_dispatch_request()\r\n  File \"/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py\", line 1952, in full_dispatch_request\r\n    rv = self.handle_user_exception(e)\r\n  File \"/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py\", line 1821, in handle_user_exception\r\n    reraise(exc_type, exc_value, tb)\r\n  File \"/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/_compat.py\", line 39, in reraise\r\n    raise value\r\n  File \"/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py\", line 1950, in full_dispatch_request\r\n    rv = self.dispatch_request()\r\n  File \"/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py\", line 1936, in dispatch_request\r\n    return self.view_functions[rule.endpoint](**req.view_args)\r\n  File \"/layers/google.python.functions-framework/functions-framework/lib/python3.8/site-packages/functions_framework/__init__.py\", line 66, in view_func\r\n    return function(request._get_current_object())\r\n  File \"/workspace/main.py\", line 162, in stream_tax\r\n    errors = bq.insert_rows_json(table=dataset_table,\r\n  File \"/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/bigquery/client.py\", line 3013, in insert_rows_json\r\n    response = self._call_api(\r\n  File \"/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/bigquery/client.py\", line 636, in _call_api\r\n    return call()\r\n  File \"/layers/google.python.pip/pip/lib/python3.8/site-packages/google/api_core/retry.py\", line 281, in retry_wrapped_func\r\n    return retry_target(\r\n  File \"/layers/google.python.pip/pip/lib/python3.8/site-packages/google/api_core/retry.py\", line 184, in retry_target\r\n    return target()\r\n  File \"/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/_http.py\", line 427, in api_request\r\n    response = self._make_request(\r\n  File \"/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/_http.py\", line 291, in _make_request\r\n    return self._do_request(\r\n  File \"/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/_http.py\", line 329, in _do_request\r\n    return self.http.request(\r\n  File \"/layers/google.python.pip/pip/lib/python3.8/site-packages/google/auth/transport/requests.py\", line 464, in request\r\n    response = super(AuthorizedSession, self).request(\r\n  File \"/layers/google.python.pip/pip/lib/python3.8/site-packages/requests/sessions.py\", line 542, in request\r\n    resp = self.send(prep, **send_kwargs)\r\n  File \"/layers/google.python.pip/pip/lib/python3.8/site-packages/requests/sessions.py\", line 655, in send\r\n    r = adapter.send(request, **kwargs)\r\n  File \"/layers/google.python.pip/pip/lib/python3.8/site-packages/requests/adapters.py\", line 498, in send\r\n    raise ConnectionError(err, request=request)\r\nrequests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))\r\n```\r\n\r\n\r\n- `bq` (=`bigquery.Client()`) in the trace is instantiated as a global variable as recommended here: https://cloud.google.com/functions/docs/bestpractices/networking#accessing_google_apis\r\n\r\n- error is logged 30 secs after function is invoked - so can't be the 60s default timeout in `-http`\r\n\r\nThoughts ?\n", "before_files": [{"content": "# Copyright 2018 Google LLC\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport io\nimport os\n\nimport setuptools\n\n\n# Package metadata.\n\nname = \"google-cloud-bigquery\"\ndescription = \"Google BigQuery API client library\"\n\n# Should be one of:\n# 'Development Status :: 3 - Alpha'\n# 'Development Status :: 4 - Beta'\n# 'Development Status :: 5 - Production/Stable'\nrelease_status = \"Development Status :: 5 - Production/Stable\"\ndependencies = [\n    \"google-api-core[grpc] >= 1.23.0, < 2.0.0dev\",\n    \"proto-plus >= 1.10.0\",\n    \"google-cloud-core >= 1.4.1, < 2.0dev\",\n    \"google-resumable-media >= 0.6.0, < 2.0dev\",\n    \"packaging >= 14.3\",\n    \"protobuf >= 3.12.0\",\n]\nextras = {\n    \"bqstorage\": [\n        \"google-cloud-bigquery-storage >= 2.0.0, <3.0.0dev\",\n        # Due to an issue in pip's dependency resolver, the `grpc` extra is not\n        # installed, even though `google-cloud-bigquery-storage` specifies it\n        # as `google-api-core[grpc]`. We thus need to explicitly specify it here.\n        # See: https://github.com/googleapis/python-bigquery/issues/83 The\n        # grpc.Channel.close() method isn't added until 1.32.0.\n        # https://github.com/grpc/grpc/pull/15254\n        \"grpcio >= 1.32.0, < 2.0dev\",\n        \"pyarrow >= 1.0.0, < 4.0dev\",\n    ],\n    \"pandas\": [\"pandas>=0.23.0\", \"pyarrow >= 1.0.0, < 4.0dev\"],\n    \"bignumeric_type\": [\"pyarrow >= 3.0.0, < 4.0dev\"],\n    \"tqdm\": [\"tqdm >= 4.7.4, <5.0.0dev\"],\n    \"opentelemetry\": [\n        \"opentelemetry-api==0.11b0\",\n        \"opentelemetry-sdk==0.11b0\",\n        \"opentelemetry-instrumentation==0.11b0\",\n    ],\n}\n\nall_extras = []\n\nfor extra in extras:\n    # Exclude this extra from all to avoid overly strict dependencies on core\n    # libraries such as pyarrow.\n    # https://github.com/googleapis/python-bigquery/issues/563\n    if extra in {\"bignumeric_type\"}:\n        continue\n    all_extras.extend(extras[extra])\n\nextras[\"all\"] = all_extras\n\n# Setup boilerplate below this line.\n\npackage_root = os.path.abspath(os.path.dirname(__file__))\n\nreadme_filename = os.path.join(package_root, \"README.rst\")\nwith io.open(readme_filename, encoding=\"utf-8\") as readme_file:\n    readme = readme_file.read()\n\nversion = {}\nwith open(os.path.join(package_root, \"google/cloud/bigquery/version.py\")) as fp:\n    exec(fp.read(), version)\nversion = version[\"__version__\"]\n\n# Only include packages under the 'google' namespace. Do not include tests,\n# benchmarks, etc.\npackages = [\n    package\n    for package in setuptools.PEP420PackageFinder.find()\n    if package.startswith(\"google\")\n]\n\n# Determine which namespaces are needed.\nnamespaces = [\"google\"]\nif \"google.cloud\" in packages:\n    namespaces.append(\"google.cloud\")\n\n\nsetuptools.setup(\n    name=name,\n    version=version,\n    description=description,\n    long_description=readme,\n    author=\"Google LLC\",\n    author_email=\"googleapis-packages@google.com\",\n    license=\"Apache 2.0\",\n    url=\"https://github.com/googleapis/python-bigquery\",\n    classifiers=[\n        release_status,\n        \"Intended Audience :: Developers\",\n        \"License :: OSI Approved :: Apache Software License\",\n        \"Programming Language :: Python\",\n        \"Programming Language :: Python :: 3\",\n        \"Programming Language :: Python :: 3.6\",\n        \"Programming Language :: Python :: 3.7\",\n        \"Programming Language :: Python :: 3.8\",\n        \"Programming Language :: Python :: 3.9\",\n        \"Operating System :: OS Independent\",\n        \"Topic :: Internet\",\n    ],\n    platforms=\"Posix; MacOS X; Windows\",\n    packages=packages,\n    namespace_packages=namespaces,\n    install_requires=dependencies,\n    extras_require=extras,\n    python_requires=\">=3.6, <3.10\",\n    include_package_data=True,\n    zip_safe=False,\n)\n", "path": "setup.py"}, {"content": "# Copyright 2018 Google LLC\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom google.api_core import exceptions\nfrom google.api_core import retry\n\n\n_RETRYABLE_REASONS = frozenset(\n    [\"rateLimitExceeded\", \"backendError\", \"internalError\", \"badGateway\"]\n)\n\n_UNSTRUCTURED_RETRYABLE_TYPES = (\n    exceptions.TooManyRequests,\n    exceptions.InternalServerError,\n    exceptions.BadGateway,\n)\n\n\ndef _should_retry(exc):\n    \"\"\"Predicate for determining when to retry.\n\n    We retry if and only if the 'reason' is 'backendError'\n    or 'rateLimitExceeded'.\n    \"\"\"\n    if not hasattr(exc, \"errors\"):\n        return False\n\n    if len(exc.errors) == 0:\n        # Check for unstructured error returns, e.g. from GFE\n        return isinstance(exc, _UNSTRUCTURED_RETRYABLE_TYPES)\n\n    reason = exc.errors[0][\"reason\"]\n    return reason in _RETRYABLE_REASONS\n\n\nDEFAULT_RETRY = retry.Retry(predicate=_should_retry)\n\"\"\"The default retry object.\n\nAny method with a ``retry`` parameter will be retried automatically,\nwith reasonable defaults. To disable retry, pass ``retry=None``.\nTo modify the default retry behavior, call a ``with_XXX`` method\non ``DEFAULT_RETRY``. For example, to change the deadline to 30 seconds,\npass ``retry=bigquery.DEFAULT_RETRY.with_deadline(30)``.\n\"\"\"\n", "path": "google/cloud/bigquery/retry.py"}], "after_files": [{"content": "# Copyright 2018 Google LLC\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport io\nimport os\n\nimport setuptools\n\n\n# Package metadata.\n\nname = \"google-cloud-bigquery\"\ndescription = \"Google BigQuery API client library\"\n\n# Should be one of:\n# 'Development Status :: 3 - Alpha'\n# 'Development Status :: 4 - Beta'\n# 'Development Status :: 5 - Production/Stable'\nrelease_status = \"Development Status :: 5 - Production/Stable\"\ndependencies = [\n    \"google-api-core[grpc] >= 1.23.0, < 2.0.0dev\",\n    \"proto-plus >= 1.10.0\",\n    \"google-cloud-core >= 1.4.1, < 2.0dev\",\n    \"google-resumable-media >= 0.6.0, < 2.0dev\",\n    \"packaging >= 14.3\",\n    \"protobuf >= 3.12.0\",\n    \"requests >= 2.18.0, < 3.0.0dev\",\n]\nextras = {\n    \"bqstorage\": [\n        \"google-cloud-bigquery-storage >= 2.0.0, <3.0.0dev\",\n        # Due to an issue in pip's dependency resolver, the `grpc` extra is not\n        # installed, even though `google-cloud-bigquery-storage` specifies it\n        # as `google-api-core[grpc]`. We thus need to explicitly specify it here.\n        # See: https://github.com/googleapis/python-bigquery/issues/83 The\n        # grpc.Channel.close() method isn't added until 1.32.0.\n        # https://github.com/grpc/grpc/pull/15254\n        \"grpcio >= 1.32.0, < 2.0dev\",\n        \"pyarrow >= 1.0.0, < 4.0dev\",\n    ],\n    \"pandas\": [\"pandas>=0.23.0\", \"pyarrow >= 1.0.0, < 4.0dev\"],\n    \"bignumeric_type\": [\"pyarrow >= 3.0.0, < 4.0dev\"],\n    \"tqdm\": [\"tqdm >= 4.7.4, <5.0.0dev\"],\n    \"opentelemetry\": [\n        \"opentelemetry-api==0.11b0\",\n        \"opentelemetry-sdk==0.11b0\",\n        \"opentelemetry-instrumentation==0.11b0\",\n    ],\n}\n\nall_extras = []\n\nfor extra in extras:\n    # Exclude this extra from all to avoid overly strict dependencies on core\n    # libraries such as pyarrow.\n    # https://github.com/googleapis/python-bigquery/issues/563\n    if extra in {\"bignumeric_type\"}:\n        continue\n    all_extras.extend(extras[extra])\n\nextras[\"all\"] = all_extras\n\n# Setup boilerplate below this line.\n\npackage_root = os.path.abspath(os.path.dirname(__file__))\n\nreadme_filename = os.path.join(package_root, \"README.rst\")\nwith io.open(readme_filename, encoding=\"utf-8\") as readme_file:\n    readme = readme_file.read()\n\nversion = {}\nwith open(os.path.join(package_root, \"google/cloud/bigquery/version.py\")) as fp:\n    exec(fp.read(), version)\nversion = version[\"__version__\"]\n\n# Only include packages under the 'google' namespace. Do not include tests,\n# benchmarks, etc.\npackages = [\n    package\n    for package in setuptools.PEP420PackageFinder.find()\n    if package.startswith(\"google\")\n]\n\n# Determine which namespaces are needed.\nnamespaces = [\"google\"]\nif \"google.cloud\" in packages:\n    namespaces.append(\"google.cloud\")\n\n\nsetuptools.setup(\n    name=name,\n    version=version,\n    description=description,\n    long_description=readme,\n    author=\"Google LLC\",\n    author_email=\"googleapis-packages@google.com\",\n    license=\"Apache 2.0\",\n    url=\"https://github.com/googleapis/python-bigquery\",\n    classifiers=[\n        release_status,\n        \"Intended Audience :: Developers\",\n        \"License :: OSI Approved :: Apache Software License\",\n        \"Programming Language :: Python\",\n        \"Programming Language :: Python :: 3\",\n        \"Programming Language :: Python :: 3.6\",\n        \"Programming Language :: Python :: 3.7\",\n        \"Programming Language :: Python :: 3.8\",\n        \"Programming Language :: Python :: 3.9\",\n        \"Operating System :: OS Independent\",\n        \"Topic :: Internet\",\n    ],\n    platforms=\"Posix; MacOS X; Windows\",\n    packages=packages,\n    namespace_packages=namespaces,\n    install_requires=dependencies,\n    extras_require=extras,\n    python_requires=\">=3.6, <3.10\",\n    include_package_data=True,\n    zip_safe=False,\n)\n", "path": "setup.py"}, {"content": "# Copyright 2018 Google LLC\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom google.api_core import exceptions\nfrom google.api_core import retry\nimport requests.exceptions\n\n\n_RETRYABLE_REASONS = frozenset(\n    [\"rateLimitExceeded\", \"backendError\", \"internalError\", \"badGateway\"]\n)\n\n_UNSTRUCTURED_RETRYABLE_TYPES = (\n    ConnectionError,\n    exceptions.TooManyRequests,\n    exceptions.InternalServerError,\n    exceptions.BadGateway,\n    requests.exceptions.ConnectionError,\n)\n\n\ndef _should_retry(exc):\n    \"\"\"Predicate for determining when to retry.\n\n    We retry if and only if the 'reason' is 'backendError'\n    or 'rateLimitExceeded'.\n    \"\"\"\n    if not hasattr(exc, \"errors\") or len(exc.errors) == 0:\n        # Check for unstructured error returns, e.g. from GFE\n        return isinstance(exc, _UNSTRUCTURED_RETRYABLE_TYPES)\n\n    reason = exc.errors[0][\"reason\"]\n    return reason in _RETRYABLE_REASONS\n\n\nDEFAULT_RETRY = retry.Retry(predicate=_should_retry)\n\"\"\"The default retry object.\n\nAny method with a ``retry`` parameter will be retried automatically,\nwith reasonable defaults. To disable retry, pass ``retry=None``.\nTo modify the default retry behavior, call a ``with_XXX`` method\non ``DEFAULT_RETRY``. For example, to change the deadline to 30 seconds,\npass ``retry=bigquery.DEFAULT_RETRY.with_deadline(30)``.\n\"\"\"\n", "path": "google/cloud/bigquery/retry.py"}]}