CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-google-api-python-client

Google API Client Library for Python that provides discovery-based access to hundreds of Google services with authentication, caching, and media upload/download support.

Pending
Overview
Eval results
Files

model.mddocs/

Data Models and Request Processing

The model module provides data models for handling different request and response formats including JSON, raw data, media, and protocol buffers. These models control how request parameters are serialized and how responses are processed.

Capabilities

Base Model Classes

Abstract base classes that define the model interface for all data formats.

class Model:
    """Model base class.
    
    All Model classes should implement this interface. The Model serializes and
    de-serializes between a wire format such as JSON and a Python object representation.
    """
    
    def request(self, headers, path_params, query_params, body_value):
        """
        Updates the request with serialized body and appropriate headers.
        
        Args:
            headers (dict): HTTP headers dictionary to modify
            path_params (dict): Parameters for URL path substitution
            query_params (dict): Query string parameters
            body_value (object): Request body data to serialize
            
        Returns:
            tuple: (headers, path_params, query, body) - processed request components
        """
    
    def response(self, resp, content):
        """
        Converts the HTTP response into a Python object.
        
        Args:
            resp (httplib2.Response): HTTP response object
            content (bytes): Response body content
            
        Returns:
            object: Deserialized Python object representation
            
        Raises:
            googleapiclient.errors.HttpError: For non-2xx HTTP responses
        """

class BaseModel(Model):
    """Base model class.
    
    Subclasses should provide implementations for the 'serialize' and 'deserialize'
    methods, as well as values for the following class attributes:
    - accept: HTTP Accept header value
    - content_type: HTTP Content-type header value  
    - no_content_response: Value for 204 No Content responses
    - alt_param: Value for "alt" query parameter
    """
    
    accept = None
    content_type = None
    no_content_response = None
    alt_param = None
    
    def request(self, headers, path_params, query_params, body_value, api_version=None):
        """
        Enhanced request method with API version support.
        
        Args:
            headers (dict): HTTP headers dictionary to modify
            path_params (dict): Parameters for URL path substitution
            query_params (dict): Query string parameters
            body_value (object): Request body data to serialize
            api_version (str, optional): API version for request metadata
            
        Returns:
            tuple: (headers, path_params, query, body) - processed request components
        """
    
    def response(self, resp, content):
        """
        Process HTTP response with error handling.
        
        Args:
            resp (httplib2.Response): HTTP response object
            content (bytes): Response body content
            
        Returns:
            object: Deserialized response object
        """
    
    def serialize(self, body_value):
        """
        Serialize Python object to wire format.
        
        Args:
            body_value (object): Python object to serialize
            
        Returns:
            str or bytes: Serialized representation
        """
    
    def deserialize(self, content):
        """
        Deserialize wire format to Python object.
        
        Args:
            content (bytes): Serialized content to deserialize
            
        Returns:
            object: Deserialized Python object
        """

JSON Data Model

Handle JSON request and response processing with optional data wrapping.

class JsonModel(BaseModel):
    """Model for JSON request and response processing."""
    
    def __init__(self, data_wrapper=False):
        """
        Initialize JSON model.
        
        Args:
            data_wrapper (bool): Whether to wrap request data in a 'data' field
        """
    
    def request(self, headers, path_params, query_params, body_value):
        """
        Create a JSON request.
        
        Args:
            headers (dict): HTTP headers to modify
            path_params (dict): Parameters for URL path substitution
            query_params (dict): Query string parameters
            body_value (object): Request body data to serialize
            
        Returns:
            tuple: (headers, path_params, query_params, body) - processed request components
        """
    
    def response(self, resp, content):
        """
        Process a JSON response.
        
        Args:
            resp (httplib2.Response): HTTP response object
            content (bytes): Response body content
            
        Returns:
            object: Deserialized JSON response data
        """

Raw Data Model

Handle raw binary request and response data without serialization.

class RawModel(JsonModel):
    """Model for requests that don't return JSON.
    
    Serializes and de-serializes between JSON and the Python object representation
    of HTTP request, and returns the raw bytes of the response body.
    """
    
    def request(self, headers, path_params, query_params, body_value):
        """
        Create a raw data request.
        
        Args:
            headers (dict): HTTP headers to modify
            path_params (dict): Parameters for URL path substitution
            query_params (dict): Query string parameters
            body_value (bytes or str): Raw request body data
            
        Returns:
            tuple: (headers, path_params, query_params, body) - processed request components
        """
    
    def response(self, resp, content):
        """
        Process a raw response.
        
        Args:
            resp (httplib2.Response): HTTP response object
            content (bytes): Response body content
            
        Returns:
            bytes: Raw response content
        """

Media Data Model

Handle media requests and responses for file uploads and downloads.

class MediaModel(JsonModel):
    """Model for requests that return Media.
    
    Serializes and de-serializes between JSON and the Python object representation
    of HTTP request, and returns the raw bytes of the response body.
    """
    
    def request(self, headers, path_params, query_params, body_value):
        """
        Create a media request.
        
        Args:
            headers (dict): HTTP headers to modify
            path_params (dict): Parameters for URL path substitution  
            query_params (dict): Query string parameters
            body_value (MediaUpload): Media upload object
            
        Returns:
            tuple: (headers, path_params, query_params, body) - processed request components
        """
    
    def response(self, resp, content):
        """
        Process a media response.
        
        Args:
            resp (httplib2.Response): HTTP response object
            content (bytes): Response body content
            
        Returns:
            object: Processed media response data
        """

Protocol Buffer Model

Handle Protocol Buffer serialization and deserialization.

class ProtocolBufferModel(BaseModel):
    """Model for protocol buffers.
    
    Serializes and de-serializes the binary protocol buffer sent in the HTTP
    request and response bodies.
    """
    
    def __init__(self, protocol_buffer):
        """
        Initialize Protocol Buffer model.
        
        Args:
            protocol_buffer: Protocol Buffer message class for serialization
        """
    
    def request(self, headers, path_params, query_params, body_value):
        """
        Create a Protocol Buffer request.
        
        Args:
            headers (dict): HTTP headers to modify
            path_params (dict): Parameters for URL path substitution
            query_params (dict): Query string parameters
            body_value (Message): Protocol Buffer message to serialize
            
        Returns:
            tuple: (headers, path_params, query_params, body) - processed request components
        """
    
    def response(self, resp, content):
        """
        Process a Protocol Buffer response.
        
        Args:
            resp (httplib2.Response): HTTP response object
            content (bytes): Response body content
            
        Returns:
            Message: Deserialized Protocol Buffer message
        """

Request Processing Utilities

Utility functions for request body construction and parameter handling.

def makepatch(original, modified):
    """
    Create a patch object by comparing two resource states.
    
    Some methods support PATCH, an efficient way to send updates to a resource.
    This method allows the easy construction of patch bodies by looking at the
    differences between a resource before and after it was modified.
    
    Args:
        original (object): The original deserialized resource
        modified (object): The modified deserialized resource
        
    Returns:
        object: An object containing only the changes from original to modified
    """

# Module-level debugging flag
dump_request_response = False  # Boolean flag for enabling request/response logging

Usage Examples

Custom JSON Model

from googleapiclient import discovery
from googleapiclient.model import JsonModel

# Create service with custom JSON model
json_model = JsonModel(data_wrapper=True)
service = discovery.build(
    'gmail', 
    'v1', 
    credentials=credentials,
    model=json_model
)

# Requests will wrap data in 'data' field
# Responses will be processed as JSON
messages = service.users().messages().list(userId='me').execute()

Raw Data Model

from googleapiclient.model import RawModel

# Create service with raw model for binary data
raw_model = RawModel()
service = discovery.build(
    'storage', 
    'v1', 
    credentials=credentials,
    model=raw_model
)

# Responses will be returned as raw bytes
object_data = service.objects().get(
    bucket='my-bucket', 
    object='my-file.bin'
).execute()

print(f"Downloaded {len(object_data)} bytes")

Media Model Integration

from googleapiclient.model import MediaModel
from googleapiclient.http import MediaFileUpload

# Create service with media model
media_model = MediaModel()
service = discovery.build(
    'drive', 
    'v3', 
    credentials=credentials,
    model=media_model
)

# Upload with media model handling
media = MediaFileUpload('document.pdf', mimetype='application/pdf')
file_metadata = {'name': 'uploaded-document.pdf'}

file = service.files().create(
    body=file_metadata,
    media_body=media
).execute()

Protocol Buffer Model

from googleapiclient.model import ProtocolBufferModel
import my_protobuf_pb2  # Your protocol buffer definitions

# Create service with protocol buffer model
pb_model = ProtocolBufferModel(my_protobuf_pb2.MyMessage)
service = discovery.build(
    'my-api', 
    'v1', 
    credentials=credentials,
    model=pb_model
)

# Create protocol buffer message
message = my_protobuf_pb2.MyMessage()
message.field1 = "value1"
message.field2 = 42

# Send protocol buffer request
response = service.my_resource().create(body=message).execute()
# Response will be deserialized protocol buffer

Custom Request Body Processing

from googleapiclient.model import makebody

# Custom request processing
def create_custom_request(service, resource_data):
    headers = {'Content-Type': 'application/json'}
    params = {'userId': 'me', 'format': 'json'}
    
    # Method description from discovery document
    method_desc = {
        'parameters': {
            'userId': {'location': 'path'},
            'format': {'location': 'query'}
        }
    }
    
    # Create request body
    processed_headers, processed_body = makebody(
        headers, 
        params, 
        resource_data, 
        method_desc
    )
    
    return processed_headers, processed_body

# Use custom processing
headers, body = create_custom_request(service, {'message': 'Hello World'})

JSON Patch Creation

from googleapiclient.model import makepatch

# Original and modified data
original_message = {
    'subject': 'Original Subject',
    'body': 'Original body text',
    'labels': ['INBOX', 'UNREAD']
}

modified_message = {
    'subject': 'Updated Subject',
    'body': 'Original body text',  # Unchanged
    'labels': ['INBOX']  # Removed UNREAD
}

# Create patch
patch = makepatch(original_message, modified_message)
print(patch)
# Output: Patch operations representing the changes

# Use patch in API request
service.users().messages().modify(
    userId='me',
    id='message_id',
    body=patch
).execute()

Model Selection Based on Content Type

from googleapiclient.model import JsonModel, RawModel, MediaModel

def get_model_for_content_type(content_type):
    """Select appropriate model based on content type."""
    
    if content_type.startswith('application/json'):
        return JsonModel()
    elif content_type.startswith('image/') or content_type.startswith('video/'):
        return MediaModel()
    else:
        return RawModel()

# Dynamic model selection
content_type = 'application/json'
model = get_model_for_content_type(content_type)

service = discovery.build(
    'my-api', 
    'v1', 
    credentials=credentials,
    model=model
)

Request and Response Interception

from googleapiclient.model import JsonModel

class LoggingJsonModel(JsonModel):
    """JSON model with request/response logging."""
    
    def request(self, headers, path_params, query_params, body_value):
        print(f"Request body: {body_value}")
        return super().request(headers, path_params, query_params, body_value)
    
    def response(self, resp, content):
        print(f"Response status: {resp.status}")
        result = super().response(resp, content)
        print(f"Response data: {result}")
        return result

# Use logging model
logging_model = LoggingJsonModel()
service = discovery.build(
    'gmail', 
    'v1', 
    credentials=credentials,
    model=logging_model
)

# All requests and responses will be logged
messages = service.users().messages().list(userId='me').execute()

Install with Tessl CLI

npx tessl i tessl/pypi-google-api-python-client@2.181.1

docs

auth.md

channel.md

discovery.md

errors.md

http.md

index.md

media.md

mimeparse.md

model.md

schema.md

testing.md

tile.json