Source Code Snippets of "PUT.py"

Mike Dinder

The entire contents of the "PUT.py" file as code examples with syntax highlighting

Auto Generated Using Generate_HTML.py script to auto format each of these pages in a standardized template.

On smaller viewports/windows, please scroll or use your touch/finger to scroll left to right to view all the code displayed.

                                        # Running basic PUT Requests using a web platform called ReqRes (https://reqres.in/) to mock API Endpoints
# For me to hit via Python requests below.
import json
import os
import requests
import sys

from GET import get_books
from datetime import datetime
from dotenv import load_dotenv
from typing import Union
from zoneinfo import ZoneInfo


# Load Environment Variables
load_dotenv()


def update_book(id=None, payload={}, info_print=True) -> Union[dict, None]:
    """
    Takes in a ReqRes record ID and performs a full update of that record (PUT). Returns updated record.

    :param id: Int or String, number ID of the book to lookup and return the actual ID in ReqRes.
    :param payload: Dict, Payload to update.
    :param info_print: Boolean, Optional turn off print statements.
    :returns: Record of book updated or None
    :rtype: dict or None
    """

    if os.getenv('REQRES_TOKEN') is None or os.getenv('REQRES_TOKEN') == os.getenv('REQRES_DUMMY_VALUE'):
        print('You Need A Validated Token to Continue!')
        sys.exit()
    else:
        book_id = get_books(id, False, False)

        headers = {
            'Accept': 'application/json',
            'Authorization': f'Bearer {os.getenv("REQRES_TOKEN")}',
            'Connection': 'keep-alive',
            'x-api-key': os.getenv('X_API_PROD_KEY')
        }

        response = requests.put(
            os.getenv('REQRES_BASE_URL') + os.getenv('REQRES_COLLECTION_BOOKS') + '/' + book_id,
            json=payload,
            headers=headers
        )

        # Get JSON Response
        result = response.json()

        # Get Status Code
        status = response.status_code

        if status == 200:
            if info_print:
                print('Success! Book Updated')

            if 'data' in result:
                data = result['data']

                if info_print:
                    json_formatted_str = json.dumps(data, indent=2)
                    print(json_formatted_str)

                record = data['data']

                if info_print:
                    print('--------------------------------------------------')
                    print('Updated Record...')
                    print(record['title'])
                    print(record['price'])
                    print(record['genre'])
                    print(record['description'])
                    print(record['book_id'])
                    print(record['website'])
                    print(record['publisher_id'])
                    print(record['stock_quantity'])
                    print(record['publication_date'])
                    print('--------------------------------------------------')

                return record
            else:
                if info_print and 'message' in result:
                    print(result['message'])
        else:
            if info_print and 'message' in result:
                print(result['message'])

        return None


if __name__ == '__main__':
    tz = ZoneInfo('America/Phoenix')

    update_book(
        101,
        {
            'data': {
                'isbn': '978-1668009741',
                'genre': 'Updated Genre',  # <-- CHANGING THIS VALUE, ALL THE REST ARE THE SAME
                'price': '29.87',
                'title': 'King Arthur Baking Company\'s Big Book of Bread',
                'book_id': 101,
                'website': 'https://shop.kingarthurbaking.com/items/king-arthur-baking-companys-big-book-of-bread',
                'created_at': datetime(2026, 2, 2, 13, 40, 0, tzinfo=tz).strftime(os.getenv('STD_DATETIME_FMT')),
                'description': 'Master the art of bread baking with 125+ bread recipes for every baker\'s journey.',
                'publisher_id': 20001,
                'stock_quantity': 25,
                'publication_date': datetime(2024, 10, 22, 6, 0, 0, tzinfo=tz).strftime(os.getenv('STD_DATETIME_FMT'))
            }
        }
    )


                                        

Runs the entire script above. Open your Console Window in Inspect Code to view the output of the script above.

A prompt popup will ask you for various input() commands.

Python in Your Browser

Client-side execution via Pyodide · No server · Full stdlib · numpy, pandas, matplotlib available via micropip

Mobile users will want to use this method. Copy snippets or the entire script from above. You may also copy this script exactly as it is shown and you can run it on your own machine using the Command-Line Interface (CLI).

Loading Pyodide...
>>>
Enter run Shift+Enter new line ↑↓ history