You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+135Lines changed: 135 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -64,6 +64,26 @@ except RequestException as e:
64
64
print'API error: '+ e
65
65
66
66
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@.
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,
0 commit comments