CHanges in `Connection()` (medium-sized) and `Stream()` (extra small)
[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:
00fc7963
MM
7 """This is the client class used to interact with Diaspora.
8 It can be used as a reference implementation of client using diaspy.
a993a4b6 9 """
c1490af7 10 def __init__(self, pod, username='', password=''):
a7661afd 11 """
c1490af7
MM
12 `pod` can also be a diaspy.connection.Connection type and
13 Client() will detect it. When giving a connection there is no need
14 to pass username and password.
15
16 :param pod: The complete url of the diaspora pod to use
17 (or Connection() object).
a7661afd
MK
18 :type pod: str
19 :param username: The username used to log in.
20 :type username: str
21 :param password: The password used to log in.
22 :type password: str
a7661afd 23 """
c1490af7
MM
24 if type(pod) == diaspy.connection.Connection:
25 self.connection = pod
26 else:
27 self.connection = diaspy.connection.Connection(pod, username, password)
28 self.connection.login()
1232dac5 29 self.stream = diaspy.streams.Stream(self.connection, 'stream.json')
313c9233 30
a6d4e76e 31 def post(self, text, aspect_ids='public', photos=None, photo=''):
a993a4b6
MK
32 """This function sends a post to an aspect
33
a6d4e76e 34 :param text: text to post
a993a4b6 35 :type text: str
9088535d
MK
36 :param aspect_ids: Aspect ids to send post to.
37 :type aspect_ids: str
a6d4e76e
MM
38 :param photo: path to picture file
39 :type photo: str
40
95d2d310 41 :returns: diaspy.models.Post -- the Post which has been created
a993a4b6 42 """
a6d4e76e 43 post = self.stream.post(text, aspect_ids, photos, photo)
9fae7c88 44 return post
a993a4b6 45
1232dac5
MM
46 def get_activity(self):
47 """This function returns activity stream.
91ce08c0 48
1232dac5
MM
49 :returns: diaspy.streams.Activity
50 """
51 activity = diaspy.streams.Activity(self.connection, 'activity.json')
52 return activity
53
b356c9f9 54 def get_stream(self):
1232dac5 55 """This functions returns stream.
b356c9f9 56
1232dac5 57 :returns: diaspy.streams.Stream
b356c9f9 58 """
f2eaa3c7
MM
59 self.stream.update()
60 return self.stream
33f21ecf 61
91ce08c0 62 def get_aspects(self):
fcb4d06b 63 """Returns aspects stream.
33f21ecf 64
91ce08c0 65 :returns: diaspy.streams.Aspects
33f21ecf 66 """
91ce08c0 67 return diaspy.streams.Aspects(self.connection)
3d3dff8f 68
3d3dff8f 69 def get_mentions(self):
91ce08c0 70 """Returns /mentions stream.
3d3dff8f 71
91ce08c0 72 :returns: diaspy.streams.Mentions
3d3dff8f 73 """
91ce08c0 74 return diaspy.streams.Mentions(self.connection)
3d3dff8f 75
91ce08c0
MM
76 def get_followed_tags(self):
77 """Returns followed tags stream.
3d3dff8f 78
91ce08c0
MM
79 :returns: diaspy.streams.FollowedTags
80 """
81 return diaspy.streams.FollowedTags(self.connection)
5c2b6162 82
fcb4d06b 83 def get_tag(self, tag):
4685fc31
MK
84 """This functions returns a list of posts containing the tag.
85 :param tag: Name of the tag
86 :type tag: str
87
fcb4d06b 88 :returns: diaspy.streams.Generic -- stream containg posts with given tag
4685fc31 89 """
fcb4d06b 90 return diaspy.streams.Generic(self.connection, location='tags/{0}.json'.format(tag))
91ce08c0
MM
91
92 def get_notifications(self):
93 """This functions returns a list of notifications.
94
95 :returns: list -- list of json formatted notifications
96 """
97 r = self.connection.get('notifications.json')
4685fc31
MK
98
99 if r.status_code != 200:
d930e127 100 raise Exception('wrong status code: {0}'.format(r.status_code))
4685fc31 101
91ce08c0
MM
102 notifications = r.json()
103 return notifications
4685fc31 104
264336e2
MM
105 def get_mailbox(self):
106 """This functions returns a list of messages found in the conversation.
107
108 :returns: list -- list of Conversation objects.
109 """
b95ffe83 110 r = self.connection.get('conversations.json')
264336e2
MM
111
112 if r.status_code != 200:
113 raise Exception('wrong status code: {0}'.format(r.status_code))
114
115 mailbox = r.json()
385e7ebe 116 return [diaspy.conversations.Conversation(str(conversation['conversation']['id']), self.connection)
490a69d0 117 for conversation in mailbox]
264336e2 118
90bf7079
MM
119 def add_aspect(self, aspect_name, visible=0):
120 """This function adds a new aspect.
121 """
122 diaspy.streams.Aspects(self.connection).add(aspect_name, visible)
123
124 def remove_aspect(self, aspect_id):
125 """This function removes an aspect.
126 """
127 diaspy.streams.Aspects(self.connection).remove(aspect_id)
128
5c2b6162
MK
129 def add_user_to_aspect(self, user_id, aspect_id):
130 """ this function adds a user to an aspect.
131
132 :param user_id: User ID
133 :type user_id: str
134 :param aspect_id: Aspect ID
135 :type aspect_id: str
136
137 """
90bf7079 138 return diaspy.models.Aspect(self.connection, aspect_id).addUser(user_id)
e475a9d0 139
5c2b6162
MK
140 def remove_user_from_aspect(self, user_id, aspect_id):
141 """ this function removes a user from an aspect.
142
143 :param user_id: User ID
144 :type user_id: str
145 :param aspect_id: Aspect ID
146 :type aspect_id: str
147
148 """
90bf7079 149 return diaspy.models.Aspect(self.connection, aspect_id).removeUser(user_id)
91d2d5dc 150
91d2d5dc 151 def new_conversation(self, contacts, subject, text):
264336e2 152 """Start a new conversation.
91d2d5dc
B
153
154 :param contacts: recipients ids, no guids, comma sperated.
155 :type contacts: str
156 :param subject: subject of the message.
157 :type subject: str
158 :param text: text of the message.
159 :type text: str
91d2d5dc 160 """
91d2d5dc
B
161 data = {'contact_ids': contacts,
162 'conversation[subject]': subject,
163 'conversation[text]': text,
164 'utf8': '✓',
59ad210c 165 'authenticity_token': self.connection.get_token()}
91d2d5dc 166
b95ffe83 167 r = self.connection.post('conversations/',
385e7ebe
MM
168 data=data,
169 headers={'accept': 'application/json'})
91d2d5dc 170 if r.status_code != 200:
9088535d
MK
171 raise Exception('{0}: Conversation could not be started.'
172 .format(r.status_code))
91d2d5dc 173 return r.json()