forked from cloudflare/python-cloudflare
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added code to query api documentation (via beautifulsoup4) to build a…
… current api listing
- Loading branch information
Showing
6 changed files
with
119 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" API extras for Cloudflare API""" | ||
|
||
from bs4 import BeautifulSoup, Comment | ||
|
||
API_TYPES = ['GET', 'POST', 'PATCH', 'PUT', 'DELETE'] | ||
|
||
def do_section(section): | ||
""" API extras for Cloudflare API""" | ||
|
||
cmds = [] | ||
# look for deprecated first in section | ||
deprecated = False | ||
for tag2 in section.find_all('h3'): | ||
if 'Deprecation Warning' in str(tag2): | ||
deprecated = True | ||
# look for all API calls in section | ||
for tag2 in section.find_all('pre'): | ||
cmd = [] | ||
for child in tag2.children: | ||
if isinstance(child, Comment): | ||
# remove <!-- react-text ... -> parts | ||
continue | ||
cmd.append(child.strip()) | ||
if len(cmd) == 0: | ||
continue | ||
action = cmd[0] | ||
cmd = '/' + ''.join(cmd[1:]) | ||
if action == '' or action not in API_TYPES: | ||
continue | ||
v = {'deprecated': deprecated, 'action': action, 'cmd': cmd} | ||
cmds.append(v) | ||
return cmds | ||
|
||
def api_decode_from_web(content): | ||
""" API extras for Cloudflare API""" | ||
|
||
soup = BeautifulSoup(content, 'html.parser') | ||
|
||
all_cmds = [] | ||
for section in soup.find_all('section'): | ||
all_cmds += do_section(section) | ||
|
||
return sorted(all_cmds, key=lambda v: v['cmd']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env python | ||
"""Cloudflare API code - example""" | ||
|
||
from __future__ import print_function | ||
|
||
import os | ||
import sys | ||
import json | ||
|
||
sys.path.insert(0, os.path.abspath('..')) | ||
import CloudFlare | ||
|
||
def main(): | ||
"""Cloudflare API code - example""" | ||
|
||
cf = CloudFlare.CloudFlare() | ||
try: | ||
r = cf.api_from_web() | ||
except Exception as e: | ||
exit('api_from_web: - %s - api call connection failed' % (e)) | ||
|
||
print(json.dumps(r)) | ||
exit(0) | ||
|
||
if __name__ == '__main__': | ||
main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ requests>=2.4.2 | |
future | ||
pyyaml | ||
jsonlines | ||
beautifulsoup4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters