From 790a38a97042d0b5e05778747845fdb3b17ac2bb Mon Sep 17 00:00:00 2001 From: Pavak Paul Date: Fri, 27 May 2022 01:47:43 +0530 Subject: [PATCH] verify access and list zones --- .gitignore | 3 ++- apicalls.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ app.py | 6 +++++- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 apicalls.py diff --git a/.gitignore b/.gitignore index dab06f1..01939c0 100644 --- a/.gitignore +++ b/.gitignore @@ -168,4 +168,5 @@ cython_debug/ # Project setup -config.json \ No newline at end of file +config.json +response-example* \ No newline at end of file diff --git a/apicalls.py b/apicalls.py new file mode 100644 index 0000000..bb16966 --- /dev/null +++ b/apicalls.py @@ -0,0 +1,51 @@ +import requests + +def getHeaders(credentials): + headers = {'Authorization': f'Bearer {credentials["api_key"]}', 'X-Auth-Email': f'{credentials["email"]}', + 'Content-Type': 'application/json'} + return headers + +def errorChecker(r): + if r.status_code != 200: + print(f'Error {r.status_code}.\nDetails:\n{r.json()}') + assert False + +def verifyCredentials(credentials): + """ +curl -X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" \ + -H "Authorization: Bearer " \ + -H "Content-Type:application/json" + """ + headers = getHeaders(credentials) + r = requests.get('https://api.cloudflare.com/client/v4/user/tokens/verify', headers=headers) + errorChecker(r) + return r + + +def listAllZones(credentials): + """ +curl -X GET "https://api.cloudflare.com/client/v4/zones \ + -H "X-Auth-Email: user@example.com" \ + -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \ + -H "Content-Type: application/json" + """ + headers = getHeaders(credentials) + zones = requests.get('https://api.cloudflare.com/client/v4/zones', headers= headers) + return zones + + +def getAllDNS(credentials): + """ +curl -X GET "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/ \ + -H "X-Auth-Email: user@example.com" \ + -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \ + -H "Content-Type: application/json" + """ + +def deleteARecord(credentials): + """ +curl -X DELETE "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records/372e67954025e0ba6aaa6d586b9e0b59" \ + -H "X-Auth-Email: user@example.com" \ + -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \ + -H "Content-Type: application/json" + """ \ No newline at end of file diff --git a/app.py b/app.py index dd4cd7d..3d70c8b 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,7 @@ import json from sys import argv from pathlib import Path import requests +from apicalls import * # Get API key and Email address @@ -24,4 +25,7 @@ if len(argv) > 1 and 'loadconfig' in argv: else: credentials = getCredentials() -# print(credentials) \ No newline at end of file +# print(credentials) + +zones = listAllZones(credentials).json()["result"] +# print(f"{zones.status_code}\n{zones.json()}")