Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.0 kB
3
Indexable
Never
import requests

account_id = ['']
bearer = ['']
worker_name = ['']
namespace_id = [''] # we generated when we created the namespace

for z in worker_name:
    for i, j, n in zip(bearer, account_id, namespace_id):
        headers1 = {
            'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryKvb9W2YnbTRjzSjH',
            'Authorization': 'Bearer '+i
        }
        data1 = '------WebKitFormBoundaryKvb9W2YnbTRjzSjH\r\nContent-Disposition: form-data; name="worker.js"; filename="worker.js"\r\nContent-Type: application/javascript+module\r\n\r\nexport default {\n  async fetch(request, env) {\n    return new Response("Hello world")\n  }\n}\r\n------WebKitFormBoundaryKvb9W2YnbTRjzSjH\r\nContent-Disposition: form-data; name="metadata"; filename="blob"\r\nContent-Type: application/json\r\n\r\n{"bindings":[{"name":"ENV","type":"kv_namespace","namespace_id":"'+n+'"}],"main_module":"worker.js"}\r\n------WebKitFormBoundaryKvb9W2YnbTRjzSjH--\r\n'
        params = {
            'include_subdomain_availability': 'true',
        }
        response1 = requests.put(
            'https://api.cloudflare.com/client/v4/accounts/'+j+'/workers/services/'+z+'/environments/production',
            headers=headers1,
            params=params,
            data=data1,
        )

        if response1.status_code == 200:
            print('Binding Created: ', response1.json())
        else:
            print('Error Binding KV:', response1.json())


#Response is
# {'result': {'created_on': '2023-05-05T10:36:44.797916Z', 'modified_on': '2023-05-05T10:40:41.700812Z', 'id': 'polished-butterfly-fbef', 'tag': '7dfc13eb824b4f22b468db9bdba3207c', 'tags': None, 'deployment_id': '2f92bf9994c84cf28b2c6f53ccd4647c', 'logpush': False, 'etag': 'b2b3bc64792efdd4d40fc96cfe82572e2495295312de0567e92e6bbd2fbd8324', 'handlers': ['fetch'], 'last_deployed_from': 'api', 'usage_model': 'bundled', 'available_on_subdomain': True}, 'success': True, 'errors': [], 'messages': []}