I'm trying to recreate this CURL request in Python and get it to loop through a CSV.
curl -X "DELETE" --user 'api:<my-api-key>' \ https://api.mailgun.net/v3/messages.thewebsite/unsubscribes \
-F address='[email protected]'
I'm thinking I should create a function and return the result. I'm using the requests package along with CSV but getting the output of
<function remove_suppression at 0x7fb3935baf70> printed to the console. What am I missing here?
Code sample below.
Import csv
Import requests
def remove_suppression():
return requests.delete(
"https://api.mailgun.net/v3/messages.thewebsite.com/unsubscribes",
auth=("api", "key-abcdefg12345789gbb"),
data={'address':'row[1]'})
with open('/Users/brett/Downloads/example.csv',newline='') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
for row in readCSV:
try:
remove_suppression()
print("=========================")
print(remove_suppression.text)
sleep(5)
except:
#write log to a text file
CodePudding user response:
You need to pass the row
value as a parameter to remove_supression
. Currently you're printing out the value of remove_suppression
, which is a function.
You should replace the top of your function with:
def remove_supression(row): ...
Make sure to also replace the 'row[1]'
part with just row
, as otherwise you're just sending the literal string 'row[1]'
instead of the actual row.
On top of this, you need to assign the value you get from remove_supression(row)
inside the for loop to a value, which you can then use to get the text from, e.g.:
resp = remove_supression(row)
print(resp.text)
CodePudding user response:
You aren't printing the result but string representation of function object. Store the result to variable and print the variable. For example:
import csv
import requests
def remove_suppression():
return requests.delete(
"https://api.mailgun.net/v3/messages.thewebsite.com/unsubscribes",
auth=("api", "key-abcdefg12345789gbb"),
data={"address": "row[1]"},
)
with open("/Users/brett/Downloads/example.csv", newline="") as csvfile:
readCSV = csv.reader(csvfile, delimiter=",")
for row in readCSV:
try:
result = remove_suppression().text # <-- store the .text result
print("=========================")
print(result) # <-- print it
sleep(5)
except:
pass