From 46de61771684f23bd64efb38ab27dc2cfe12f487 Mon Sep 17 00:00:00 2001 From: saturn4er Date: Wed, 26 Jul 2017 12:30:41 +0300 Subject: [PATCH 1/3] Return also scopes when requesting access token --- shopify/session.py | 11 ++++++++--- test/session_test.py | 5 +++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/shopify/session.py b/shopify/session.py index 9097b8f5..d40ad9ce 100644 --- a/shopify/session.py +++ b/shopify/session.py @@ -38,9 +38,12 @@ def temp(cls, domain, token): yield shopify.ShopifyResource.activate_session(original_session) - def __init__(self, shop_url, token=None, params=None): + def __init__(self, shop_url, token=None, params=None, scopes=None): + if scopes is None: + scopes = [] self.url = self.__prepare_url(shop_url) self.token = token + self.scopes = scopes return def create_permission_url(self, scope, redirect_uri=None): @@ -63,8 +66,10 @@ def request_token(self, params): response = urllib.request.urlopen(request) if response.code == 200: - self.token = json.loads(response.read().decode('utf-8'))['access_token'] - return self.token + json_response = json.loads(response.read().decode('utf-8')) + self.token = json_response['access_token'] + self.scopes = json_response['scopes'] + return self.token, self.scopes else: raise Exception(response.msg) diff --git a/test/session_test.py b/test/session_test.py index 11843810..011bf41b 100644 --- a/test/session_test.py +++ b/test/session_test.py @@ -167,10 +167,11 @@ def test_return_token_if_hmac_is_valid(self): hmac = shopify.Session.calculate_hmac(params) params['hmac'] = hmac - self.fake(None, url='https://localhost.myshopify.com/admin/oauth/access_token', method='POST', body='{"access_token" : "token"}', has_user_agent=False) + self.fake(None, url='https://localhost.myshopify.com/admin/oauth/access_token', method='POST', body='{"access_token" : "token", "scopes" : "a,b,c"}', has_user_agent=False) session = shopify.Session('http://localhost.myshopify.com') - token = session.request_token(params) + token, scopes = session.request_token(params) self.assertEqual("token", token) + self.assertEqual("a,b,c", scopes) def test_raise_error_if_hmac_is_invalid(self): shopify.Session.secret='secret' From 5f563d4b060903ac22f96ae586597013ad42357d Mon Sep 17 00:00:00 2001 From: saturn4er Date: Mon, 7 Aug 2017 15:55:31 +0300 Subject: [PATCH 2/3] Add marketing events support --- shopify/resources/__init__.py | 1 + shopify/resources/marketing_event.py | 10 ++++ .../resources/marketing_event_engagement.py | 5 ++ test/fixtures/marketing_event.json | 28 ++++++++++ .../fixtures/marketing_event_engagements.json | 15 +++++ test/fixtures/marketing_events.json | 56 +++++++++++++++++++ test/fixtures/marketing_events_count.json | 3 + test/marketing_events_test.py | 39 +++++++++++++ 8 files changed, 157 insertions(+) create mode 100644 shopify/resources/marketing_event.py create mode 100644 shopify/resources/marketing_event_engagement.py create mode 100644 test/fixtures/marketing_event.json create mode 100644 test/fixtures/marketing_event_engagements.json create mode 100644 test/fixtures/marketing_events.json create mode 100644 test/fixtures/marketing_events_count.json create mode 100644 test/marketing_events_test.py diff --git a/shopify/resources/__init__.py b/shopify/resources/__init__.py index fd921cd4..a5c1ca4c 100644 --- a/shopify/resources/__init__.py +++ b/shopify/resources/__init__.py @@ -56,4 +56,5 @@ from .report import Report from .price_rule import PriceRule from .discount_code import DiscountCode +from .marketing_event import MarketingEvent from ..base import ShopifyResource diff --git a/shopify/resources/marketing_event.py b/shopify/resources/marketing_event.py new file mode 100644 index 00000000..72c9f3fc --- /dev/null +++ b/shopify/resources/marketing_event.py @@ -0,0 +1,10 @@ +import json + +from shopify.resources.marketing_event_engagement import MarketingEventEngagement +from ..base import ShopifyResource + + +class MarketingEvent(ShopifyResource): + def engagements(self, *engagements): + resource = self.post("engagements", body=json.dumps(dict(engagements=engagements)).encode('utf8')) + return [MarketingEventEngagement(item) for item in MarketingEventEngagement.format.decode(resource.body)] diff --git a/shopify/resources/marketing_event_engagement.py b/shopify/resources/marketing_event_engagement.py new file mode 100644 index 00000000..fb476f34 --- /dev/null +++ b/shopify/resources/marketing_event_engagement.py @@ -0,0 +1,5 @@ +from ..base import ShopifyResource + + +class MarketingEventEngagement(ShopifyResource): + pass diff --git a/test/fixtures/marketing_event.json b/test/fixtures/marketing_event.json new file mode 100644 index 00000000..d4f86012 --- /dev/null +++ b/test/fixtures/marketing_event.json @@ -0,0 +1,28 @@ +{ + "marketing_event": { + "id": 998730532, + "event_target": "", + "event_type": "post", + "remote_id": "12345678", + "started_at": "2017-01-15T10:56:18-05:00", + "ended_at": null, + "scheduled_to_end_at": null, + "budget": "10.11", + "currency": "GBP", + "manage_url": null, + "preview_url": null, + "utm_campaign": "1234567890", + "utm_source": "facebook", + "utm_medium": "facebook-post", + "utm_content": null, + "utm_term": null, + "budget_type": "daily", + "description": null, + "marketing_channel": "social", + "paid": false, + "referring_domain": "facebook.com", + "breadcrumb_id": null, + "marketed_resources": [ + ] + } +} \ No newline at end of file diff --git a/test/fixtures/marketing_event_engagements.json b/test/fixtures/marketing_event_engagements.json new file mode 100644 index 00000000..faf41703 --- /dev/null +++ b/test/fixtures/marketing_event_engagements.json @@ -0,0 +1,15 @@ +{ + "engagements": [ + { + "occurred_on": "2017-01-15", + "views_count": 0, + "impressions_count": null, + "clicks_count": 0, + "favorites_count": 0, + "comments_count": null, + "shares_count": null, + "ad_spend": "10.00", + "is_cumulative": true + } + ] +} \ No newline at end of file diff --git a/test/fixtures/marketing_events.json b/test/fixtures/marketing_events.json new file mode 100644 index 00000000..1f952d2f --- /dev/null +++ b/test/fixtures/marketing_events.json @@ -0,0 +1,56 @@ +{ + "marketing_events": [ + { + "id": 998730532, + "event_target": "", + "event_type": "post", + "remote_id": "12345678", + "started_at": "2017-01-15T10:56:18-05:00", + "ended_at": null, + "scheduled_to_end_at": null, + "budget": "10.11", + "currency": "GBP", + "manage_url": null, + "preview_url": null, + "utm_campaign": "1234567890", + "utm_source": "facebook", + "utm_medium": "facebook-post", + "utm_content": null, + "utm_term": null, + "budget_type": "daily", + "description": null, + "marketing_channel": "social", + "paid": false, + "referring_domain": "facebook.com", + "breadcrumb_id": null, + "marketed_resources": [ + ] + }, + { + "id": 998730542, + "event_target": "", + "event_type": "post", + "remote_id": "12345678", + "started_at": "2017-01-15T10:56:18-05:00", + "ended_at": null, + "scheduled_to_end_at": null, + "budget": "25", + "currency": "USD", + "manage_url": null, + "preview_url": null, + "utm_campaign": "facebook-ads", + "utm_source": "facebook", + "utm_medium": "facebook-post", + "utm_content": null, + "utm_term": null, + "budget_type": "daily", + "description": null, + "marketing_channel": "social", + "paid": false, + "referring_domain": "facebook.com", + "breadcrumb_id": null, + "marketed_resources": [ + ] + } + ] +} \ No newline at end of file diff --git a/test/fixtures/marketing_events_count.json b/test/fixtures/marketing_events_count.json new file mode 100644 index 00000000..5d95c551 --- /dev/null +++ b/test/fixtures/marketing_events_count.json @@ -0,0 +1,3 @@ +{ + "count": 2 +} \ No newline at end of file diff --git a/test/marketing_events_test.py b/test/marketing_events_test.py new file mode 100644 index 00000000..e484182e --- /dev/null +++ b/test/marketing_events_test.py @@ -0,0 +1,39 @@ +import shopify +from shopify.resources.marketing_event_engagement import MarketingEventEngagement +from test.test_helper import TestCase + + +class MarketingEventsTest(TestCase): + def setUp(self): + super(MarketingEventsTest, self).setUp() + self.fake("marketing_events/998730532", method="GET", body=self.load_fixture('marketing_event')) + self.event = shopify.MarketingEvent.find(998730532) + self.assertEqual(998730532, self.event.id) + + def test_fetch_marketing_events(self): + self.fake("marketing_events", method="GET", body=self.load_fixture('marketing_events')) + events = shopify.MarketingEvent.find() + self.assertEqual(len(events), 2) + self.assertEqual(events[0].id, 998730532) + self.assertEqual(events[1].id, 998730542) + + def test_fetch_marketing_events_count(self): + self.fake("marketing_events/count", method="GET", body=self.load_fixture('marketing_events_count')) + count = shopify.MarketingEvent.count() + self.assertEqual(count, 2) + + def test_fetch_marketing_event_engagements(self): + self.fake("marketing_events/998730532/engagements", method="POST", code=201, + body=self.load_fixture('marketing_event_engagements'), headers=({'Content-type': 'application/json'})) + new_engagements = self.event.engagements({ + "occurred_on": "2017-01-15", + "views_count": 0, + "clicks_count": 0, + "favorites_count": 0, + "ad_spend": 10.0, + "is_cumulative": True + }) + self.assertEqual(len(new_engagements), 1) + self.assertEqual(type(new_engagements[0]), MarketingEventEngagement) + self.assertEqual(new_engagements[0].occurred_on, "2017-01-15") + self.assertEqual(new_engagements[0].ad_spend, "10.00") From 3f767a9e939425f357fdd004a1bdeb4947d181f1 Mon Sep 17 00:00:00 2001 From: saturn4er Date: Tue, 8 Aug 2017 14:52:03 +0300 Subject: [PATCH 3/3] Receveive scope instead of scopes --- shopify/session.py | 12 ++++++------ test/session_test.py | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/shopify/session.py b/shopify/session.py index d40ad9ce..1bbe6d34 100644 --- a/shopify/session.py +++ b/shopify/session.py @@ -38,12 +38,12 @@ def temp(cls, domain, token): yield shopify.ShopifyResource.activate_session(original_session) - def __init__(self, shop_url, token=None, params=None, scopes=None): - if scopes is None: - scopes = [] + def __init__(self, shop_url, token=None, params=None, scope=None): + if scope is None: + scope = [] self.url = self.__prepare_url(shop_url) self.token = token - self.scopes = scopes + self.scope = scope return def create_permission_url(self, scope, redirect_uri=None): @@ -68,8 +68,8 @@ def request_token(self, params): if response.code == 200: json_response = json.loads(response.read().decode('utf-8')) self.token = json_response['access_token'] - self.scopes = json_response['scopes'] - return self.token, self.scopes + self.scope = json_response['scope'] + return self.token, self.scope else: raise Exception(response.msg) diff --git a/test/session_test.py b/test/session_test.py index 011bf41b..cc64d63c 100644 --- a/test/session_test.py +++ b/test/session_test.py @@ -167,11 +167,11 @@ def test_return_token_if_hmac_is_valid(self): hmac = shopify.Session.calculate_hmac(params) params['hmac'] = hmac - self.fake(None, url='https://localhost.myshopify.com/admin/oauth/access_token', method='POST', body='{"access_token" : "token", "scopes" : "a,b,c"}', has_user_agent=False) + self.fake(None, url='https://localhost.myshopify.com/admin/oauth/access_token', method='POST', body='{"access_token" : "token", "scope" : "a,b,c"}', has_user_agent=False) session = shopify.Session('http://localhost.myshopify.com') - token, scopes = session.request_token(params) + token, scope = session.request_token(params) self.assertEqual("token", token) - self.assertEqual("a,b,c", scopes) + self.assertEqual("a,b,c", scope) def test_raise_error_if_hmac_is_invalid(self): shopify.Session.secret='secret'