Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended get reporting functionality #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 73 additions & 32 deletions searchads_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def api_call(self,
headers={},
json_data={},
params={},
method="GET",
method="GET",
limit=1000,
offset=0):
"""
Expand Down Expand Up @@ -64,22 +64,21 @@ def api_call(self,
kwargs["params"].update(params)
api_endpoint = "{}/{}".format(self.api_version, api_endpoint)
url = url.format(api_endpoint)
try:
if method == "get" or method == "GET":
req = caller.get(url, **kwargs)
elif method == "post" or method == "POST":
req = caller.post(url, **kwargs)
elif method == "put" or method == "PUT":
req = caller.put(url, **kwargs)
elif method == "delete" or method == "DELETE":
req = caller.delete(url, **kwargs)
if self.verbose:
print(req.url)
print(req.text)
return req.json()
except Exception as e:
print(str(e), url)
return None
req = None
if method == "get" or method == "GET":
req = caller.get(url, **kwargs)
elif method == "post" or method == "POST":
req = caller.post(url, **kwargs)
elif method == "put" or method == "PUT":
req = caller.put(url, **kwargs)
elif method == "delete" or method == "DELETE":
req = caller.delete(url, **kwargs)
if self.verbose and req:
print(req.url)
print(req.text)
# Raising errors to show errors
req.raise_for_status()
return req.json()

# Campaign Methods
def create_campaign(self,
Expand All @@ -88,7 +87,7 @@ def create_campaign(self,
campaign_name,
budget,
daily_budget,
curruncy):
currency):
"""
Creates a campaign to promote an app.
"""
Expand All @@ -97,11 +96,11 @@ def create_campaign(self,
"name": campaign_name,
"budgetAmount": {
"amount": "{}".format(budget),
"currency": curruncy
"currency": currency
},
"dailyBudgetAmount": {
"amount": "{}".format(daily_budget),
"currency": curruncy
"currency": currency
},
"adamId": app_id,
"countriesOrRegions": countries
Expand Down Expand Up @@ -187,6 +186,31 @@ def get_campaigns(self, limit=0, offset=0):
offset += result["pagination"]["itemsPerPage"]
return res

def get_origins(self):
"""
Get User ACL Response Example
{
"data": [
{
"currency": "USD",
"orgId": <OrgID>,
"orgName": "<OrgName1>",
"paymentModel": "LOC",
"roleNames": ["Admin"]
},
{
"currency": "USD",
"orgId": <OrgID>;,
"orgName": "<OrgName2>",
"paymentModel": "LOC",
"roleNames": ["Admin"]
}],
}
"""
return self.api_call(
api_endpoint="acls"
)

def update_campaign(self,
campaign_id,
countries=None,
Expand Down Expand Up @@ -1146,7 +1170,8 @@ def get_campaigns_report_by_date(self,
return_row_totals=True,
return_grand_totals=True,
offset=0,
limit=1000):
limit=1000,
**kwargs):
"""
Get reports on campaigns within a specific org.
{
Expand Down Expand Up @@ -1206,7 +1231,8 @@ def get_campaigns_report_by_date(self,
return_row_totals=return_row_totals,
return_grand_totals=return_grand_totals,
offset=offset,
limit=limit)
limit=limit,
**kwargs)

def get_adgroups_report_by_date(self,
campaignId,
Expand All @@ -1219,7 +1245,8 @@ def get_adgroups_report_by_date(self,
return_row_totals=True,
return_grand_totals=True,
offset=0,
limit=1000):
limit=1000,
**kwargs):
"""
Get reports on adGroups within a specific campaign.
{
Expand Down Expand Up @@ -1261,7 +1288,8 @@ def get_adgroups_report_by_date(self,
return_row_totals=return_row_totals,
return_grand_totals=return_grand_totals,
offset=offset,
limit=limit)
limit=limit,
**kwargs)

def get_creativesets_report_by_date(self,
campaignId,
Expand All @@ -1274,7 +1302,8 @@ def get_creativesets_report_by_date(self,
return_row_totals=True,
return_grand_totals=True,
offset=0,
limit=1000):
limit=1000,
**kwargs):
"""
Fetches reports on Creative Sets used within a campaign.
conditions example
Expand All @@ -1291,7 +1320,8 @@ def get_creativesets_report_by_date(self,
return_row_totals=return_row_totals,
return_grand_totals=return_grand_totals,
offset=offset,
limit=limit)
limit=limit,
**kwargs)

def get_keywords_report_by_date(self,
campaignId,
Expand All @@ -1304,7 +1334,8 @@ def get_keywords_report_by_date(self,
return_row_totals=True,
return_grand_totals=True,
offset=0,
limit=1000):
limit=1000,
**kwargs):
"""
Get reports on targeting keywords within a specific campaign.
"""
Expand All @@ -1319,7 +1350,8 @@ def get_keywords_report_by_date(self,
return_row_totals=return_row_totals,
return_grand_totals=return_grand_totals,
offset=offset,
limit=limit)
limit=limit,
**kwargs)

def get_searchterms_report_by_date(self,
campaignId,
Expand All @@ -1332,7 +1364,8 @@ def get_searchterms_report_by_date(self,
return_row_totals=True,
return_grand_totals=True,
offset=0,
limit=1000):
limit=1000,
**kwargs):
"""
Get reports on targeting keywords within a specific campaign.
"""
Expand All @@ -1347,7 +1380,8 @@ def get_searchterms_report_by_date(self,
return_row_totals=return_row_totals,
return_grand_totals=return_grand_totals,
offset=offset,
limit=limit)
limit=limit,
**kwargs)

def _get_data(self,
data_type,
Expand All @@ -1362,7 +1396,8 @@ def _get_data(self,
offset,
limit,
campaignId=None,
group_by=None):
group_by=None,
**kwargs):
row = []
grandTotals = []
if limit == 0:
Expand Down Expand Up @@ -1395,6 +1430,12 @@ def _get_data(self,
data["groupBy"] = [
group_by
]
if kwargs:
data = {
**data,
**kwargs
}

if data_type == "campaigns":
res = self.api_call("reports/campaigns",
json_data=data, method="POST")
Expand Down Expand Up @@ -1427,7 +1468,7 @@ def _get_data(self,
grandTotals.extend(
res["data"]["reportingDataResponse"]["grandTotals"])
res_len = len(row)

offset = len(row)
# print(limit)
if res_len == limit or res_len >= res["pagination"]["totalResults"]:
Expand Down