Saltar al contenido principal

Example Python Script


from zipfile import ZipFile
import snowflake.connector
import requests
import json
import os
import time


PARAMS = dict(
#snowflake connection parameters
)
conn = snowflake.connector.connect(**PARAMS)
cur = conn.cursor()

create_report_url = "https://openapi.doordash.com/dataexchange/v1/reports"
DQM_ENV = os.getenv("TOKEN", "default")

auth_header = "Bearer " + DQM_ENV
headers = {"Content-Type": "application/json; charset=utf-8", "Authorization": auth_header}

data = {
"business_ids": [],
"store_ids": [],
"end_date": "2022-08-03",
"start_date": "2022-08-01",
"report_type": "ORDER_DETAIL"
}

# create report API Post request
create_report_response = requests.post(create_report_url, headers=headers, json=data)
print("Status Code", create_report_response.status_code)
report_id = create_report_response.json()['report_id']
print("Report Id ", report_id)
get_report_url = "http://openapi.doordash.com/dataexchange/v1/reports/" + report_id +"/reportlink"


# get report API Get request
get_report_response = requests.get(get_report_url, headers=headers)
while get_report_response.json()['report_status'] != 'SUCCEEDED':
time.sleep(1)
get_report_response = requests.get(get_report_url, headers=headers)

report_link = get_report_response.json()['report_link']

# download report package to local
req = requests.get(report_link)
with open('/tmp/testreport.zip','wb') as output_file:
output_file.write(req.content)

# unpack and load data to Data Warehouse
with ZipFile('/tmp/testreport.zip', 'r') as zipObj:
listOfFileNames = zipObj.namelist()
for fileName in listOfFileNames:
print(fileName)
zipObj.extract(fileName)
# update this line with proper local and stage
q1 = "put file:///<current_directory>" + fileName + " @mystage/createreport/;"

q2 = """
create or replace file format customer_csv_ff
type = 'csv'
compression = 'GZIP'
field_delimiter = ','
FIELD_OPTIONALLY_ENCLOSED_BY='"'
skip_header = 1 ;
"""
q3 = "copy into order_details from @mystage/createreport/ FILE_FORMAT = 'custom_csv_ff'"

cur.execute(q1)
cur.execute(q2)
cur.execute(q3)


conn.close()