1- """ Cloudflare v4 API"""
1+ """ Cloudflare v4 API
2+
3+ A Python interface Cloudflare's v4 API.
4+
5+ See README.md for detailed/further reading.
6+
7+ Copyright (c) 2016 thru 2024, Cloudflare. All rights reserved.
8+ """
9+
210import json
311import keyword
412
1826DEFAULT_MAX_REQUEST_RETRIES = 5
1927
2028class CloudFlare ():
21- """ Cloudflare v4 API"""
29+ """ A Python interface Cloudflare's v4 API.
30+
31+ :param email: Authentication email (if not provided by config methods).
32+ :param key: Authentication key (if not provided by config methods).
33+ :param token: Authentication token (if not provided by config methods).
34+ :param certtoken: Authentication certtoken (if not provided by config methods).
35+ :param debug: Debug is enabled by setting to True.
36+ :param raw: Set to True to force raw responses so you can see paging.
37+ :param use_sessions: The default is True; rarely needs changing.
38+ :param profile: Profile name (default is "CloudFlare").
39+ :param base_url: Rarely changed Cloudflare API URL.
40+ :param global_request_timeout: Timeout value (default is 5 seconds).
41+ :param max_request_retries: Number of retry times (default is 5 times).
42+ :param http_headers: Additional HTTP headers (as a list).
43+ :return: New instance of CloudFlare()
44+
45+ A Python interface Cloudflare's v4 API.
46+ """
2247
2348 class _v4base ():
24- """ Cloudflare v4 API """
49+ """ :meta private: """
2550
2651 def __init__ (self , config ):
27- """ Cloudflare v4 API """
52+ """ :meta private: """
2853
2954 self .network = None
3055 self .config = config
@@ -684,7 +709,7 @@ def _read_from_web(self, url):
684709 return response .text
685710
686711 class _CFbase ():
687- """ Cloudflare v4 API """
712+ """ :meta private: """
688713
689714 def __init__ (self , base , parts , content_type = None ):
690715 """ Cloudflare v4 API"""
@@ -762,7 +787,7 @@ def delete(self, identifier1=None, identifier2=None, identifier3=None, identifie
762787 raise CloudFlareAPIError (e = e ) from None
763788
764789 class _CFbaseUnused (_CFbase ):
765- """ Cloudflare v4 API """
790+ """ :meta private: """
766791
767792 def __init__ (self , base , parts , content_type ):
768793 """ Cloudflare v4 API"""
@@ -771,7 +796,7 @@ def __init__(self, base, parts, content_type):
771796 self ._do = self ._base .do_not_available
772797
773798 class _CFbaseNoAuth (_CFbase ):
774- """ Cloudflare v4 API """
799+ """ :meta private: """
775800
776801 def __init__ (self , base , parts , content_type ):
777802 """ Cloudflare v4 API"""
@@ -821,7 +846,7 @@ def delete(self, identifier1=None, identifier2=None, identifier3=None, identifie
821846 raise CloudFlareAPIError (e = e ) from None
822847
823848 class _CFbaseAuth (_CFbase ):
824- """ Cloudflare v4 API """
849+ """ :meta private: """
825850
826851 def __init__ (self , base , parts , content_type ):
827852 """ Cloudflare v4 API"""
@@ -831,7 +856,7 @@ def __init__(self, base, parts, content_type):
831856 self ._valid = True
832857
833858 class _CFbaseAuthUnwrapped (_CFbase ):
834- """ Cloudflare v4 API """
859+ """ :meta private: """
835860
836861 def __init__ (self , base , parts , content_type ):
837862 """ Cloudflare v4 API"""
@@ -841,7 +866,7 @@ def __init__(self, base, parts, content_type):
841866 self ._valid = True
842867
843868 class _CFbaseAuthCert (_CFbase ):
844- """ Cloudflare v4 API """
869+ """ :meta private: """
845870
846871 def __init__ (self , base , parts , content_type ):
847872 """ Cloudflare v4 API"""
@@ -868,7 +893,18 @@ def sanitize_verb(cls, v):
868893 return v
869894
870895 def add (self , t , p1 , p2 = None , p3 = None , p4 = None , p5 = None , content_type = None ):
871- """ add api call to class"""
896+ """ add()
897+
898+ :param t: type of API call.
899+ :param p1: part1 of API call.
900+ :param p2: part1 of API call.
901+ :param p3: part1 of API call.
902+ :param p4: part1 of API call.
903+ :param p5: part1 of API call.
904+ :param content_type: optional value for the HTTP Content-Type for an API call.
905+
906+ add() is the core fuction that creates a new API endpoint that can be called later on.
907+ """
872908
873909 a = []
874910 if p1 :
@@ -923,7 +959,13 @@ def add(self, t, p1, p2=None, p3=None, p4=None, p5=None, content_type=None):
923959 setattr (branch , CloudFlare .sanitize_verb (name ), f )
924960
925961 def find (self , cmd ):
926- """ find """
962+ """ find()
963+
964+ :param cmd: API in slash format
965+ :return: fuction to call for that API
966+
967+ You can use this call to convert a string API command into the actual function call
968+ """
927969 m = self
928970 for verb in cmd .split ('/' ):
929971 if verb == '' or verb [0 ] == ':' :
@@ -935,11 +977,16 @@ def find(self, cmd):
935977 return m
936978
937979 def api_list (self ):
938- """ recursive walk of the api tree returning a list of api calls"""
980+ """ api_list()
981+
982+ :return: list of API calls
983+
984+ A recursive walk of the api tree returning a list of api calls
985+ """
939986 return self ._api_list (m = self )
940987
941988 def _api_list (self , m = None , s = '' ):
942- """ recursive walk of the api tree returning a list of api calls """
989+ """ :meta private: """
943990 w = []
944991 for n in sorted (dir (m )):
945992 if n [0 ] == '_' :
@@ -974,12 +1021,16 @@ def _api_list(self, m=None, s=''):
9741021 return w
9751022
9761023 def api_from_openapi (self , url = None ):
977- """ Cloudflare v4 API"""
1024+ """ api_from_openapi()
1025+
1026+ :param url: OpenAPI URL or None if you use the built official URL
1027+
1028+ """
9781029
9791030 return self ._base .api_from_openapi (url )
9801031
9811032 def __init__ (self , email = None , key = None , token = None , certtoken = None , debug = False , raw = False , use_sessions = True , profile = None , base_url = None , global_request_timeout = None , max_request_retries = None , http_headers = None ):
982- """ Cloudflare v4 API """
1033+ """ :meta private: """
9831034
9841035 self ._base = None
9851036
@@ -1050,30 +1101,30 @@ def __init__(self, email=None, key=None, token=None, certtoken=None, debug=False
10501101 raise e
10511102
10521103 def __del__ (self ):
1053- """ Network for Cloudflare API """
1104+ """ :meta private: """
10541105
10551106 if self ._base :
10561107 del self ._base
10571108 self ._base = None
10581109
10591110 def __call__ (self ):
1060- """ Cloudflare v4 API """
1111+ """ :meta private: """
10611112
10621113 raise TypeError ('object is not callable' )
10631114
10641115 def __enter__ (self ):
1065- """ Cloudflare v4 API """
1116+ """ :meta private: """
10661117 return self
10671118
10681119 def __exit__ (self , t , v , tb ):
1069- """ Cloudflare v4 API """
1120+ """ :meta private: """
10701121 if t is None :
10711122 return True
10721123 # pretend we didn't deal with raised error - which is true
10731124 return False
10741125
10751126 def __str__ (self ):
1076- """ Cloudflare v4 API """
1127+ """ :meta private: """
10771128
10781129 if self ._base .api_email is None :
10791130 s = '["%s","%s"]' % (self ._base .profile , 'REDACTED' )
@@ -1082,7 +1133,7 @@ def __str__(self):
10821133 return s
10831134
10841135 def __repr__ (self ):
1085- """ Cloudflare v4 API """
1136+ """ :meta private: """
10861137
10871138 if self ._base .api_email is None :
10881139 s = '%s,%s("%s","%s","%s","%s",%s,"%s")' % (
@@ -1099,10 +1150,22 @@ def __repr__(self):
10991150 return s
11001151
11011152 def __getattr__ (self , key ):
1102- """ __getattr__ """
1153+ """ :meta private: """
11031154
11041155 # this code will expand later
11051156 if key in dir (self ):
11061157 return self [key ]
11071158 # this is call to a non-existent endpoint
11081159 raise AttributeError (key )
1160+
1161+ class Cloudflare (CloudFlare ):
1162+ """ A Python interface Cloudflare's v4 API.
1163+
1164+ Alternate upper/lowercase version.
1165+ """
1166+
1167+ class cloudflare (CloudFlare ):
1168+ """ A Python interface Cloudflare's v4 API.
1169+
1170+ Alternate upper/lowercase version.
1171+ """
0 commit comments