Style check (flake8)
[diaspy.git] / diaspy / client.py
1 import diaspy.models
2 import diaspy.streams
3 import diaspy.connection
4
5
6 class Client:
7 """This is the client class used to interact with Diaspora.
8 It can be used as a reference implementation of client using diaspy.
9 """
10 def __init__(self, pod, username='', password=''):
11 """
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).
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
23 """
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()
29 self.stream = diaspy.streams.Stream(self.connection, 'stream.json')
30
31 def post(self, text, aspect_ids='public', photos=None, photo=''):
32 """This function sends a post to an aspect
33
34 :param text: text to post
35 :type text: str
36 :param aspect_ids: Aspect ids to send post to.
37 :type aspect_ids: str
38 :param photo: path to picture file
39 :type photo: str
40 :returns: diaspy.models.Post -- the Post which has been created
41 """
42 post = self.stream.post(text, aspect_ids, photos, photo)
43 return post
44
45 def get_activity(self):
46 """This function returns activity stream.
47
48 :returns: diaspy.streams.Activity
49 """
50 activity = diaspy.streams.Activity(self.connection, 'activity.json')
51 return activity
52
53 def get_stream(self):
54 """This functions returns stream.
55
56 :returns: diaspy.streams.Stream
57 """
58 self.stream.update()
59 return self.stream
60
61 def get_aspects(self):
62 """Returns aspects stream.
63
64 :returns: diaspy.streams.Aspects
65 """
66 return diaspy.streams.Aspects(self.connection)
67
68 def get_mentions(self):
69 """Returns /mentions stream.
70
71 :returns: diaspy.streams.Mentions
72 """
73 return diaspy.streams.Mentions(self.connection)
74
75 def get_followed_tags(self):
76 """Returns followed tags stream.
77
78 :returns: diaspy.streams.FollowedTags
79 """
80 return diaspy.streams.FollowedTags(self.connection)
81
82 def get_tag(self, tag):
83 """This functions returns a list of posts containing the tag.
84 :param tag: Name of the tag
85 :type tag: str
86
87 :returns: diaspy.streams.Generic -- stream containg posts with given tag
88 """
89 return diaspy.streams.Generic(self.connection, location='tags/{0}.json'.format(tag))
90
91 def get_notifications(self):
92 """This functions returns a list of notifications.
93
94 :returns: list -- list of json formatted notifications
95 """
96 r = self.connection.get('notifications.json')
97
98 if r.status_code != 200:
99 raise Exception('wrong status code: {0}'.format(r.status_code))
100
101 notifications = r.json()
102 return notifications
103
104 def get_mailbox(self):
105 """This functions returns a list of messages found in the conversation.
106
107 :returns: list -- list of Conversation objects.
108 """
109 r = self.connection.get('conversations.json')
110
111 if r.status_code != 200:
112 raise Exception('wrong status code: {0}'.format(r.status_code))
113
114 mailbox = r.json()
115 return [diaspy.conversations.Conversation(str(conversation['conversation']['id']), self.connection)
116 for conversation in mailbox]
117
118 def add_aspect(self, aspect_name, visible=0):
119 """This function adds a new aspect.
120 """
121 diaspy.streams.Aspects(self.connection).add(aspect_name, visible)
122
123 def remove_aspect(self, aspect_id):
124 """This function removes an aspect.
125 """
126 diaspy.streams.Aspects(self.connection).remove(aspect_id)
127
128 def add_user_to_aspect(self, user_id, aspect_id):
129 """ this function adds a user to an aspect.
130
131 :param user_id: User ID
132 :type user_id: str
133 :param aspect_id: Aspect ID
134 :type aspect_id: str
135
136 """
137 return diaspy.models.Aspect(self.connection, aspect_id).addUser(user_id)
138
139 def remove_user_from_aspect(self, user_id, aspect_id):
140 """ this function removes a user from an aspect.
141
142 :param user_id: User ID
143 :type user_id: str
144 :param aspect_id: Aspect ID
145 :type aspect_id: str
146
147 """
148 return diaspy.models.Aspect(self.connection, aspect_id).removeUser(user_id)
149
150 def new_conversation(self, contacts, subject, text):
151 """Start a new conversation.
152
153 :param contacts: recipients ids, no guids, comma sperated.
154 :type contacts: str
155 :param subject: subject of the message.
156 :type subject: str
157 :param text: text of the message.
158 :type text: str
159 """
160 data = {'contact_ids': contacts,
161 'conversation[subject]': subject,
162 'conversation[text]': text,
163 'utf8': '✓',
164 'authenticity_token': self.connection.get_token()}
165
166 r = self.connection.post('conversations/',
167 data=data,
168 headers={'accept': 'application/json'})
169 if r.status_code != 200:
170 raise Exception('{0}: Conversation could not be started.'
171 .format(r.status_code))
172 return r.json()