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

Commit 3621a4d

Browse files
committed
clean up and more
1 parent 282aa54 commit 3621a4d

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

examples/example_ai_translate.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import sys
55
import CloudFlare
66

7-
def translate_call(cf):
7+
def find_call(cf, verbs):
88
# So we walk over the @ via a getattr() call.
99
# We also have to deal with a . in a verb - that does not work in Python. So sad.
1010
# Also, the - is actually an _ in this Python library.
1111
# This is not normally needed for other calls
1212
m = cf
13-
for verb in 'accounts/ai/run/@cf/meta/m2m100_1.2b'.split('/'):
13+
for verb in verbs.split('/'):
1414
m = getattr(m, verb)
1515
return m
1616

@@ -19,7 +19,8 @@ def doit(account_name, english_text):
1919
# Or place these in your cloudflare.cfg file
2020
os.environ['CLOUDFLARE_API_EXTRAS'] = '/accounts/:id/ai/run/@cf/meta/m2m100-1.2b'
2121

22-
cf = CloudFlare.CloudFlare()
22+
# We set the timeout because these AI calls take longer than normal API calls
23+
cf = CloudFlare.CloudFlare(global_request_timeout=120)
2324

2425
try:
2526
params = {'name': account_name, 'per_page': 1}
@@ -35,13 +36,11 @@ def doit(account_name, english_text):
3536
'target_lang': 'french',
3637
}
3738

38-
# This should be easy to call; however, the @ will not work in Python (or many languages)
39-
# r = [email protected](account_id, data=translate_data)
40-
41-
the_call = translate_call(cf)
42-
4339
try:
44-
r = the_call.post(account_id, data=translate_data)
40+
# This should be easy to call; however, the @ will not work in Python (or many languages)
41+
# r = [email protected](account_id, data=translate_data)
42+
# We find the endpoint via a quick string search
43+
r = find_call(cf, 'accounts/ai/run/@cf/meta/m2m100_1.2b').post(account_id, data=translate_data)
4544
except CloudFlare.exceptions.CloudFlareAPIError as e:
4645
exit('/ai.run %d %s - api call failed' % (e, e))
4746

0 commit comments

Comments
 (0)