Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,32 +9,38 @@ load_dotenv()
|
|
9 |
# Replace with your actual Cloudflare API token
|
10 |
API_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN")
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
if
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
else:
|
35 |
-
|
36 |
-
else:
|
37 |
-
print("Failed to fetch account details. HTTP Status Code:", response.status_code)
|
38 |
|
39 |
# Creating a Gradio interface
|
40 |
iface = gr.Interface(fn=get_cloudflare_accounts,
|
|
|
9 |
# Replace with your actual Cloudflare API token
|
10 |
API_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN")
|
11 |
|
12 |
+
def get_cloudflare_accounts():
|
13 |
+
if not API_TOKEN:
|
14 |
+
return "Please set the CLOUDFLARE_API_TOKEN environment variable"
|
15 |
+
|
16 |
+
# Cloudflare API endpoint for getting account details
|
17 |
+
url = "https://api.cloudflare.com/client/v4/accounts"
|
18 |
+
|
19 |
+
# Headers for the API request
|
20 |
+
headers = {
|
21 |
+
"Authorization": f"Bearer {API_TOKEN}",
|
22 |
+
"Content-Type": "application/json"
|
23 |
+
}
|
24 |
+
|
25 |
+
# Making the API request
|
26 |
+
response = requests.get(url, headers=headers)
|
27 |
+
|
28 |
+
# Checking if the request was successful
|
29 |
+
if response.status_code == 200:
|
30 |
+
# Parsing the JSON response
|
31 |
+
data = response.json()
|
32 |
+
if data['success']:
|
33 |
+
accounts = data['result']
|
34 |
+
result = ""
|
35 |
+
for account in accounts:
|
36 |
+
account_id = account['id']
|
37 |
+
account_name = account['name']
|
38 |
+
result += f"Account Name: {account_name}, Account ID: {account_id}\n"
|
39 |
+
return result
|
40 |
+
else:
|
41 |
+
return f"Error fetching account details: {data['errors']}"
|
42 |
else:
|
43 |
+
return f"Failed to fetch account details. HTTP Status Code: {response.status_code}"
|
|
|
|
|
44 |
|
45 |
# Creating a Gradio interface
|
46 |
iface = gr.Interface(fn=get_cloudflare_accounts,
|