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
+
2
10
import json
3
11
import keyword
4
12
18
26
DEFAULT_MAX_REQUEST_RETRIES = 5
19
27
20
28
class 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
+ """
22
47
23
48
class _v4base ():
24
- """ Cloudflare v4 API """
49
+ """ :meta private: """
25
50
26
51
def __init__ (self , config ):
27
- """ Cloudflare v4 API """
52
+ """ :meta private: """
28
53
29
54
self .network = None
30
55
self .config = config
@@ -684,7 +709,7 @@ def _read_from_web(self, url):
684
709
return response .text
685
710
686
711
class _CFbase ():
687
- """ Cloudflare v4 API """
712
+ """ :meta private: """
688
713
689
714
def __init__ (self , base , parts , content_type = None ):
690
715
""" Cloudflare v4 API"""
@@ -762,7 +787,7 @@ def delete(self, identifier1=None, identifier2=None, identifier3=None, identifie
762
787
raise CloudFlareAPIError (e = e ) from None
763
788
764
789
class _CFbaseUnused (_CFbase ):
765
- """ Cloudflare v4 API """
790
+ """ :meta private: """
766
791
767
792
def __init__ (self , base , parts , content_type ):
768
793
""" Cloudflare v4 API"""
@@ -771,7 +796,7 @@ def __init__(self, base, parts, content_type):
771
796
self ._do = self ._base .do_not_available
772
797
773
798
class _CFbaseNoAuth (_CFbase ):
774
- """ Cloudflare v4 API """
799
+ """ :meta private: """
775
800
776
801
def __init__ (self , base , parts , content_type ):
777
802
""" Cloudflare v4 API"""
@@ -821,7 +846,7 @@ def delete(self, identifier1=None, identifier2=None, identifier3=None, identifie
821
846
raise CloudFlareAPIError (e = e ) from None
822
847
823
848
class _CFbaseAuth (_CFbase ):
824
- """ Cloudflare v4 API """
849
+ """ :meta private: """
825
850
826
851
def __init__ (self , base , parts , content_type ):
827
852
""" Cloudflare v4 API"""
@@ -831,7 +856,7 @@ def __init__(self, base, parts, content_type):
831
856
self ._valid = True
832
857
833
858
class _CFbaseAuthUnwrapped (_CFbase ):
834
- """ Cloudflare v4 API """
859
+ """ :meta private: """
835
860
836
861
def __init__ (self , base , parts , content_type ):
837
862
""" Cloudflare v4 API"""
@@ -841,7 +866,7 @@ def __init__(self, base, parts, content_type):
841
866
self ._valid = True
842
867
843
868
class _CFbaseAuthCert (_CFbase ):
844
- """ Cloudflare v4 API """
869
+ """ :meta private: """
845
870
846
871
def __init__ (self , base , parts , content_type ):
847
872
""" Cloudflare v4 API"""
@@ -868,7 +893,18 @@ def sanitize_verb(cls, v):
868
893
return v
869
894
870
895
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
+ """
872
908
873
909
a = []
874
910
if p1 :
@@ -923,7 +959,13 @@ def add(self, t, p1, p2=None, p3=None, p4=None, p5=None, content_type=None):
923
959
setattr (branch , CloudFlare .sanitize_verb (name ), f )
924
960
925
961
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
+ """
927
969
m = self
928
970
for verb in cmd .split ('/' ):
929
971
if verb == '' or verb [0 ] == ':' :
@@ -935,11 +977,16 @@ def find(self, cmd):
935
977
return m
936
978
937
979
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
+ """
939
986
return self ._api_list (m = self )
940
987
941
988
def _api_list (self , m = None , s = '' ):
942
- """ recursive walk of the api tree returning a list of api calls """
989
+ """ :meta private: """
943
990
w = []
944
991
for n in sorted (dir (m )):
945
992
if n [0 ] == '_' :
@@ -974,12 +1021,16 @@ def _api_list(self, m=None, s=''):
974
1021
return w
975
1022
976
1023
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
+ """
978
1029
979
1030
return self ._base .api_from_openapi (url )
980
1031
981
1032
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: """
983
1034
984
1035
self ._base = None
985
1036
@@ -1050,30 +1101,30 @@ def __init__(self, email=None, key=None, token=None, certtoken=None, debug=False
1050
1101
raise e
1051
1102
1052
1103
def __del__ (self ):
1053
- """ Network for Cloudflare API """
1104
+ """ :meta private: """
1054
1105
1055
1106
if self ._base :
1056
1107
del self ._base
1057
1108
self ._base = None
1058
1109
1059
1110
def __call__ (self ):
1060
- """ Cloudflare v4 API """
1111
+ """ :meta private: """
1061
1112
1062
1113
raise TypeError ('object is not callable' )
1063
1114
1064
1115
def __enter__ (self ):
1065
- """ Cloudflare v4 API """
1116
+ """ :meta private: """
1066
1117
return self
1067
1118
1068
1119
def __exit__ (self , t , v , tb ):
1069
- """ Cloudflare v4 API """
1120
+ """ :meta private: """
1070
1121
if t is None :
1071
1122
return True
1072
1123
# pretend we didn't deal with raised error - which is true
1073
1124
return False
1074
1125
1075
1126
def __str__ (self ):
1076
- """ Cloudflare v4 API """
1127
+ """ :meta private: """
1077
1128
1078
1129
if self ._base .api_email is None :
1079
1130
s = '["%s","%s"]' % (self ._base .profile , 'REDACTED' )
@@ -1082,7 +1133,7 @@ def __str__(self):
1082
1133
return s
1083
1134
1084
1135
def __repr__ (self ):
1085
- """ Cloudflare v4 API """
1136
+ """ :meta private: """
1086
1137
1087
1138
if self ._base .api_email is None :
1088
1139
s = '%s,%s("%s","%s","%s","%s",%s,"%s")' % (
@@ -1099,10 +1150,22 @@ def __repr__(self):
1099
1150
return s
1100
1151
1101
1152
def __getattr__ (self , key ):
1102
- """ __getattr__ """
1153
+ """ :meta private: """
1103
1154
1104
1155
# this code will expand later
1105
1156
if key in dir (self ):
1106
1157
return self [key ]
1107
1158
# this is call to a non-existent endpoint
1108
1159
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