Last active
August 25, 2022 23:28
-
-
Save ojmccall/82744a853857c9f460faf9725289f246 to your computer and use it in GitHub Desktop.
python for CF to run SQL and post to SFMC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import psycopg2 | |
import pandas as pd | |
import requests | |
#DB connection | |
dbname = "DB Name here" | |
host = "host info here - eg your redshift instance host" | |
port = "5439" | |
user = "username here" | |
password = "password here" | |
con=psycopg2.connect(dbname= dbname, host=host, | |
port= port, user= user, password= password) | |
#run sql on db and pass into a dataframe | |
sql = "your SQL syntax here" | |
df = pd.read_sql(sql,con) | |
datarows = df.to_dict(orient="records") | |
#sfmc info | |
client_id = "sfmc client id" | |
client_secret = "sfmc secret" | |
baseURL = "unique substring in your sfmc app endpoint" | |
deKey = " External key to your target DE" | |
url = "https://"+baseURL+".auth.marketingcloudapis.com/v2/Token" | |
data = {"grant_type":"client_credentials", | |
"client_id":client_id, | |
"client_secret":client_secret | |
} | |
r = requests.post(url, data=data) | |
print("posted") | |
body = json.loads(r.content) | |
token = body["access_token"] | |
postRequests = 0 | |
# Create arrays of rows for API payload of 1000 rows per request, change this value in the arraybuilder function if needed | |
if len(datarows) >0: | |
def arraybuilder(seq, size): | |
return (seq[pos:pos + size] for pos in range(0, len(seq), size)) | |
for group in arraybuilder(datarows, 1000) | |
payload = {"items":group} | |
head = {"Authorization": "Bearer " + token} | |
url = f"https://{baseURL}.rest.marketingcloudapis.com/data/v1/async/dataextensions/key:{deKey}/rows" | |
response = requests.post(url, json=payload, headers=head) | |
#incremement number of posts made during function | |
postRequests += 1 | |
print("total API posts: "+str(postRequests)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment