forked from marsan02/anotherdsp-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
213 lines (195 loc) · 9.74 KB
/
main.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from flask import Flask, request, jsonify, request, render_template, redirect,url_for
from pymongo import MongoClient
from dotenv import load_dotenv
from flask_cors import CORS
import utils.geo_api as geo_api
import requests
from utils.auth import requires_auth, handle_auth_error, AuthError
import os
import services.reporting as reporting
from services.sellers import SellersService
from services.buyers import BuyersService
from services.deals import DealsService
from services.creatives import CreativesService
from services.campaigns import CampaignsService
from services.advertisers import AdvertisersService
import pypyodbc
from utils.mysql_connection import Database
from services.user_management import UserManagementService
load_dotenv() # This loads the environment variables from .env
def configure_routes(app):
# MongoDB Atlas connection URI
mongo_uri = os.environ.get("MONGO_URI")
client = MongoClient(mongo_uri)
auth0_domain = os.environ.get("AUTH0_DOMAIN")
auth0_client_id = os.environ.get("AUTH0_CLIENT_ID")
auth0_client_secret = os.environ.get("AUTH0_CLIENT_SECRET")
try:
mysql_conn = Database()
app.register_error_handler(AuthError, handle_auth_error)
sellers = SellersService(client)
buyers = BuyersService(client)
deals = DealsService(client)
creatives = CreativesService(client)
campaigns = CampaignsService(client)
advertisers = AdvertisersService(client)
users = UserManagementService(auth0_domain,auth0_client_id,auth0_client_secret)
users.get_token()
@app.route('/countries')
@requires_auth
def get_countries(*args, **kwargs):
buyer_id = kwargs.get('buyer_id', None)
return geo_api.get_countries()
@app.route('/users', methods=['GET','POST','PUT','DELETE'])
def users_service(*args, **kwargs):
print(request.args.to_dict())
if request.method == 'GET':
return users.get_users(request.args.to_dict())
if request.method == 'POST':
return users.create_user(request.json)
if request.method == 'PUT':
return users.edit_user(request.args.get('user_id'),request.json)
if request.method == 'DELETE':
return users.delete_user(request.args.get('user_id'))
@app.route('/reporting', methods=['POST'])
@requires_auth
def submit_report(*args, **kwargs):
buyer_id = kwargs.get('buyer_id', None)
if request.method == 'POST':
payload = request.json
return reporting.run_report(buyer_id,payload['dimensions'],payload['metrics'],payload['filters'])
@app.route('/campaign-monitoring', methods=['GET'])
@requires_auth
def process_campaign_monitoring(*args, **kwargs):
buyer_id = kwargs.get('buyer_id', None)
if not buyer_id:
buyer_id = request.args.get('buyer_id')
if buyer_id:
buyer_id=int(buyer_id)
all_campaigns, status = campaigns.get_mysql(request.args, request.args.get('id'),buyer_id,mysql_conn)
payload = {
"dimensions": ["campaign_id"],
"metrics": ["bids", "imps", "clicks", "viewable_imps", "spend", "revenue"],
"filters": []
}
reporting_data = reporting.run_report(buyer_id, payload['dimensions'], payload['metrics'], payload['filters'])
# Create a lookup for campaign reporting data
reporting_lookup = {report['campaign_id']: report for report in reporting_data}
stitched_data = []
if status == 200:
for campaign in all_campaigns:
campaign_id = campaign['id']
if campaign_id in reporting_lookup:
# Merge the dictionaries (Python 3.5+)
merged = {**campaign, **reporting_lookup[campaign_id]}
else:
# Create a record with all metrics set to 0 for campaigns without reporting data
metrics_zero = {metric: 0 for metric in payload['metrics']}
metrics_zero['campaign_id'] = campaign_id
merged = {**campaign, **metrics_zero}
stitched_data.append(merged)
return stitched_data
else:
return jsonify({"message": "object not found"}), 404
@app.route('/campaigns', methods=['POST','GET', 'PUT', 'DELETE'])
@requires_auth
def campaigns_service(*args, **kwargs):
buyer_id = kwargs.get('buyer_id', None)
if not buyer_id:
buyer_id = request.args.get('buyer_id')
if buyer_id:
buyer_id=int(buyer_id)
if request.method == 'GET':
return campaigns.get_mysql(request.args,request.args.get('id'),buyer_id,mysql_conn)
elif request.method == 'PUT':
return campaigns.put_mysql(request.args['id'],request.json,buyer_id,mysql_conn)
elif request.method == 'DELETE':
return campaigns.get_mysql(request.args['id'],buyer_id,mysql_conn)
elif request.method == 'POST':
return campaigns.post_mysql(request.json,buyer_id,mysql_conn)
@app.route('/advertisers', methods=['POST','GET', 'PUT', 'DELETE'])
@requires_auth
def advertisers_service(*args, **kwargs):
buyer_id = kwargs.get('buyer_id', None)
if not buyer_id:
buyer_id = request.args.get('buyer_id')
if buyer_id:
buyer_id=int(buyer_id)
if request.method == 'GET':
result = advertisers.get_mysql(request.args,request.args.get('id'),1,mysql_conn)
elif request.method == 'PUT':
result = advertisers.put_mysql(request.args['id'],request.json,1,mysql_conn)
elif request.method == 'DELETE':
result = advertisers.delete_mysql(request.args['id'],1,mysql_conn)
elif request.method == 'POST':
result = advertisers.post_mysql(request.json,1,mysql_conn)
return result
@app.route('/deals', methods=['POST','GET', 'PUT', 'DELETE'])
@requires_auth
def deals_service(*args, **kwargs):
buyer_id = kwargs.get('buyer_id', None)
if not buyer_id:
buyer_id = request.args.get('buyer_id')
if buyer_id:
buyer_id=int(buyer_id)
if request.method == 'GET':
result = deals.get_mysql(request.args,request.args.get('id'),buyer_id,mysql_conn)
elif request.method == 'PUT':
result = deals.put_mysql(request.args['id'],request.json,buyer_id,mysql_conn)
elif request.method == 'DELETE':
result= deals.delete_mysql(request.args['id'],buyer_id,mysql_conn)
elif request.method == 'POST':
result= deals.post_mysql(request.json,buyer_id,mysql_conn)
return result
@app.route('/creatives', methods=['POST','GET', 'PUT', 'DELETE'])
@requires_auth
def creatives_service(*args, **kwargs):
buyer_id = kwargs.get('buyer_id', None)
if not buyer_id:
buyer_id = request.args.get('buyer_id')
if buyer_id:
buyer_id=int(buyer_id)
if request.method == 'GET':
result= creatives.get_mysql(request.args,request.args.get('id'),buyer_id,mysql_conn)
elif request.method == 'PUT':
result= creatives.put_mysql(request.args['id'],request.json,buyer_id,mysql_conn)
elif request.method == 'DELETE':
result= creatives.delete_mysql(request.args['id'],buyer_id,mysql_conn)
elif request.method == 'POST':
result= creatives.post_mysql(request.json,buyer_id,mysql_conn)
return result
@app.route('/sellers', methods=['POST','GET', 'PUT', 'DELETE'])
@requires_auth
def sellers_service(*args, **kwargs):
buyer_id = kwargs.get('buyer_id', None)
if request.method == 'GET':
result= sellers.get_mysql(request.args,request.args.get('id'),buyer_id,mysql_conn)
elif request.method == 'PUT':
result= sellers.put_mysql(request.args['id'],request.json,buyer_id,mysql_conn)
elif request.method == 'DELETE':
result= sellers.delete_mysql(request.args['id'],buyer_id,mysql_conn)
elif request.method == 'POST':
result= sellers.post_mysql(request.json,buyer_id,mysql_conn)
return result
@app.route('/buyers', methods=['POST','GET', 'PUT', 'DELETE'])
@requires_auth
def buyers_service(*args, **kwargs):
buyer_id = kwargs.get('buyer_id', None)
if request.method == 'GET':
result= buyers.get_mysql(request.args,request.args.get('id'),buyer_id,mysql_conn)
elif request.method == 'PUT':
result= buyers.put_mysql(request.args['id'],request.json,buyer_id,mysql_conn)
elif request.method == 'DELETE':
result= buyers.delete(request.args['id'],buyer_id)
elif request.method == 'POST':
result= buyers.post_mysql(request.json,buyer_id,mysql_conn)
return result
# Example of a protected route
@app.route('/')
def your_protected_route():
# Now this route only requires the user to be authenticated
return 'Welcome to Another DSP API'
except pypyodbc.Error as e:
print(f"Error: {e}")
if __name__ == '__main__':
app.run(debug=True)