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