maropost_cf / app.py
joshuadunlop's picture
Create app.py
bb98c0f verified
import streamlit as st
import pandas as pd
import requests
# Function to create a custom field in Maropost
def create_custom_field(data, auth_token):
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
}
response = requests.post(
"https://api.maropost.com/accounts/1783/custom_fields.json",
json=data,
headers=headers,
params={'auth_token': auth_token}
)
return response
# Function to read CSV and create custom fields
def create_custom_fields_from_csv(csv_file, auth_token):
df = pd.read_csv(csv_file)
success_fields = []
failed_fields = []
for _, row in df.iterrows():
data = {
"custom_field": {
"name": row['name'],
"field_type": row['field_type'],
"add_to_profile_page": row.get('add_to_profile_page', False),
"default_value": row.get('default_value', "")
}
}
response = create_custom_field(data, auth_token)
if response.status_code in [200, 201]:
success_fields.append(row['name'])
else:
failed_fields.append((row['name'], response.status_code, response.text))
return success_fields, failed_fields
# Streamlit UI
def main():
st.title("Maropost Custom Fields Creator")
auth_token = st.text_input("Enter your Maropost API auth token:", type="password")
csv_file = st.file_uploader("Upload CSV file for custom fields", type=['csv'])
if st.button("Create Custom Fields"):
if not auth_token or csv_file is None:
st.error("Please enter your auth token and upload a CSV file.")
else:
success_fields, failed_fields = create_custom_fields_from_csv(csv_file, auth_token)
if success_fields:
st.success(f"Successfully created custom fields: {', '.join(success_fields)}.")
if failed_fields:
for failed_field, status_code, message in failed_fields:
st.error(f"Failed to create custom field {failed_field}. Response code: {status_code}, Message: {message}")
if __name__ == "__main__":
main()