Skip to content

let Analytics class use resource_property() #211

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

Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion examples/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@

async_stats_job_result = LineItem.async_stats_job_result(account, [job_id]).first

async_data = LineItem.async_stats_job_data(account, async_stats_job_result['url'])
async_data = LineItem.async_stats_job_data(account, async_stats_job_result.url)
8 changes: 4 additions & 4 deletions tests/test_analytics_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ def test_analytics_async():
[job_id]).first

assert job_result is not None
assert isinstance(job_result, dict)
assert job_result['url'] == 'https://ton.twimg.com/advertiser-api-async-analytics/stats.json.gz'
assert isinstance(job_result, Analytics)
assert job_result.url == 'https://ton.twimg.com/advertiser-api-async-analytics/stats.json.gz'

# call async_stats_job_result() from Analytics class directly
job_result = Analytics.async_stats_job_result(
account,
[job_id]).first

assert job_result is not None
assert isinstance(job_result, dict)
assert job_result['url'] == 'https://ton.twimg.com/advertiser-api-async-analytics/stats.json.gz'
assert isinstance(job_result, Analytics)
assert job_result.url == 'https://ton.twimg.com/advertiser-api-async-analytics/stats.json.gz'
6 changes: 3 additions & 3 deletions twitter_ads/campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def tv_shows(klass, account, **kwargs):
resource_property(TargetingCriteria, 'to_delete', transform=TRANSFORM.BOOL)


class FundingInstrument(Resource, Persistence, Analytics):
class FundingInstrument(Analytics, Resource, Persistence):

PROPERTIES = {}

Expand Down Expand Up @@ -226,7 +226,7 @@ def apps(self):
resource_property(AppList, 'apps', readonly=True)


class Campaign(Resource, Persistence, Analytics, Batch):
class Campaign(Analytics, Resource, Persistence, Batch):

PROPERTIES = {}

Expand Down Expand Up @@ -259,7 +259,7 @@ class Campaign(Resource, Persistence, Analytics, Batch):
resource_property(Campaign, 'to_delete', transform=TRANSFORM.BOOL)


class LineItem(Resource, Persistence, Analytics, Batch):
class LineItem(Analytics, Resource, Persistence, Batch):

PROPERTIES = {}

Expand Down
4 changes: 2 additions & 2 deletions twitter_ads/creative.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PromotedAccount(Resource, Persistence):
resource_property(PromotedAccount, 'user_id')


class PromotedTweet(Resource, Persistence, Analytics):
class PromotedTweet(Analytics, Resource, Persistence):

PROPERTIES = {}

Expand Down Expand Up @@ -91,7 +91,7 @@ class AccountMedia(Resource, Persistence):
resource_property(AccountMedia, 'video_id')


class MediaCreative(Resource, Persistence, Analytics):
class MediaCreative(Analytics, Resource, Persistence):

PROPERTIES = {}

Expand Down
17 changes: 15 additions & 2 deletions twitter_ads/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,12 @@ def delete(self):
self.from_response(response.body['data'])


class Analytics(object):
class Analytics(Resource):
"""
Container for all analytics related logic used by API resource objects.
"""
PROPERTIES = {}

ANALYTICS_MAP = {
'Campaign': ENTITY.CAMPAIGN,
'FundingInstrument': ENTITY.FUNDING_INSTRUMENT,
Expand Down Expand Up @@ -297,7 +299,7 @@ def async_stats_job_result(klass, account, job_ids=None, **kwargs):
resource = klass.RESOURCE_ASYNC.format(account_id=account.id)
request = Request(account.client, 'get', resource, params=params)

return Cursor(None, request, init_with=[account])
return Cursor(Analytics, request, init_with=[account])

@classmethod
def async_stats_job_data(klass, account, url, **kwargs):
Expand Down Expand Up @@ -341,3 +343,14 @@ def active_entities(klass, account, start_time, end_time, **kwargs):
resource = klass.RESOURCE_ACTIVE_ENTITIES.format(account_id=account.id)
response = Request(account.client, 'get', resource, params=params).perform()
return response.body['data']


# async_stats_job_result() properties
# read-only
resource_property(Analytics, 'id', readonly=True)
resource_property(Analytics, 'id_str', readonly=True)
resource_property(Analytics, 'status', readonly=True)
resource_property(Analytics, 'url', readonly=True)
resource_property(Analytics, 'created_at', readonly=True)
resource_property(Analytics, 'expires_at', readonly=True)
resource_property(Analytics, 'updated_at', readonly=True)