Rename getToken() to get_token()
[diaspy.git] / diaspy / client.py
CommitLineData
95d2d310 1import diaspy.models
1232dac5 2import diaspy.streams
8993810c 3import diaspy.connection
a993a4b6 4
4685fc31 5
a993a4b6 6class Client:
d930e127 7 """This is the client class to connect to Diaspora.
a993a4b6 8 """
a7661afd
MK
9 def __init__(self, pod, username, password):
10 """
11 :param pod: The complete url of the diaspora pod to use.
12 :type pod: str
13 :param username: The username used to log in.
14 :type username: str
15 :param password: The password used to log in.
16 :type password: str
a7661afd 17 """
8993810c
MM
18 self.connection = diaspy.connection.Connection(pod, username, password)
19 self.connection.login()
a993a4b6 20 self.pod = pod
1232dac5 21 self.stream = diaspy.streams.Stream(self.connection, 'stream.json')
313c9233 22
9088535d 23 def post(self, text, aspect_ids='public', photos=None):
a993a4b6
MK
24 """This function sends a post to an aspect
25
26 :param text: Text to post.
27 :type text: str
9088535d
MK
28 :param aspect_ids: Aspect ids to send post to.
29 :type aspect_ids: str
a993a4b6 30
95d2d310 31 :returns: diaspy.models.Post -- the Post which has been created
a993a4b6 32 """
f2eaa3c7 33 post = self.stream.post(text, aspect_ids, photos)
9fae7c88 34 return post
a993a4b6 35
28ca595a 36 def post_picture(self, filename):
a1a09a06
MM
37 """This method posts a picture to D*.
38
39 :param filename: Path to picture file.
40 :type filename: str
41 """
f2eaa3c7 42 return self.stream.post_picture(filename)
28ca595a 43
1232dac5
MM
44 def get_activity(self):
45 """This function returns activity stream.
91ce08c0 46
1232dac5
MM
47 :returns: diaspy.streams.Activity
48 """
49 activity = diaspy.streams.Activity(self.connection, 'activity.json')
50 return activity
51
b356c9f9 52 def get_stream(self):
1232dac5 53 """This functions returns stream.
b356c9f9 54
1232dac5 55 :returns: diaspy.streams.Stream
b356c9f9 56 """
f2eaa3c7
MM
57 self.stream.update()
58 return self.stream
33f21ecf 59
91ce08c0
MM
60 def get_aspects(self):
61 """Returns /aspects stream.
33f21ecf 62
91ce08c0 63 :returns: diaspy.streams.Aspects
33f21ecf 64 """
91ce08c0 65 return diaspy.streams.Aspects(self.connection)
3d3dff8f 66
3d3dff8f 67 def get_mentions(self):
91ce08c0 68 """Returns /mentions stream.
3d3dff8f 69
91ce08c0 70 :returns: diaspy.streams.Mentions
3d3dff8f 71 """
91ce08c0 72 return diaspy.streams.Mentions(self.connection)
3d3dff8f 73
91ce08c0
MM
74 def get_followed_tags(self):
75 """Returns followed tags stream.
3d3dff8f 76
91ce08c0
MM
77 :returns: diaspy.streams.FollowedTags
78 """
79 return diaspy.streams.FollowedTags(self.connection)
5c2b6162 80
91ce08c0 81 def get_tag(self, tag, stream=False):
4685fc31
MK
82 """This functions returns a list of posts containing the tag.
83 :param tag: Name of the tag
84 :type tag: str
91ce08c0
MM
85 :param stream: specify wheter you want a stream object (True) or
86 normal list (False)
87 :type stream: bool
4685fc31
MK
88
89 :returns: list -- list of Post objects
4685fc31 90 """
91ce08c0 91 if stream:
62f1912f
MM
92 tagged_posts = diaspy.streams.Generic(self.connection, location='tags/{0}.json'.format(tag))
93 else:
91ce08c0
MM
94 r = self.connection.get('tags/{0}.json'.format(tag))
95 if r.status_code != 200:
96 raise Exception('wrong status code: {0}'.format(r.status_code))
97 tagged_posts = [diaspy.models.Post(str(post['id']), self.connection) for post in r.json()]
91ce08c0
MM
98 return tagged_posts
99
100 def get_notifications(self):
101 """This functions returns a list of notifications.
102
103 :returns: list -- list of json formatted notifications
104 """
105 r = self.connection.get('notifications.json')
4685fc31
MK
106
107 if r.status_code != 200:
d930e127 108 raise Exception('wrong status code: {0}'.format(r.status_code))
4685fc31 109
91ce08c0
MM
110 notifications = r.json()
111 return notifications
4685fc31 112
264336e2
MM
113 def get_mailbox(self):
114 """This functions returns a list of messages found in the conversation.
115
116 :returns: list -- list of Conversation objects.
117 """
b95ffe83 118 r = self.connection.get('conversations.json')
264336e2
MM
119
120 if r.status_code != 200:
121 raise Exception('wrong status code: {0}'.format(r.status_code))
122
123 mailbox = r.json()
385e7ebe 124 return [diaspy.conversations.Conversation(str(conversation['conversation']['id']), self.connection)
490a69d0 125 for conversation in mailbox]
264336e2 126
5c2b6162
MK
127 def add_user_to_aspect(self, user_id, aspect_id):
128 """ this function adds a user to an aspect.
129
130 :param user_id: User ID
131 :type user_id: str
132 :param aspect_id: Aspect ID
133 :type aspect_id: str
134
135 """
59ad210c 136 data = {'authenticity_token': self.connection.get_token(),
5c2b6162
MK
137 'aspect_id': aspect_id,
138 'person_id': user_id}
139
b95ffe83 140 r = self.connection.post('aspect_memberships.json', data=data)
5c2b6162
MK
141
142 if r.status_code != 201:
d930e127 143 raise Exception('wrong status code: {0}'.format(r.status_code))
5c2b6162
MK
144 return r.json()
145
e475a9d0
MM
146 def add_aspect(self, aspect_name, visible=0):
147 """ This function adds a new aspect.
148 """
505fc964
MM
149 aspects = diaspy.streams.Aspects(self.connection)
150 aspects.add(aspect_name, visible)
e475a9d0 151
5c2b6162
MK
152 def remove_user_from_aspect(self, user_id, aspect_id):
153 """ this function removes a user from an aspect.
154
155 :param user_id: User ID
156 :type user_id: str
157 :param aspect_id: Aspect ID
158 :type aspect_id: str
159
160 """
59ad210c 161 data = {'authenticity_token': self.connection.get_token(),
5c2b6162
MK
162 'aspect_id': aspect_id,
163 'person_id': user_id}
164
b95ffe83 165 r = self.connection.delete('aspect_memberships/42.json',
385e7ebe 166 data=data)
5c2b6162
MK
167
168 if r.status_code != 200:
d930e127 169 raise Exception('wrong status code: {0}'.format(r.status_code))
5c2b6162
MK
170
171 return r.json()
22cb1646 172
22cb1646
MK
173 def remove_aspect(self, aspect_id):
174 """ This function adds a new aspect.
175 """
59ad210c 176 data = {'authenticity_token': self.connection.get_token()}
22cb1646 177
b95ffe83 178 r = self.connection.delete('aspects/{}'.format(aspect_id),
385e7ebe 179 data=data)
22cb1646
MK
180
181 if r.status_code != 404:
d930e127 182 raise Exception('wrong status code: {0}'.format(r.status_code))
91d2d5dc 183
91d2d5dc 184 def new_conversation(self, contacts, subject, text):
264336e2 185 """Start a new conversation.
91d2d5dc
B
186
187 :param contacts: recipients ids, no guids, comma sperated.
188 :type contacts: str
189 :param subject: subject of the message.
190 :type subject: str
191 :param text: text of the message.
192 :type text: str
91d2d5dc 193 """
91d2d5dc
B
194 data = {'contact_ids': contacts,
195 'conversation[subject]': subject,
196 'conversation[text]': text,
197 'utf8': '✓',
59ad210c 198 'authenticity_token': self.connection.get_token()}
91d2d5dc 199
b95ffe83 200 r = self.connection.post('conversations/',
385e7ebe
MM
201 data=data,
202 headers={'accept': 'application/json'})
91d2d5dc 203 if r.status_code != 200:
9088535d
MK
204 raise Exception('{0}: Conversation could not be started.'
205 .format(r.status_code))
91d2d5dc 206 return r.json()