Repository navigation
Expand file tree
/
Copy pathbazaar.py
More file actions
81 lines (66 loc) · 2.58 KB
/
Copy pathbazaar.py
File metadata and controls
81 lines (66 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Bazaar catalog for storing discovered x402 resources.
This module provides a simple in-memory catalog for discovered resources during e2e testing
"""
from typing import Any
from x402.extensions.bazaar import DiscoveryResource
class BazaarCatalog:
"""Catalog for storing discovered x402 resources."""
def __init__(self) -> None:
self._resources: dict[str, DiscoveryResource] = {}
def add(self, resource: DiscoveryResource) -> None:
"""Add a discovered resource to the catalog."""
print(f"📝 Discovered resource: {resource.resource}")
print(f" x402 Version: {resource.x402_version}")
if resource.service_name is not None:
print(f" Service: {resource.service_name}")
if resource.tags is not None:
print(f" Tags: {', '.join(resource.tags)}")
self._resources[resource.resource] = resource
def get_resources(self, limit: int = 100, offset: int = 0) -> dict[str, Any]:
"""Get paginated list of discovered resources."""
all_resources = list(self._resources.values())
total = len(all_resources)
items = all_resources[offset : offset + limit]
return {
"x402Version": 2,
"items": [r.to_dict() for r in items],
"pagination": {
"limit": limit,
"offset": offset,
"total": total,
},
}
def search_resources(
self,
query: str,
resource_type: str | None = None,
limit: int | None = None,
) -> dict[str, Any]:
"""Search resources using case-insensitive keyword matching."""
needle = query.lower()
results = []
for r in self._resources.values():
haystack = " ".join(
[
r.resource,
r.type,
r.description or "",
r.service_name or "",
*(r.tags or []),
*[str(v) for v in (r.extensions or {}).values()],
]
).lower()
if needle in haystack:
results.append(r)
if resource_type:
results = [r for r in results if r.type == resource_type]
items = results[:limit] if limit is not None else results
return {
"x402Version": 2,
"resources": [r.to_dict() for r in items],
"partialResults": False,
"pagination": None,
}
def get_count(self) -> int:
"""Get total count of discovered resources."""
return len(self._resources)