Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 163 lines (135 sloc) 6.253 kB
415052f bumped changelog, refactored tests and methods, fixed bug in find met…
Edward F. Long, Jr authored
1 import aweber_api
0a51ea7 Initial commit
Russ P authored
2 from aweber_api.response import AWeberResponse
b3a20a0 Added ability to delete and update subscribers
Russ P authored
3 from aweber_api.data_dict import DataDict
415052f bumped changelog, refactored tests and methods, fixed bug in find met…
Edward F. Long, Jr authored
4
879455b @borisvaweber Added findSubscriber and get_parent_entry
borisvaweber authored
5 from urllib import urlencode
0a51ea7 Initial commit
Russ P authored
6
5660533 updated documentation and version bump
Edward F. Long, Jr authored
7
0a51ea7 Initial commit
Russ P authored
8 class AWeberEntry(AWeberResponse):
9 """
10 Represents a single entry in the AWeber API data heirarchy, such as one
11 specific account, list, web form, etc. Built on data that is returned
12 from an id-ed URI, such as:
13 /accounts/XXXX
14 /accounts/XXXX/lists/XXXX
15 Can also be generated from the data in the entries array of a collection
16 object, which is identical to the data return from the URI for that
17 specific entry.
18
19 Provides direct access to properties in the response, such as self.id
20 """
21
22 def __init__(self, url, data, adapter):
b3a20a0 Added ability to delete and update subscribers
Russ P authored
23 self._data = {}
24 self._diff = {}
0a51ea7 Initial commit
Russ P authored
25 AWeberResponse.__init__(self, url, data, adapter)
26 self._child_collections = {}
27
b3a20a0 Added ability to delete and update subscribers
Russ P authored
28 def __setattr__(self, key, value):
29 if not key == '_data' and key in self._data:
30 self._data[key] = value
31 self._diff[key] = value
32 return value
33 return AWeberResponse.__setattr__(self, key, value)
34
35 def delete(self):
5323394 added support for move operation on entries. refactored MockAdapter a…
Edward F. Long, Jr authored
36 """Invoke the API method to DELETE* this entry resource.
37
38 * Note: Not all entry resources are eligible to be DELETED, please
39 refer to the AWeber API Reference Documentation at
40 https://labs.aweber.com/docs/reference/1.0 for more
41 details on which entry resources may be deleted.
42 """
415052f bumped changelog, refactored tests and methods, fixed bug in find met…
Edward F. Long, Jr authored
43 self.adapter.request('DELETE', self.url, response='status')
44 return True
b3a20a0 Added ability to delete and update subscribers
Russ P authored
45
5323394 added support for move operation on entries. refactored MockAdapter a…
Edward F. Long, Jr authored
46 def move(self, list_):
1b329bf fixed typo in location header, updated documentation.
Edward F. Long, Jr authored
47 """Invoke the API method to MOVE an entry resource to a
5323394 added support for move operation on entries. refactored MockAdapter a…
Edward F. Long, Jr authored
48 different List.
49
1b329bf fixed typo in location header, updated documentation.
Edward F. Long, Jr authored
50 Note: Not all entry resources are eligible to be moved, please
51 refer to the AWeber API Reference Documentation at
52 https://labs.aweber.com/docs/reference/1.0 for more
53 details on which entry resources may be moved and if there
54 are any requirements for moving that resource.
5323394 added support for move operation on entries. refactored MockAdapter a…
Edward F. Long, Jr authored
55 """
56 params = {'ws.op': 'move',
57 'list_link': list_.self_link}
1a9ca16 updated code to properly initiate the move request.
Edward F. Long, Jr authored
58 response = self.adapter.request('POST', self.url, params,
59 response='headers')
415052f bumped changelog, refactored tests and methods, fixed bug in find met…
Edward F. Long, Jr authored
60
1b329bf fixed typo in location header, updated documentation.
Edward F. Long, Jr authored
61 new_resource = response['location']
5323394 added support for move operation on entries. refactored MockAdapter a…
Edward F. Long, Jr authored
62 self._diff = {}
63 self._data = self.adapter.request('GET', new_resource)
64 return True
65
b3a20a0 Added ability to delete and update subscribers
Russ P authored
66 def save(self):
415052f bumped changelog, refactored tests and methods, fixed bug in find met…
Edward F. Long, Jr authored
67 self.adapter.request('PATCH', self.url, self._diff, response='status')
b3a20a0 Added ability to delete and update subscribers
Russ P authored
68 self._diff = {}
415052f bumped changelog, refactored tests and methods, fixed bug in find met…
Edward F. Long, Jr authored
69 return True
0a51ea7 Initial commit
Russ P authored
70
a95d6a9 added support for subscriber getActivity named operation.
Edward F. Long, Jr authored
71 def get_activity(self):
72 """Invoke the API method to return all Subscriber activity.
73
74 * Note: This method only works on Subscriber Entry resources.
75 refer to the AWeber API Reference Documentation at
76 https://labs.aweber.com/docs/reference/1.0#subscriber
77 for more details on how to call this method.
78 """
79 self._method_for('subscriber')
80 params = {'ws.op': 'getActivity'}
81 query_string = urlencode(params)
82 url = '{0.url}?{1}'.format(self, query_string)
83 data = self.adapter.request('GET', url)
84
415052f bumped changelog, refactored tests and methods, fixed bug in find met…
Edward F. Long, Jr authored
85 collection = aweber_api.AWeberCollection(url, data, self.adapter)
a95d6a9 added support for subscriber getActivity named operation.
Edward F. Long, Jr authored
86 collection._data['total_size'] = self._get_total_size(url)
87 return collection
88
879455b @borisvaweber Added findSubscriber and get_parent_entry
borisvaweber authored
89 def findSubscribers(self, **kwargs):
5660533 updated documentation and version bump
Edward F. Long, Jr authored
90 """Invoke the API method to find all subscribers on all Lists
91
92 * Note: This method only works on Account Entry resources and
93 requires access to subscriber information. please
94 refer to the AWeber API Reference Documentation at
95 https://labs.aweber.com/docs/reference/1.0#account
96 for more details on how to call this method.
97 """
879455b @borisvaweber Added findSubscriber and get_parent_entry
borisvaweber authored
98 self._method_for('account')
99 params = {'ws.op': 'findSubscribers'}
100 params.update(kwargs)
101 query_string = urlencode(params)
102 url = '{0.url}?{1}'.format(self, query_string)
103
415052f bumped changelog, refactored tests and methods, fixed bug in find met…
Edward F. Long, Jr authored
104 data = self.adapter.request('GET', url)
105 collection = aweber_api.AWeberCollection(url, data, self.adapter)
879455b @borisvaweber Added findSubscriber and get_parent_entry
borisvaweber authored
106 collection._data['total_size'] = self._get_total_size(url)
107 return collection
108
109 def _get_total_size(self, uri, **kwargs):
110 """Get actual total size number from total_size_link."""
111 total_size_uri = '{0}&ws.show=total_size'.format(uri)
27c4902 fixed bug when calling __len__ on an empty collection returns a TypeE…
Edward F. Long, Jr authored
112 return int(self.adapter.request('GET', total_size_uri))
879455b @borisvaweber Added findSubscriber and get_parent_entry
borisvaweber authored
113
114 def get_parent_entry(self):
5660533 updated documentation and version bump
Edward F. Long, Jr authored
115 """Return the parent entry of this entry or None if no parent exists.
116
117 Example:
118 calling get_parent_entry on a SubscriberEntry will return the List
119 Entry that SubscriberEntry belongs to. For more information on
120 the AWeber API and how resources are arranged, refer to:
121 https://labs.aweber.com/docs/reference/1.0
122 """
879455b @borisvaweber Added findSubscriber and get_parent_entry
borisvaweber authored
123 url_parts = self.url.split('/')
124 size = len(url_parts)
125 url = self.url[:-len(url_parts[size-1])-1]
126 url = url[:-len(url_parts[size-2])-1]
309d9f1 refactored client library to raise exceptions when 40x and 50x errors…
Edward F. Long, Jr authored
127
128 if url == '':
879455b @borisvaweber Added findSubscriber and get_parent_entry
borisvaweber authored
129 return None
309d9f1 refactored client library to raise exceptions when 40x and 50x errors…
Edward F. Long, Jr authored
130
131 data = self.adapter.request('GET', url)
132 return AWeberEntry(url, data, self.adapter)
879455b @borisvaweber Added findSubscriber and get_parent_entry
borisvaweber authored
133
0a51ea7 Initial commit
Russ P authored
134 def get_web_forms(self):
135 self._method_for('account')
136 data = self.adapter.request('GET',
137 "{0}?ws.op=getWebForms".format(self.url))
138 return self._parseNamedOperation(data)
139
879455b @borisvaweber Added findSubscriber and get_parent_entry
borisvaweber authored
140 def get_web_form_split_tests(self):
141 self._method_for('account')
142 data = self.adapter.request(
143 'GET',
144 "{0}?ws.op=getWebFormSplitTests".format(self.url),
145 )
146 return self._parseNamedOperation(data)
147
0a51ea7 Initial commit
Russ P authored
148 def _child_collection(self, attr):
149 if not attr in self._child_collections:
150 url = "{0}/{1}".format(self.url, attr)
151 self._child_collections[attr] = self.load_from_url(url)
152 return self._child_collections[attr]
153
154 def __getattr__(self, attr):
155 if attr in self._data:
58ab4f7 Replaced type() == with isinstance, added comments for DataDict
Russ P authored
156 if isinstance(self._data[attr], dict):
b3a20a0 Added ability to delete and update subscribers
Russ P authored
157 return DataDict(self._data[attr], attr, self)
0a51ea7 Initial commit
Russ P authored
158 return self._data[attr]
159 elif attr in self.collections_map[self.type]:
160 return self._child_collection(attr)
161 else:
162 raise AttributeError(attr)
Something went wrong with that request. Please try again.