forked from arsari/AEP-Batch-Download
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
59 lines (45 loc) · 1.63 KB
/
Copy pathapp.py
File metadata and controls
59 lines (45 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from flask import Flask, render_template, request, jsonify
import requests
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Setup Flask app
app = Flask(__name__)
AEP_API_KEY = os.getenv("AEP_API_KEY")
AEP_TENANT_ID = os.getenv("AEP_TENANT_ID")
AEP_ENDPOINT = os.getenv("AEP_ENDPOINT")
@app.route('/')
def index():
return render_template('index.html')
@app.route('/preview_batches', methods=['POST'])
def preview_batches():
batch_ids = request.form.get('batch_ids')
if not batch_ids:
return jsonify({"error": "No batch IDs provided"}), 400
batch_ids_list = batch_ids.split(',')
batch_previews = []
for batch_id in batch_ids_list:
response = get_failed_batch(batch_id.strip())
if response.status_code == 200:
batch_previews.append({"batch_id": batch_id, "data": response.json()})
else:
batch_previews.append({"batch_id": batch_id, "error": "Failed to retrieve data"})
return jsonify(batch_previews)
def get_failed_batch(batch_id):
headers = {
"Authorization": f"Bearer {os.getenv('AEP_ACCESS_TOKEN')}",
"x-api-key": AEP_API_KEY,
"Content-Type": "application/json"
}
url = f"{AEP_ENDPOINT}/batches/{batch_id}/failures"
response = requests.get(url, headers=headers)
return response
@app.route('/download_batch_data', methods=['POST'])
def download_batch_data():
batch_data = request.get_json()
if not batch_data:
return jsonify({"error": "No batch data provided"}), 400
return jsonify(batch_data), 200
if __name__ == '__main__':
app.run() # debug=True, host='0.0.0.0'