Spaces:
Running
Running
File size: 1,721 Bytes
10660a7 9fcd7e5 5871ec6 9fcd7e5 1dc1ef1 015d1f8 6e829a6 c8302a1 6e829a6 9fcd7e5 |
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 |
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()
|