MCPcopy Create free account
hub / github.com/TextGeneratorio/text-generator.io / get_user_stripe_usage

Function get_user_stripe_usage

main.py:1026–1068  ·  view source on GitHub ↗

Get user data using cookie-based authentication (no usage tracking since we don't do metering)

(request: Request, db: Session = Depends(get_db))

Source from the content-addressed store, hash-verified

1024
1025@app.post("/api/get-user/stripe-usage")
1026async def get_user_stripe_usage(request: Request, db: Session = Depends(get_db)):
1027 """Get user data using cookie-based authentication (no usage tracking since we don't do metering)"""
1028 if USE_POSTGRES:
1029 try:
1030 user = get_current_user(request, db)
1031 if not user:
1032 raise HTTPException(status_code=401, detail="Not authenticated")
1033
1034 # Check subscription status
1035 subscription_item_id = get_subscription_item_id_for_user_email(user.email)
1036 user.is_subscribed = subscription_item_id is not None
1037
1038 if not user.is_subscribed:
1039 # recreate stripe customer if required - remediates users being created in test mode
1040 customer = None
1041 if user.stripe_id:
1042 try:
1043 customer = stripe.Customer.retrieve(user.stripe_id)
1044 except Exception:
1045 customer = None
1046 if not customer or not getattr(customer, 'id', None):
1047 customer = stripe.Customer.create( # type: ignore
1048 email=user.email,
1049 idempotency_key=user.email,
1050 )
1051 user.stripe_id = customer.id
1052 db.commit()
1053 db.refresh(user)
1054 set_session_for_user(user)
1055
1056 user.num_self_hosted_instances = get_self_hosted_subscription_count_for_user(user.stripe_id)
1057
1058 user_to_dict = user.to_dict()
1059 # No usage tracking anymore - we don't do metering
1060 return JSONResponse(user_to_dict)
1061 except HTTPException:
1062 raise
1063 except Exception as e:
1064 logger.error(f"Error getting user data: {e}")
1065 raise HTTPException(status_code=500, detail="Failed to get user data")
1066 else:
1067 # Fallback for non-PostgreSQL setup
1068 raise HTTPException(status_code=501, detail="User data not available without PostgreSQL")
1069
1070
1071# New PostgreSQL-based authentication endpoints

Callers

nothing calls this directly

Calls 6

get_current_userFunction · 0.90
set_session_for_userFunction · 0.90
createMethod · 0.80
to_dictMethod · 0.45

Tested by

no test coverage detected