base.py


Overview

The base.py file defines a foundational class named Base that serves as a generic container and interface for interacting with RESTful API endpoints via an underlying request abstraction (rag). This class allows hierarchical data structures—potentially nested dictionaries—to be wrapped into Base instances, providing convenient recursive access and serialization to JSON-compatible dictionaries.

The Base class also encapsulates standard HTTP methods (GET, POST, PUT, DELETE) by delegating these requests to the rag object provided upon initialization. This design suggests that Base acts as a lightweight client-side model, mapping API resources to Python objects with easy data manipulation and API interaction capabilities.


Detailed Description

Class: Base

Purpose

Base is designed to:


Constructor

def __init__(self, rag, res_dict):
rag = SomeRequestHandler()
response_data = {
    "id": 123,
    "name": "Example",
    "metadata": {
        "created": "2025-01-01",
        "tags": ["tag1", "tag2"]
    }
}
base_obj = Base(rag, response_data)

print(base_obj.name)  # Output: Example
print(base_obj.metadata.created)  # Output: 2025-01-01

Method: _update_from_dict

def _update_from_dict(self, rag, res_dict):

Method: to_json

def to_json(self):
json_dict = base_obj.to_json()
print(json_dict)
# Output:
# {
#   "id": 123,
#   "name": "Example",
#   "metadata": {
#       "created": "2025-01-01",
#       "tags": ["tag1", "tag2"]
#   }
# }

HTTP Wrapper Methods

Each method delegates the corresponding HTTP operation to the stored rag object.

post

def post(self, path, json=None, stream=False, files=None):
response = base_obj.post("/api/resource", json={"key": "value"})

get

def get(self, path, params=None):
response = base_obj.get("/api/resource", params={"id": 123})

rm (Delete)

def rm(self, path, json):
response = base_obj.rm("/api/resource", json={"id": 123})

put

def put(self, path, json):
response = base_obj.put("/api/resource", json={"name": "New Name"})

Method: __str__

def __str__(self):
print(str(base_obj))
# Output: '{"id": 123, "name": "Example", "metadata": {"created": "2025-01-01", "tags": ["tag1", "tag2"]}}'

Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class Base {
        -rag
        +__init__(rag, res_dict)
        -_update_from_dict(rag, res_dict)
        +to_json() dict
        +post(path, json=None, stream=False, files=None)
        +get(path, params=None)
        +rm(path, json)
        +put(path, json)
        +__str__()
    }

Summary

The base.py file provides a generalized, recursive data model class Base for representing API responses and requests as Python objects. It encapsulates nested data, provides JSON serialization, and delegates HTTP method calls to a request handler abstraction. This makes it a flexible foundation for API client implementations in the InfiniFlow project or similar systems.