Google Apitools
Apitools is a Python library for accessing REST APIs, primarily designed for auto-generated client libraries for Google APIs. As of its latest version (0.5.35), it is not actively developed and is considered deprecated, being maintained solely for use by the Google Cloud SDK. New projects should utilize the appropriate Google Cloud Client Libraries instead.
Warnings
- breaking This library is officially deprecated and not actively developed. It is maintained only for use by the Google Cloud SDK. New projects should NOT use `google-apitools` directly; instead, use the official Google Cloud Client Libraries for specific services (e.g., `google-cloud-storage`, `google-cloud-bigquery`).
- breaking Version 0.5.5 was an emergency release due to a backwards incompatible change in `oauth2client` 4.0.0. This indicates tight coupling with older dependency versions, which might break with newer versions of its core dependencies if not carefully managed.
- gotcha The library heavily supports Python 2.7 (indicated by `requires_python: >=2.7`). While it works with Python 3, its design and maintenance context are rooted in an older era of Python development, which might lead to unexpected behaviors or compatibility issues in modern Python 3-only environments.
- gotcha Apitools is primarily designed for use with *auto-generated* client libraries specific to Google APIs. Direct usage of its base components (like `Http` or `encoding`) for general REST API interaction can be more complex and less intuitive compared to other, more actively maintained HTTP client libraries (e.g., `requests`).
Install
-
pip install google-apitools
Imports
- encoding
from apitools.base.py import encoding
- HttpError
from apitools.base.py import exceptions
- GetCredentials
from apitools.base.py import credentials_lib
- Http
from apitools.base.py import http_wrapper
Quickstart
from apitools.base.py import encoding
from apitools.base.py import exceptions
# Apitools is primarily used with auto-generated client libraries
# that define specific Message classes. For demonstration, we'll
# define a simple Message using apitools' base Message class.
class MySimpleMessage(encoding.Message):
field1 = encoding.StringField(1)
field2 = encoding.IntegerField(2)
# 1. Convert a Python dictionary to an apitools Message
data_dict = {'field1': 'Hello Apitools', 'field2': 42}
message_instance = encoding.DictToMessage(data_dict, MySimpleMessage)
print(f"Original dict: {data_dict}")
print(f"Converted Message: {message_instance}")
print(f"Accessing message fields: {message_instance.field1}, {message_instance.field2}")
# 2. Convert an apitools Message back to a Python dictionary
dict_from_message = encoding.MessageToDict(message_instance)
print(f"Converted back to dict: {dict_from_message}")
# 3. Handle simulated HttpError (common in API interactions)
try:
# In a real scenario, this error would be raised during an API call
raise exceptions.HttpError(
'https://api.example.com/data/404',
request_headers={'User-Agent': 'apitools-demo'},
response_headers={'Content-Type': 'application/json'},
status_code=404,
content=b'{"error": "Resource not found"}'
)
except exceptions.HttpError as e:
print(f"\nCaught a simulated HTTP Error:")
print(f"Status Code: {e.status_code}")
print(f"Error Content: {e.content.decode('utf-8')}")
print(f"URL: {e.url}")