Skip to content
This repository was archived by the owner on Jan 17, 2025. It is now read-only.

Commit c150f15

Browse files
Merge pull request #14 from dynata/NameChanges
Name changes
2 parents fc5e9ff + 8a78015 commit c150f15

File tree

11 files changed

+114
-45
lines changed

11 files changed

+114
-45
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ Package for building and interacting with the Dynata Respondent Exchange (REX)
1414
from dynata_rex import OpportunityRegistry
1515
registry = OpportunityRegistry('rex_access_key', 'rex_secret_key')
1616
```
17-
### List opportunities from the registry
17+
### List opportunity notifications from the registry
1818
```
19-
opportunities = registry.list_opportunities()
19+
opportunities = registry.receive_notifications()
2020
2121
# [Opportunity(id=1,...), Opportunity(id=2,...), Opportunity(id=1,...)]
2222
```
2323
### Convert an opportunity to JSON
2424
```
2525
opportunity_json = Opportunity.json()
2626
```
27-
### Acknowledge a list of opportunities from the registry
27+
### Acknowledge a list of notifications from the registry
2828
```
29-
registry.ack_opportunities([opportunity_1.id, ..., opportunity_N.id])
29+
registry.ack_notifications([opportunity_1.id, ..., opportunity_N.id])
3030
```
31-
### Acknowledge a single opportunity from the registry
31+
### Acknowledge a single notification from the registry
3232
```
33-
registry.ack_opportunity(opportunity.id)
33+
registry.ack_notification(opportunity.id)
3434
```
3535
### Get a list of corresponding opportunities from a project_id
3636
```
@@ -209,9 +209,9 @@ gw.expire_context('super-unique-ctx-id')
209209
# }
210210
```
211211

212-
##### Get Attributes
212+
##### List Attributes
213213
```
214-
gw.get_attributes('country', 'page_number', 'page_size')
214+
gw.list_attributes('country', 'page_number', 'page_size')
215215
216216
# {
217217
# 'data':

dynata_rex/opportunity_registry.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ def _get_opportunity(self, opportunity_id: int) -> dict:
6565
return self.make_request.post(endpoint, data)
6666

6767
def _list_opportunities(self, limit: int = 10) -> List[dict]:
68-
"""Raw get opportunities"""
68+
"""
69+
[Deprecated - please use _receive_notifications()]
70+
Raw get opportunities"""
6971
endpoint = f"{self.base_url}/list-opportunities"
7072
data = {
7173
"limit": limit,
@@ -76,9 +78,22 @@ def _list_opportunities(self, limit: int = 10) -> List[dict]:
7678
}
7779
return self.make_request.post(endpoint, data)
7880

81+
def _receive_notifications(self, limit: int = 10) -> List[dict]:
82+
"""Raw receive notifications"""
83+
endpoint = f"{self.base_url}/receive-notifications"
84+
data = {
85+
"limit": limit,
86+
"shards": {
87+
"count": self.shard_count,
88+
"current": self.current_shard
89+
}
90+
}
91+
return self.make_request.post(endpoint, data)
92+
7993
def list_opportunities(self, limit: int = 10) -> List[models.Opportunity]:
80-
"""Get opportunities from Opportunity Registry
8194
"""
95+
[Deprecated - please use receive_notifications()]
96+
Get opportunities from Opportunity Registry"""
8297
opportunities = self._list_opportunities(limit=limit)
8398
out = []
8499
for opp in opportunities:
@@ -94,6 +109,24 @@ def list_opportunities(self, limit: int = 10) -> List[models.Opportunity]:
94109

95110
return out
96111

112+
def receive_notifications(self,
113+
limit: int = 10) -> List[models.Opportunity]:
114+
"""Get opportunity notifications from Opportunity Registry"""
115+
opportunities = self._receive_notifications(limit=limit)
116+
out = []
117+
for opp in opportunities:
118+
try:
119+
out.append(models.Opportunity(**opp))
120+
except pydantic.error_wrappers.ValidationError:
121+
opportunity_id = opp['id']
122+
logger.warning(
123+
f"Unable to parse {opportunity_id}, excluding...")
124+
logger.warning(json.dumps(opp, indent=4))
125+
# Ack notification so we don't see it again
126+
self.ack_notification(opportunity_id)
127+
128+
return out
129+
97130
def get_opportunity(self, opportunity_id: int) -> models.Opportunity:
98131
"""Get specific opportunity from SMOR
99132
"""
@@ -107,18 +140,31 @@ def list_project_opportunities(self, project_id: int) -> List[int]:
107140
return self.make_request.post(endpoint, data)
108141

109142
def ack_opportunity(self, opportunity_id: int) -> None:
110-
"""Acknowledge a single oppportunity
111143
"""
144+
[Deprecated - please use ack_notification()]
145+
Acknowledge a single opportunity"""
112146
data = [opportunity_id]
113147
return self.ack_opportunities(data)
114148

149+
def ack_notification(self, opportunity_id: int) -> None:
150+
"""Acknowledge a single notification"""
151+
data = [opportunity_id]
152+
return self.ack_notifications(data)
153+
115154
def ack_opportunities(self, opportunities: List[int]) -> None:
116-
"""Acknowledge a list of oppportunities
117155
"""
156+
[Deprecated - please use ack_notifications()]
157+
Acknowledge a list of opportunities"""
118158
endpoint = f"{self.base_url}/ack-opportunities"
119159
res = self.make_request.post(endpoint, opportunities)
120160
return res
121161

162+
def ack_notifications(self, opportunities: List[int]) -> None:
163+
"""Acknowledge a list of notifications"""
164+
endpoint = f"{self.base_url}/ack-notifications"
165+
res = self.make_request.post(endpoint, opportunities)
166+
return res
167+
122168
def download_collection(self, collection_id: str) -> list:
123169
"""Download targeting from a collection cell"""
124170
endpoint = f"{self.base_url}/download-collection"

dynata_rex/respondent_gateway.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ def get_attributes(self,
261261
page_number: int,
262262
page_size: int) -> dict:
263263
"""
264+
[Deprecated - please use list_attributes()]
265+
264266
Get a list of attribute id's and their statuses
265267
266268
@country: Country code for which you would like attributes
@@ -276,3 +278,24 @@ def get_attributes(self,
276278
"page_size": page_size
277279
}
278280
return self.make_request.post(endpoint, data)
281+
282+
def list_attributes(self,
283+
country: str,
284+
page_number: int,
285+
page_size: int) -> dict:
286+
"""
287+
Get a list of attribute id's and their statuses
288+
289+
@country: Country code for which you would like attributes
290+
291+
@page_number: What page number you are requesting (for pagination)
292+
293+
@page_size: How many id's you would like returned in each page
294+
"""
295+
endpoint = f"{self.base_url}/list-attributes"
296+
data = {
297+
"country": country,
298+
"page_number": page_number,
299+
"page_size": page_size
300+
}
301+
return self.make_request.post(endpoint, data)

examples/opportunity_registry/02_list_opportunities.py renamed to examples/opportunity_registry/02_receive_notifications.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
registry = OpportunityRegistry('rex_access_key', 'rex_secret_key')
44

5-
opportunities = registry.list_opportunities()
5+
opportunities = registry.receive_notifications()
66

7-
# Returns a list of Opportunities
7+
# Returns a list of Opportunity notifications
88
# [Opportunity(id=1,...), Opportunity(id=2,...), Opportunity(id=1,...)]

examples/opportunity_registry/04_acknowledge_multiple_opportunities.py renamed to examples/opportunity_registry/04_acknowledge_multiple_notifications.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
registry = OpportunityRegistry('rex_access_key', 'rex_secret_key')
44

5-
opportunities = registry.list_opportunities()
5+
opportunities = registry.receive_notifications()
66

77
opportunity_ids = [opportunity.id for opportunity in opportunities]
88

9-
registry.ack_opportunities(opportunity_ids)
9+
registry.ack_notifications(opportunity_ids)

examples/opportunity_registry/05_acknowledge_single_opportunity.py renamed to examples/opportunity_registry/05_acknowledge_single_notification.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
registry = OpportunityRegistry('rex_access_key', 'rex_secret_key')
44

5-
opportunities = registry.list_opportunities()
5+
opportunities = registry.receive_notifications()
66

77
opportunity = opportunities[0]
88

9-
registry.ack_opportunity(opportunity.id)
9+
registry.ack_notification(opportunity.id)

examples/respondent_gateway/11_get_attributes.py renamed to examples/respondent_gateway/11_list_attributes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
page_size = 100 # Number of Items Being Requested
99

10-
gateway.get_attributes(country, page_number, page_size)
10+
gateway.list_attributes(country, page_number, page_size)
1111

1212
# {
1313
# "data": [

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setuptools.setup(
1111
name="dynata_rex",
12-
version="1.1.0",
12+
version="1.2.0",
1313
author="REX Maintainers",
1414
author_email="[email protected]",
1515
description=("Package for building and interacting with the "
File renamed without changes.

tests/test_opportunity_registry.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ def test_get_opportunity(session_post):
7070

7171

7272
@patch.object(requests.Session, "post")
73-
def test__list_opportunities(session_post):
74-
"""_list_opportunities should return the json data from response as a
73+
def test__receive_notifications(session_post):
74+
"""_receive_notifications should return the json data from response as a
7575
list of dicts"""
76-
data = TEST_DATA['test_list_opportunities']
76+
data = TEST_DATA['test_receive_notifications']
7777
session_post.return_value = ResponseMock._response_mock(
7878
200, content=json.dumps(data), content_type="application/json"
7979
)
8080

81-
r = REGISTRY._list_opportunities()
81+
r = REGISTRY._receive_notifications()
8282

8383
assert isinstance(r, list)
8484

@@ -87,28 +87,28 @@ def test__list_opportunities(session_post):
8787

8888

8989
@patch.object(requests.Session, "post")
90-
def test_list_opportunities(session_post):
91-
"""_get_opportunity should return the json data from response as a
90+
def test_receive_notifications(session_post):
91+
"""_receive_notifications should return the json data from response as a
9292
list of Opportunity objects"""
93-
data = TEST_DATA['test_list_opportunities']
93+
data = TEST_DATA['test_receive_notifications']
9494
session_post.return_value = ResponseMock._response_mock(
9595
200, content=json.dumps(data), content_type="application/json"
9696
)
97-
r = REGISTRY.list_opportunities()
97+
r = REGISTRY.receive_notifications()
9898

9999
assert isinstance(r, list)
100100

101101
for opportunity in r:
102102
assert isinstance(opportunity, dynata_rex.models.Opportunity)
103103

104104

105-
@patch.object(dynata_rex.OpportunityRegistry, "ack_opportunity")
105+
@patch.object(dynata_rex.OpportunityRegistry, "ack_notification")
106106
@patch.object(requests.Session, "post")
107-
def test_list_opportunities_assert_ack_for_invalid_opportunity(session_post,
108-
ack_method):
109-
"""list opportunities should 'ack' an opportunity returned
107+
def test_receive_notifications_assert_ack_for_invalid_opportunity(session_post,
108+
ack_method):
109+
"""receive notifications should 'ack' a notification returned
110110
that it cannot convert into an Opportunity object"""
111-
data = TEST_DATA['test_list_opportunities']
111+
data = TEST_DATA['test_receive_notifications']
112112

113113
# Append an invalid Opportunity
114114
data.append(
@@ -123,41 +123,41 @@ def test_list_opportunities_assert_ack_for_invalid_opportunity(session_post,
123123
session_post.return_value = ResponseMock._response_mock(
124124
200, content=json.dumps(data), content_type="application/json"
125125
)
126-
REGISTRY.list_opportunities()
126+
REGISTRY.receive_notifications()
127127

128128
# Make sure the ack method was called for the invalid opportunity
129129
assert ack_method.call_count == 1
130130

131131

132-
@patch.object(dynata_rex.OpportunityRegistry, "ack_opportunities")
132+
@patch.object(dynata_rex.OpportunityRegistry, "ack_notifications")
133133
@patch.object(requests.Session, "post")
134-
def test_ack_opportunity(session_post, ack_method):
135-
data = TEST_DATA['test_list_opportunities'][0]
134+
def test_ack_notification(session_post, ack_method):
135+
data = TEST_DATA['test_receive_notifications'][0]
136136

137137
session_post.return_value = ResponseMock._response_mock(
138138
200, content=json.dumps(data), content_type="application/json"
139139
)
140-
REGISTRY.ack_opportunity(data['id'])
140+
REGISTRY.ack_notification(data['id'])
141141

142142
assert ack_method.call_count == 1
143143

144-
# Make sure we called parent `ack_opportunities` method with
144+
# Make sure we called parent `ack_notifications` method with
145145
# [ data['id'] ]
146146
assert ack_method.call_args == (([data['id']],),)
147147

148148

149149
@patch.object(requests.Session, "post")
150-
def test_ack_opportunities(session_post):
151-
"""list opportunities should 'ack' an opportunity returned
150+
def test_ack_notifications(session_post):
151+
"""receive notifications should 'ack' an opportunity returned
152152
that it cannot convert into an Opportunity object"""
153153

154-
data = [x['id'] for x in TEST_DATA['test_list_opportunities']]
154+
data = [x['id'] for x in TEST_DATA['test_receive_notifications']]
155155

156156
session_post.return_value = ResponseMock._response_mock(
157157
204, content_type="application/json"
158158
)
159159

160-
REGISTRY.ack_opportunities(data)
160+
REGISTRY.ack_notifications(data)
161161

162162
assert session_post.call_count == 1
163163
assert session_post.call_args[1]['data'] == json.dumps(data)

0 commit comments

Comments
 (0)