Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI,Request,HTTPException,Response | |
| from pydantic import BaseModel | |
| from fastapi.responses import JSONResponse | |
| from components.mongodbconnection import provideClient | |
| from components.checkaccountexists import checkAccount | |
| from components.endpointscheck import checkEndpoints | |
| from components.dynamicendpointscheck import checkDynamicEndpoints | |
| from components.updatedynamicendpoint import UpdateDynamicEndpoint | |
| from components.getinvoices import GetInvoices | |
| from components.updateendpointprice import UpdateEndpointPrice | |
| from components.updatedynamicendpointprice import UpdateDynamicEndpointPrice | |
| from openkitx403 import OpenKit403Middleware | |
| import uuid | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import json | |
| import base64 | |
| from components.checktoken import TokenCheck | |
| from components.middleware import x401Kit | |
| app=FastAPI() | |
| client=provideClient() | |
| origins=[ | |
| "http://localhost:3000", | |
| "http://localhost:3001" | |
| ] | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=origins, | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| ''' | |
| app.add_middleware( | |
| OpenKit403Middleware, | |
| audience="https://itsvelocity-velocity.hf.space/", | |
| issuer="my-api-v1", | |
| #token_gate=TokenCheck, | |
| excluded_paths=[ | |
| "/setupaccount", | |
| "/checkaccount", | |
| "/register_endpoint", | |
| "/register_dynamic_endpoint", | |
| "/delete_endpoint", | |
| "/delete_dynamic_endpoint", | |
| "/checkendpoints", | |
| "/checkdynamicendpoints", | |
| "/update_dynamic_endpoint", | |
| "/update_endpoint_price", | |
| "/update_dynamic_endpoint_price", | |
| "/docs", | |
| "/openapi.json", | |
| "/invoices" | |
| ] | |
| ) | |
| ''' | |
| app.add_middleware( | |
| x401Kit, | |
| protected_paths=[ | |
| "/custom_account_login" | |
| ], | |
| required_mint="3d4XyPWkUJzruF5c2qc1QfpLgsaNaDLMtTya1bWBpump", | |
| mint_amount=100000, | |
| helius_api_key="", | |
| geo_code=False, | |
| geo_code_locs=["IN"] | |
| ) | |
| db=client["x402ify_db"] | |
| coll=db["x402ify_col"] | |
| marketplace=db["x402marketplace"] | |
| sdkdb=db["sdklogs_db"] | |
| sdkcol=sdkdb["sdklogs_col"] | |
| class SetupPayload(BaseModel): | |
| owner:str | |
| class RegisterPayload(BaseModel): | |
| endpoint:str | |
| owner:str | |
| description:str | |
| meta:str | |
| price:str | |
| #pay_per_request:bool | |
| class RegisterPayloadDynamic(BaseModel): | |
| endpoint:str | |
| owner:str | |
| description:str | |
| tag:str | |
| meta:str | |
| price:str | |
| #pay_per_request:bool | |
| class RegisterAgent(BaseModel): | |
| agent:str | |
| owner:str | |
| description:str | |
| meta:str | |
| price:str | |
| class Update(BaseModel): | |
| owner:str | |
| tag:str | |
| newendpoint:str | |
| class Owner(BaseModel): | |
| owner:str | |
| class Delete(BaseModel): | |
| owner:str | |
| endpoint_linker:str | |
| class Invoice(BaseModel): | |
| owner:str | |
| class UpdatePrice(BaseModel): | |
| owner:str | |
| endpoint_linker:str | |
| price:str | |
| def saveToMarket(payload): | |
| marketplace.update_one( | |
| {"owner": "system"}, | |
| {"$push": {"market": payload}} | |
| ) | |
| def UpdateMarketData(endpoint_method,endpoint_linker,status_code): | |
| call_success = 200 <= status_code < 300 | |
| marketplace.update_one( | |
| { "owner": "system" }, | |
| { | |
| "$set": { | |
| "market.$[elem].endpoint_method":endpoint_method, | |
| }, | |
| "$inc": { | |
| "market.$[elem].calls": 1, | |
| "market.$[elem].failures": 0 if call_success else 1 | |
| } | |
| }, | |
| array_filters=[{ "elem.endpoint_linker": endpoint_linker }] | |
| ); | |
| #POST requests proxy | |
| #GET proxy with query params | |
| #'''post proxy tester''' | |
| async def Tester(request:Request): | |
| body=await request.json() | |
| return {"data":body.get("name")} | |
| def AccountLogin(request:Request): | |
| return {"message":"login"} | |
| def CustomAccountLogin(request:Request): | |
| return {"address":""} | |
| #'''Account Setup''' | |
| def SetupAccount(data:Owner): | |
| payload={ | |
| "owner":data.owner, | |
| "endpoints":[], | |
| "dynamicendpoints":[], | |
| "agents":[] | |
| } | |
| coll.insert_one(payload) | |
| return {"status":True} | |
| #'''Account Check ''' | |
| def checkaccount(data:Owner): | |
| status=checkAccount(data.owner) | |
| print(data) | |
| if status==True: | |
| return {'status':True} | |
| else: | |
| return {"status":False} | |
| #''' Regular Endpoint Registration ''' | |
| def RegisterEndpoint(data:RegisterPayload): | |
| try: | |
| document=coll.find_one({'owner':data.owner}) | |
| endpoints=document["endpoints"] | |
| if len(endpoints) > 0: | |
| for endpointobject in endpoints: | |
| if endpointobject["endpoint"]==data.endpoint: | |
| return {"status":False,"message":"endpoint already present"} | |
| short_id = str(uuid.uuid4())[:5] | |
| short_id_e=short_id+"x402" | |
| payload={ | |
| "owner":data.owner, | |
| "endpoint":data.endpoint, | |
| "meta":data.meta, | |
| "endpoint_linker":short_id_e, | |
| "price":data.price, | |
| "invoices":[] | |
| #"pay_per_request":data.pay_per_request | |
| } | |
| coll.update_one( | |
| {"owner": data.owner}, | |
| {"$push": {"endpoints": payload}} | |
| ) | |
| marketpayload={ | |
| "owner":data.owner, | |
| "endpoint_linker":short_id_e, | |
| "meta":data.meta, | |
| "description":data.description, | |
| "endpoint_method":"", | |
| "price":data.price, | |
| "endpoint_mcp_base":"https://mcpv100-production.up.railway.app/mcp:8080", | |
| "calls":0, | |
| "failures":0 | |
| } | |
| saveToMarket(marketpayload) | |
| return {"status":True,"x402ify_tag":short_id_e,"message":"done"} | |
| except Exception as e: | |
| return {"status":False,"x402ify_tag":str(e),"message":"error"} | |
| #''' Dynamic Endpoint Creation ''' | |
| async def RegisterDynamicEndpoint(data:RegisterPayloadDynamic): | |
| try: | |
| document=coll.find_one({'owner':data.owner}) | |
| endpoints=document["dynamicendpoints"] | |
| if len(endpoints) > 0: | |
| for endpointobject in endpoints: | |
| if endpointobject["endpoint"]==data.endpoint: | |
| return {"status":False,"message":"endpoint already present"} | |
| payload={ | |
| "owner":data.owner, | |
| "endpoint":data.endpoint, | |
| "description":data.description, | |
| "meta":data.meta, | |
| "endpoint_linker":f"{data.tag}", | |
| "price":data.price, | |
| "invoices":[] | |
| #"pay_per_request":data.pay_per_request | |
| } | |
| coll.update_one( | |
| {"owner": data.owner}, | |
| {"$push": {"dynamicendpoints": payload}} | |
| ) | |
| marketpayload={ | |
| "owner":data.owner, | |
| "endpoint_linker":short_id_e, | |
| "meta":data.meta, | |
| "description":data.description, | |
| "endpoint_method":"", | |
| "price":data.price, | |
| "endpoint_mcp_base":"https://mcpv100-production.up.railway.app/mcp:8080", | |
| "calls":0, | |
| "failures":0 | |
| } | |
| saveToMarket(marketpayload) | |
| return {"status":True,"x402ify_tag":f"{data.tag}","message":"done"} | |
| except Exception as e: | |
| return {"status":False,"x402ify_tag":str(e),"message":"error"} | |
| #'''Regular Endpoint Deletion''' | |
| def DeleteEndpoint(data:Delete): | |
| try: | |
| coll.update_one( | |
| {"owner": data.owner}, | |
| {"$pull": {"endpoints": {"endpoint_linker": data.endpoint_linker}}}) | |
| return {"status":True} | |
| except Exception as e: | |
| return {"status":False} | |
| #'''Dynamic Endpoint Deletion ''' | |
| def DeleteDynamicEndpoint(data:Delete): | |
| try: | |
| coll.update_one( | |
| {"owner": data.owner}, | |
| {"$pull": {"dynamicendpoints": {"endpoint_linker": data.endpoint_linker}}}) | |
| return {"status":True} | |
| except Exception as e: | |
| return {"status":False} | |
| #'''Regular Endpoint Fetching''' | |
| def checkEP(data:Owner): | |
| print(data.owner) | |
| endpoints=checkEndpoints(data.owner) | |
| return endpoints | |
| #''' Dynamic Endpoint Fetching ''' | |
| def checkDEP(data:Owner): | |
| dendpoints=checkDynamicEndpoints(data.owner) | |
| return dendpoints | |
| #''' Dynamic Endpoint Updation ''' | |
| def UpdateDynamic(data:Update): | |
| print(data.tag) | |
| status=UpdateDynamicEndpoint(data.owner,data.tag,data.newendpoint) | |
| return status | |
| def UpdateEndpointPriceRoute(data:UpdatePrice): | |
| status=UpdateEndpointPrice(data.owner,data.endpoint_linker,data.price) | |
| return status | |
| def UpdateDynamicEndpointPriceRoute(data:UpdatePrice): | |
| status=UpdateDynamicEndpointPrice(data.owner,data.endpoint_linker,data.price) | |
| return status | |
| #'''Fetch Invoices ''' | |
| def getaudit(data:Invoice): | |
| invoices=GetInvoices(data.owner) | |
| result = [] | |
| for item in invoices: | |
| owner = item.get("owner") | |
| endpoint = item.get("endpoint") | |
| endpoint_linker = item.get("endpoint_linker") | |
| signature = item.get("signature") | |
| #Navigate into nested fields | |
| payment = item["details"]["payment_request"] | |
| #Some entries use "accepts", some use flat structure | |
| if "accepts" in payment: | |
| pay_info = payment["accepts"][0] | |
| else: | |
| pay_info = payment | |
| payTo = pay_info.get("payTo") | |
| asset = pay_info.get("asset") | |
| amount = pay_info.get("maxAmountRequired") | |
| result.append({ | |
| "owner": owner, | |
| "endpoint_linker": endpoint_linker, | |
| "endpoint": endpoint, | |
| "signature": signature, | |
| "payTo": payTo, | |
| "asset": asset, | |
| "amount": amount | |
| }) | |
| return {"invoices":result} | |
| def MarketData(request:Request): | |
| marketdocument=marketplace.find_one({"owner":"system"}) | |
| marketdata=marketdocument["market"] | |
| return {"market":marketdata} | |
| #'''GET Endpoints Proxy endpoint''' | |
| def SDKLog(id): | |
| document=col.find_one({"owner":"system"}) | |
| details=None | |
| if document: | |
| logs=document["logs"] | |
| for data in logs: | |
| if data.signer==id: | |
| details=data | |
| return JSONResponse(content={"details":details}) | |