How to Expand Related Resources with a Mock Embed in Python

Simulate API response embedding by attaching mock embedded data to each related resource in a list using a simple Python class.

Easy Python 3.9+ Aug 9, 2026 API design & gRPC 15 views 0 copies

Python code

25 lines
Python 3.9+
import json

class EmbedMock:
    def __init__(self, resources):
        self.resources = resources

    def expand(self):
        for resource in self.resources:
            resource["embedded"] = self._generate_embed()

    def _generate_embed(self):
        return {
            "id": 1,
            "type": "mock",
            "data": {"message": "Expanded resource"}
        }

if __name__ == "__main__":
    resources = [
        {"name": "users", "url": "/api/users"},
        {"name": "posts", "url": "/api/posts"}
    ]
    embedder = EmbedMock(resources)
    embedder.expand()
    print(json.dumps(resources, indent=2))

Output

stdout
[
  {
    "name": "users",
    "url": "/api/users",
    "embedded": {
      "id": 1,
      "type": "mock",
      "data": {
        "message": "Expanded resource"
      }
    }
  },
  {
    "name": "posts",
    "url": "/api/posts",
    "embedded": {
      "id": 1,
      "type": "mock",
      "data": {
        "message": "Expanded resource"
      }
    }
  }
]

How it works

The EmbedMock class holds a list of resource dictionaries. The expand method iterates over each resource and inserts a new embedded key with mock data. _generate_embed returns a dictionary that simulates an expanded related resource. This pattern mirrors how APIs like JSON:API or GraphQL embeds relationships in responses. Printing with json.dumps with indent ensures the output is readable and shows the expanded structure.

Common mistakes

  • Mutating resources without deep copying can cause side effects in other parts of the code. Use `copy.deepcopy` if you need to preserve the original.
  • Assuming every resource has the same embedded structure; in real APIs, related resources differ by type.
  • Forgetting to handle missing resource keys when expanding in production code.
  • Overwriting existing 'embedded' keys without checking whether the resource already has related data.

Variations

  1. Use a dict comprehension to build a new list of resources with embedded data instead of modifying in place.
  2. Make `_generate_embed` accept a resource type to produce different mock embeds per relationship.

Real-world use cases

  • Mocking API responses in unit tests to verify your client handles embedded relationships correctly without a live server.
  • Prototyping a REST API endpoint that returns related resources inline, allowing frontend teams to develop against realistic data.
  • Generating sample payloads for API documentation or performance testing to simulate how embedded resources will look in production.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from API design & gRPC

Related tutorials and quizzes for this topic.