Skip to content
The Chat Archive
claude-3-5-sonnet-20241022· Anthropic LMArena Human Preference 140K

Write me a standard python function, as generic as possible, to handle calls to a rest api

  1. User

    Write me a standard python function, as generic as possible, to handle calls to a rest api

  2. Assistant

    Here's a generic Python function to handle REST API calls using the requests library. This function includes error handling, request customization, and common HTTP methods:

    import requests
    from typing import Optional, Any, Dict
    import logging
    from requests.exceptions import RequestException
    from http import HTTPStatus
    
    def make_api_request(
        url: str,
        method: str = 'GET',
        params: Optional[Dict] = None,
        data: Optional[Any] = None,
        json_data: Optional[Dict] = None,
        headers: Optional[Dict] = None,
        timeout: int = 30,
        verify_ssl: bool = True,
        auth: Optional[tuple] = None,
        retry_attempts: int = 3,
        retry_delay: int = 1
    ) -> Dict:
        """
        Makes a REST API request with error handling and retries.
    
        Args:
            url (str): The API endpoint URL
            method (str): HTTP method (GET, POST, PUT, DELETE, etc.)
            params (dict, optional): URL parameters
            data (Any, optional): Request body data
            json_data (dict, optional): JSON request body
            headers (dict, optional): Request headers
            timeout (int): Request timeout in seconds
            verify_ssl (bool): Whether to verify SSL certificates
            auth (tuple, optional): Authentication credentials (username, password)
            retry_attempts (int): Number of retry attempts for failed requests
            retry_delay (int): Delay between retries in seconds
    
        Returns:
            dict: Response data
    
        Raises:
            RequestException: If the request fails after all retry attempts
        """
        
        # Setup logging
        logger = logging.getLogger(__name__)
        
        # Default headers if none provided
        if headers is None:
            headers = {
                'Content-Type': 'application/json',
                'Accept': 'application/json'
            }
    
        # Normalize HTTP method
        method = method.upper()
    
        # Initialize response
        response = None
        last_exception = None
    
        # Retry loop
        for attempt in range(retry_attempts):
            try:
                response = requests.request(
                    method=method,
                    url=url,
                    params=params,
                    data=data,
                    json=json_data,
                    headers=headers,
                    timeout=timeout,
                    verify=verify_ssl,
                    auth=auth
                )
    
                # Log request details
                logger.debug(f"API Request: {method} {url}")
                logger.debug(f"Headers: {headers}")
                logger.debug(f"Params: {params}")
                logger.debug(f"Data: {data}")
                logger.debug(f"JSON: {json_data}")
    
                # Check if response is successful
                response.raise_for_status()
    
                # Try to return JSON response
                try:
                    return response.json()
                except ValueError:
                    # Return text response if not JSON
                    return {'content': response.text}
    
            except requests.exceptions.HTTPError as e:
                logger.error(f"HTTP Error: {e}")
                last_exception = e
                
                # Handle specific HTTP status codes
                if response is not None:
                    if response.status_code == HTTPStatus.UNAUTHORIZED:
                        logger.error("Authentication failed")
                        break  # Don't retry on authentication failures
                    elif response.status_code == HTTPStatus.NOT_FOUND:
                        logger.error("Resource not found")
                        break  # Don't retry on 404 errors
    
            except requests.exceptions.ConnectionError as e:
                logger.error(f"Connection Error: {e}")
                last_exception = e
    
            except requests.exceptions.Timeout as e:
                logger.error(f"Timeout Error: {e}")
                last_exception = e
    
            except requests.exceptions.RequestException as e:
                logger.error(f"Request Error: {e}")
                last_exception = e
    
            # If this wasn't the last attempt, wait before retrying
            if attempt < retry_attempts - 1:
                import time
                time.sleep(retry_delay)
                logger.info(f"Retrying request (attempt {attempt + 2}/{retry_attempts})")
            
        # If all retries failed, raise the last exception
        raise RequestException(f"API request failed after {retry_attempts} attempts. "
                             f"Last error: {str(last_exception)}")
    
    

    Usage examples:

    # Simple GET request
    try:
        response = make_api_request('https://api.example.com/data')
        print(response)
    except RequestException as e:
        print(f"Error: {e}")
    
    # POST request with JSON data
    data = {
        'name': 'John Doe',
        'email': 'john@example.com'
    }
    try:
        response = make_api_request(
            url='https://api.example.com/users',
            method='POST',
            json_data=data,
            headers={'Authorization': 'Bearer your-token-here'}
        )
        print(response)
    except RequestException as e:
        print(f"Error: {e}")
    
    # GET request with parameters and custom timeout
    params = {
        'page': 1,
        'limit': 10
    }
    try:
        response = make_api_request(
            url='https://api.example.com/users',
            params=params,
            timeout=60
        )
        print(response)
    except RequestException as e:
        print(f"Error: {e}")
    

    This function includes:

    1. Support for all HTTP methods (GET, POST, PUT, DELETE, etc.)
    2. Customizable headers, parameters, and request body
    3. Error handling with detailed logging
    4. Automatic retries with configurable attempts and delay
    5. SSL verification options
    6. Basic authentication support
    7. Timeout handling
    8. JSON response parsing with fallback to text
    9. Comprehensive logging
    10. Type hints for better code documentation

    Features:

    • Handles common HTTP errors
    • Retries failed requests
    • Supports both JSON and form data
    • Configurable timeout and SSL verification
    • Detailed logging for debugging
    • Type hints for better IDE support
    • Flexible authentication options

    You can customize this function further based on your specific needs by:

    • Adding custom error handling for specific status codes
    • Implementing different authentication methods
    • Adding request/response middleware
    • Implementing rate limiting
    • Adding response caching
    • Customizing logging format and levels

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.