atp-protocol 1.0.0__py3-none-any.whl → 1.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
atp/__init__.py CHANGED
@@ -1,11 +1,8 @@
1
- """ATP Protocol package."""
2
-
3
- from atp.middleware import (
4
- ATPSettlementMiddleware,
5
- create_settlement_middleware,
6
- )
1
+ from atp.middleware import ATPSettlementMiddleware, create_settlement_middleware
2
+ from atp.settlement_client import SettlementServiceClient
7
3
 
8
4
  __all__ = [
9
5
  "ATPSettlementMiddleware",
10
6
  "create_settlement_middleware",
11
- ]
7
+ "SettlementServiceClient",
8
+ ]
atp/config.py CHANGED
@@ -73,5 +73,5 @@ ATP_SOLANA_DEBUG = _bool_env("ATP_SOLANA_DEBUG", default=False)
73
73
 
74
74
  # Settlement Service URL
75
75
  ATP_SETTLEMENT_URL = os.getenv(
76
- "ATP_SETTLEMENT_URL", "http://localhost:8001"
76
+ "ATP_SETTLEMENT_URL", "https://facilitator.swarms.world"
77
77
  )
atp/middleware.py CHANGED
@@ -67,8 +67,6 @@ class ATPSettlementMiddleware(BaseHTTPMiddleware):
67
67
  recipient_pubkey: Optional[str] = None,
68
68
  skip_preflight: bool = False,
69
69
  commitment: str = "confirmed",
70
- usage_response_key: str = "usage",
71
- include_usage_in_response: bool = True,
72
70
  require_wallet: bool = True,
73
71
  settlement_service_url: Optional[str] = None,
74
72
  ):
@@ -92,8 +90,6 @@ class ATPSettlementMiddleware(BaseHTTPMiddleware):
92
90
  This wallet receives the main payment (after processing fee). Required.
93
91
  skip_preflight: Whether to skip preflight simulation for Solana transactions.
94
92
  commitment: Solana commitment level (processed|confirmed|finalized).
95
- usage_response_key: Key in response JSON where usage data is located (default: "usage").
96
- include_usage_in_response: Whether to add usage/cost info to the response.
97
93
  require_wallet: Whether to require wallet private key (if False, skips settlement when missing).
98
94
  settlement_service_url: Base URL of the settlement service. If not provided, uses
99
95
  ATP_SETTLEMENT_URL environment variable (default: http://localhost:8001).
@@ -119,8 +115,6 @@ class ATPSettlementMiddleware(BaseHTTPMiddleware):
119
115
  )
120
116
  self.skip_preflight = skip_preflight
121
117
  self.commitment = commitment
122
- self.usage_response_key = usage_response_key
123
- self.include_usage_in_response = include_usage_in_response
124
118
  self.require_wallet = require_wallet
125
119
  # Always use settlement service - initialize client with config value or provided URL
126
120
  service_url = (
@@ -146,10 +140,9 @@ class ATPSettlementMiddleware(BaseHTTPMiddleware):
146
140
  """
147
141
  Extract usage information from response body.
148
142
 
149
- Tries multiple strategies:
150
- 1. Look for usage data at the configured usage_response_key
151
- 2. Check if the entire response contains usage-like keys
152
- 3. Try nested structures (usage.usage, meta.usage, etc.)
143
+ Automatically detects usage data using multiple strategies:
144
+ 1. Check if the entire response contains usage-like keys
145
+ 2. Try nested structures with common usage key names (usage, token_usage, etc.)
153
146
 
154
147
  The usage data is then sent to the settlement service for parsing,
155
148
  so we just need to extract the raw usage object.
@@ -160,12 +153,7 @@ class ATPSettlementMiddleware(BaseHTTPMiddleware):
160
153
  return None
161
154
  data = json.loads(body_str)
162
155
 
163
- # Strategy 1: Try the configured usage key first
164
- usage = data.get(self.usage_response_key)
165
- if usage and isinstance(usage, dict):
166
- return usage
167
-
168
- # Strategy 2: Check if the entire response is usage-like
156
+ # Strategy 1: Check if the entire response is usage-like
169
157
  if isinstance(data, dict):
170
158
  # Check for common usage keys at top level
171
159
  usage_keys = [
@@ -182,7 +170,7 @@ class ATPSettlementMiddleware(BaseHTTPMiddleware):
182
170
  if any(key in data for key in usage_keys):
183
171
  return data
184
172
 
185
- # Strategy 3: Try nested structures
173
+ # Strategy 2: Try nested structures
186
174
  # Check for usage nested in common locations
187
175
  for nested_key in [
188
176
  "usage",
@@ -284,21 +272,20 @@ class ATPSettlementMiddleware(BaseHTTPMiddleware):
284
272
  detail=f"Settlement failed: {str(e)}",
285
273
  )
286
274
 
287
- # Modify response to include usage/payment info if requested
288
- if self.include_usage_in_response:
289
- try:
290
- response_data = json.loads(
291
- response_body.decode("utf-8")
292
- )
293
- response_data["atp_settlement"] = payment_result
294
- response_data["atp_usage"] = usage
295
- response_body = json.dumps(response_data).encode(
296
- "utf-8"
297
- )
298
- except Exception as e:
299
- logger.warning(
300
- f"Failed to add settlement info to response: {e}"
301
- )
275
+ # Always include settlement information in the response
276
+ try:
277
+ response_data = json.loads(
278
+ response_body.decode("utf-8")
279
+ )
280
+ response_data["atp_settlement"] = payment_result
281
+ response_data["atp_usage"] = usage
282
+ response_body = json.dumps(response_data).encode(
283
+ "utf-8"
284
+ )
285
+ except Exception as e:
286
+ logger.warning(
287
+ f"Failed to add settlement info to response: {e}"
288
+ )
302
289
 
303
290
  return Response(
304
291
  content=response_body,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: atp-protocol
3
- Version: 1.0.0
3
+ Version: 1.0.1
4
4
  Summary: ATP Protocol - Agentic Trade Protocol. The easiest way to enable agent-to-agent payments powered by Solana.
5
5
  License: MIT
6
6
  Keywords: atp,atp-protocol,solana,blockchain,cryptocurrency,payment-gated,agent-to-agent,agent payments,agent execution,payment settlement,solana payments,usdc,sol,fastapi,agent api,payment gateway,settlement service,agent marketplace,decentralized payments,web3,crypto payments,agent infrastructure,payment middleware,automated settlement
@@ -17,6 +17,7 @@ Classifier: Programming Language :: Python :: 3.13
17
17
  Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
18
18
  Classifier: Topic :: Office/Business :: Financial
19
19
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
20
+ Requires-Dist: fastapi
20
21
  Requires-Dist: httpx
21
22
  Requires-Dist: loguru
22
23
  Requires-Dist: pydantic
@@ -24,6 +25,8 @@ Requires-Dist: python-dotenv
24
25
  Requires-Dist: setuptools
25
26
  Requires-Dist: solana
26
27
  Requires-Dist: solders
28
+ Requires-Dist: starlette
29
+ Requires-Dist: swarms
27
30
  Project-URL: Documentation, https://github.com/The-Swarm-Corporation/ATP-Protocol
28
31
  Project-URL: Homepage, https://github.com/The-Swarm-Corporation/ATP-Protocol
29
32
  Project-URL: Repository, https://github.com/The-Swarm-Corporation/ATP-Protocol
@@ -303,8 +306,6 @@ app.add_middleware(
303
306
  recipient_pubkey="YourPublicKeyHere", # Required: endpoint host receives payment
304
307
  skip_preflight=False, # Skip Solana transaction preflight simulation
305
308
  commitment="confirmed", # Solana commitment level
306
- usage_response_key="usage", # Key in response where usage data is located
307
- include_usage_in_response=True, # Add usage/cost info to response
308
309
  require_wallet=True, # Require wallet key (if False, skips settlement when missing)
309
310
  )
310
311
  ```
@@ -319,8 +320,6 @@ app.add_middleware(
319
320
  - **`recipient_pubkey`** (required): Solana public key of the recipient wallet (endpoint host receives main payment)
320
321
  - **`skip_preflight`** (default: `False`): Whether to skip preflight simulation for Solana transactions
321
322
  - **`commitment`** (default: `"confirmed"`): Solana commitment level (`processed`|`confirmed`|`finalized`)
322
- - **`usage_response_key`** (default: `"usage"`): Key in response JSON where usage data is located
323
- - **`include_usage_in_response`** (default: `True`): Whether to add usage/cost info to the response
324
323
  - **`require_wallet`** (default: `True`): Whether to require wallet private key (if False, skips settlement when missing)
325
324
 
326
325
  ### Usage Example
@@ -409,10 +408,9 @@ The middleware automatically detects and parses usage data from multiple API for
409
408
  - **Generic**: `input_tokens`, `output_tokens`, `total_tokens`
410
409
  - **Nested**: `usage.prompt_tokens`, `meta.usage`, `statistics`, etc.
411
410
 
412
- The middleware searches for usage data in this order:
413
- 1. The configured `usage_response_key` (default: `"usage"`)
414
- 2. Top-level keys in the response
415
- 3. Nested structures (`usage`, `meta.usage`, `statistics`, etc.)
411
+ The middleware automatically detects usage data by:
412
+ 1. Checking if the entire response contains usage-like keys at the top level
413
+ 2. Searching nested structures with common usage key names (`usage`, `token_usage`, `meta`, `statistics`, etc.)
416
414
 
417
415
  ### Payment Flow
418
416
 
@@ -0,0 +1,9 @@
1
+ atp/__init__.py,sha256=fpKQQCi9HQEj4t2Rs5ng5qmp0KwGV6bZIl1tnS-3HQ8,251
2
+ atp/config.py,sha256=lYERbrySRTuyS_jeaAnLHYuBEQNLqaKLQTy_nEFP7lY,2041
3
+ atp/middleware.py,sha256=GlRBXEba_hlGMPi69LAGUJSQsKfmwSnAENKZQ9QcXhU,13185
4
+ atp/schemas.py,sha256=iASVBPpAhrO-LDXs2UC9ABtfV1oDYsu4kZcz3dVeJNA,6220
5
+ atp/settlement_client.py,sha256=jfvhxDqp2kDISWG7tjX5s88goAPxp-lMXvuJtpMyOys,6742
6
+ atp_protocol-1.0.1.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
7
+ atp_protocol-1.0.1.dist-info/METADATA,sha256=BdbWCKlFe31u-00ijy9aKpoIcSIpEwHvjJVS5n3GumA,18192
8
+ atp_protocol-1.0.1.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
9
+ atp_protocol-1.0.1.dist-info/RECORD,,