Spaces:
Running
Running
import requests | |
import os | |
from dotenv import load_dotenv | |
import gradio as gr | |
# Load environment variables from .env file | |
load_dotenv() | |
# Replace with your actual Cloudflare API token | |
API_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN") | |
def get_cloudflare_accounts(): | |
if not API_TOKEN: | |
return "Please set the CLOUDFLARE_API_TOKEN environment variable" | |
# Cloudflare API endpoint for getting account details | |
url = "https://api.cloudflare.com/client/v4/accounts" | |
# Headers for the API request | |
headers = { | |
"Authorization": f"Bearer {API_TOKEN}", | |
"Content-Type": "application/json" | |
} | |
# Making the API request | |
response = requests.get(url, headers=headers) | |
# Checking if the request was successful | |
if response.status_code == 200: | |
# Parsing the JSON response | |
data = response.json() | |
if data['success']: | |
accounts = data['result'] | |
result = "" | |
for account in accounts: | |
account_id = account['id'] | |
account_name = account['name'] | |
result += f"Account Name: {account_name}, Account ID: {account_id}\n" | |
return result | |
else: | |
return f"Error fetching account details: {data['errors']}" | |
else: | |
return f"Failed to fetch account details. HTTP Status Code: {response.status_code}" | |
# Creating a Gradio interface | |
iface = gr.Interface(fn=get_cloudflare_accounts, | |
inputs=None, | |
outputs="text", | |
title="Cloudflare Account Details", | |
description="Fetch and display Cloudflare account details using the API.") | |
# Launch the interface | |
iface.launch() | |