Skip to content

Commit 8a1ee55

Browse files
author
Rasmus Lager
committed
readmefile update to show attributes and PaginatedResultClass
1 parent 402f8f9 commit 8a1ee55

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

README.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,26 @@ except RequestException as e:
6464
print 'API error: ' + e
6565

6666

67+
```
68+
69+
### Querying for contacts
70+
You have to create an instance of the ```ContactManager``` class and then use it's ```query()``` method to retrieve the contacts you need.
71+
```query()``` takes a dict of [url parameters](https://api.getanewsletter.com/v3/docs/contacts/#get-contacts) and will return a ```PaginatedResultSet``` with the first page of contacts in a list in PaginatedResultSet.entities.
72+
73+
```python
74+
# Get PaginatedResultSet containing contacts with emails starting with name@.
75+
queried_contacts = contact_manager.query({'search_email': 'name@'})
76+
77+
# list of contacts in current page(1)
78+
queried_contacts.entities
79+
80+
# list of contacts in next page(2) if available and update PaginatedResultSet
81+
queried_contacts.next()
82+
83+
# list of contacts in previous page(1) if available and update PaginatedResultSet
84+
queried_contacts.prev()
85+
86+
6787
```
6888

6989
#### Creating a contact
@@ -187,3 +207,118 @@ contact.email = 'john.doe@example.com'
187207
contact.subscribe_to(list)
188208
contact.save()
189209
```
210+
211+
212+
#### The attribute object
213+
The instances of the Attribute class represent the attribute entities in the API.
214+
They have the following fields:
215+
216+
*Required fields*
217+
* ```name``` - attribute name.
218+
219+
*Lookup field*
220+
* ```code``` - the slugified attribute code. required when updating or deleting the attribute. *The name "A new attribute" code will be "a-new-attribute"*
221+
222+
*Read-only fields*
223+
* ```url``` - the attribute resource URL.
224+
* ```usage_count``` - number of usages.
225+
226+
227+
#### Retreiving an attribute
228+
You have to create an instance of the ```AttributeManager``` class and then use it's ```get()``` method to retrieve the attribute you need.
229+
```python
230+
from ganapi import AttributeManager
231+
232+
attribute_manager = AttributeManager(gan)
233+
attribute = attribute_manager.get('code')
234+
```
235+
The manager methods will throw an ```RequestException``` in case of HTTP error from the API, so it's a good idea to catch it.
236+
```python
237+
238+
try:
239+
attribute = attribute_manager.get('code')
240+
except RequestException as e:
241+
if e.response.code == 404:
242+
print 'Attribute not found!'
243+
else:
244+
print 'API error: ' + e
245+
246+
247+
```
248+
249+
#### Creating an attribute
250+
```python
251+
252+
attribute = attribute_manager.create()
253+
attribute.name = 'City'
254+
255+
attribute.save()
256+
```
257+
This will create a new attribute and save it. Again, it'll be a good idea to catch exceptions when calling the ```save()``` method. The API will respond with an error if the contact already exists.
258+
One way to avoid it is to force the creation of the contact, overwriting the existing one:
259+
```python
260+
261+
attribute.overwrite()
262+
```
263+
264+
Both ```save()``` and ```overwrite()``` will return the same contact object with it's read-only fields updated (e.g. ```url```, ```usage_count```).
265+
266+
```python
267+
268+
attribute = attribute.save()
269+
print attribute.usage_count
270+
```
271+
272+
#### Updating an existing attribute
273+
```python
274+
275+
# Get the attribute.
276+
attribute = attribute_manager.get('code')
277+
# Change name field.
278+
attribute.name = 'Changed!'
279+
# Save it.
280+
attribute.save()
281+
```
282+
You can avoid making two calls to the API by forcing a *partial update*.
283+
```python
284+
285+
attribute = attribute_manager.create()
286+
attribute.set_persisted()
287+
attribute.code = 'code'
288+
attribute.name = 'Changed!'
289+
attribute.save()
290+
```
291+
Calling ```set_persisted()``` on the attribute object marks it like it's already existing and coming from the API. The calls to the ```save()``` method when a contact is made as existing will do only a *partial update*, i.e. update only the supplied fields and skipping all the ```None``` fields.
292+
Do not forget that ```code``` is a ***_lookup field_*** and required when updating or deleting the attribute.
293+
294+
#### Deleting an attribute
295+
```python
296+
297+
attribute.delete()
298+
```
299+
300+
301+
302+
#### The PaginatedResultSet class
303+
The instance of the PaginatedResultSet class represent the result of get from the API.
304+
305+
*Parameters*
306+
* ```data``` - json of result from the ```query()``` method in the managers.
307+
* ```manager``` - the manager using ```query()```.
308+
309+
*Properties*
310+
* ```next_link``` - URL to get the next page of the results.
311+
* ```previous_link``` - URL to get the previous page of the results.
312+
* ```manager``` - ```EntityManager``` currently used.
313+
* ```entities``` - List of ```Entity``` for the current result page.
314+
* ```count``` - Total amount of entity results in all pages.
315+
316+
*Methods*
317+
* ```prev()``` - replaces the instance with results from the previous page and returns entities.
318+
* ```next()``` - replaces the instance with results from the next page and returns entities.
319+
320+
```next()``` and ```prev()``` will raise ```StopIteration``` if the end of the direction has been reached.
321+
They will raise ```RequestException``` in case of HTTP error from the API,
322+
323+
324+

ganapi/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from api import Api
2+
from attribute_manager import AttributeManager
3+
from contact_manager import ContactManager
4+
from list_manager import ListManager
5+
from attribute import Attribute
6+
from contact import Contact
7+
from list import List

0 commit comments

Comments
 (0)