Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 719c1f9

Browse files
committed
cleanup of version checking code for readability
1 parent 299ef93 commit 719c1f9

1 file changed

Lines changed: 34 additions & 17 deletions

File tree

examples/example_images_v2_direct_upload.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@
2525
# cli4 /accounts/:"${ACCOUNT}"/images/v1 | jq -r '.images[]|.id' | while read image_id ; do cli4 --delete /accounts/:"${ACCOUNT}"/images/v1/::$image_id ; done
2626
#
2727

28+
#
29+
# A note about version numbers.
30+
# this code works with 2.14.2 in a simple way
31+
# this code works with 2.18.0 is a simple way
32+
# released between then require at-least one paramater send via files= in order to not trigger a backend API bug
33+
#
34+
35+
def method_from_library_version():
36+
if CloudFlare.__version__ <= '2.14.2':
37+
print('Using %s version of Cloudflare python library - hence do not need data= or files=; but use files= if passing anything' % (CloudFlare.__version__))
38+
return ''
39+
if CloudFlare.__version__ <= '2.17.0':
40+
print('Using %s version of Cloudflare python library - hence must use files=' % (CloudFlare.__version__))
41+
return 'USE-FILES'
42+
# with newer library than 2.17.0 (i.e 2.18.0 and above) you should be able to pass just the data version
43+
print('Using %s version of Cloudflare python library - hence use data= as it is simpler' % (CloudFlare.__version__))
44+
return 'USE-DATA'
45+
2846
def doit(account_name, image_filename):
2947

3048
# https://developers.cloudflare.com/stream/uploading-videos/direct-creator-uploads/
@@ -75,25 +93,24 @@ def doit(account_name, image_filename):
7593
'size': image_filesize,
7694
}
7795

78-
# this code works with 2.14.2 in a simpler way
79-
8096
data = None
8197
files = None
82-
if CloudFlare.__version__ <= '2.14.2':
83-
print('Using %s version of Cloudflare python library - hence using neither data or files')
84-
else:
85-
# with newer library than 2.17.0 you should be able to pass just the data version
86-
print('Using %s version of Cloudflare python library - hence using %s' % (CloudFlare.__version__, 'data' if CloudFlare.__version__ > '2.17.0' else 'files'))
87-
if CloudFlare.__version__ > '2.17.0':
88-
data = {
89-
'metadata': json.dumps(metadata_values),
90-
'expiry': time_plus_one_hour_in_iso,
91-
}
92-
else:
93-
files = {
94-
('metadata', (None, json.dumps(metadata_values))),
95-
('expiry', (None, time_plus_one_hour_in_iso))
96-
}
98+
99+
lib_method = method_from_library_version()
100+
101+
if lib_method == 'USE-FILES':
102+
files = {
103+
('metadata', (None, json.dumps(metadata_values))),
104+
('expiry', (None, time_plus_one_hour_in_iso))
105+
}
106+
elif lib_method == 'USE-DATA':
107+
data = {
108+
'metadata': json.dumps(metadata_values),
109+
'expiry': time_plus_one_hour_in_iso,
110+
}
111+
elif lib_method == '':
112+
# optionally do nothing or send via files=
113+
pass
97114

98115
try:
99116
r = cf.accounts.images.v2.direct_upload.post(account_id, data=data, files=files)

0 commit comments

Comments
 (0)