|
from flask import request, jsonify |
|
import os |
|
import requests |
|
import globs |
|
|
|
api_key = os.getenv('api_key') |
|
base_url = os.getenv('base_url') |
|
token = os.getenv('token') |
|
|
|
def save_db(): |
|
err = 0 |
|
response_message = "Operation failed" |
|
|
|
if api_key == request.args.get('api'): |
|
|
|
names = globs.name |
|
emails = globs.email |
|
phones = globs.phone |
|
grups_id = globs.grup_id |
|
pr1s = globs.pr1 |
|
pr2s = globs.pr2 |
|
|
|
|
|
data = { |
|
'email': emails, |
|
'unconfirmed': True, |
|
'values': [ |
|
{'parameter_id': '1', 'value': names}, |
|
{'parameter_id': '2', 'value': phones}, |
|
{'parameter_id': '3', 'value': pr1s}, |
|
{'parameter_id': '4', 'value': pr2s} |
|
], |
|
'tags': [ |
|
'buyer', |
|
'regular customer' |
|
] |
|
} |
|
|
|
|
|
headers = { |
|
'Authorization': f'Bearer {token}', |
|
'Content-Type': 'application/json' |
|
} |
|
response = requests.post(f"{base_url}lists/{grups_id}/recipients", json=data, headers=headers) |
|
|
|
|
|
globs.server_response = response.text |
|
|
|
|
|
if response.status_code == 200: |
|
err = 1 |
|
response_message = "Operation completed successfully" |
|
else: |
|
response_message = f"Error: {response.status_code} - {response.text}" |
|
err = 0 |
|
|
|
response_data = { |
|
"status": "success" if err == 1 else "error", |
|
"err": err, |
|
"message": response_message, |
|
"server_response": globs.server_response if err == 1 else None |
|
} |
|
|
|
return jsonify(response_data) |
|
|