Add inboxapp temporary service
[pixelated-user-agent.git] / inboxapp-service / app / inboxapp / client.py
1 import json
2 import urllib2
3 import requests
4
5
6 class Client:
7
8     INBOX_APP_ROOT = 'http://localhost:5555/n'
9
10     def _get_user(self, account):
11         accounts = json.load(urllib2.urlopen(self.INBOX_APP_ROOT))
12         return [a for a in accounts if a['email_address'] == account][0]
13
14     def __init__(self, account):
15         self.user = self._get_user(account)
16         self.namespace = self.user['namespace']
17
18     def _get(self, append_url):
19         url = "%s/%s/%s" % (self.INBOX_APP_ROOT, self.namespace, append_url)
20         return requests.get(url).json()
21
22     def _post(self, append_url, body):
23         url = "%s/%s/%s" % (self.INBOX_APP_ROOT, self.namespace, append_url)
24         return requests.post(url, json.dumps(body)).json()
25
26     def _put(self, append_url, body):
27         url = "%s/%s/%s" % (self.INBOX_APP_ROOT, self.namespace, append_url)
28         return requests.put(url, json.dumps(body)).json()
29
30     def mails(self, query):
31         url = "messages"
32         if('tags' in query and len(query['tags']) > 0):
33             url = url + "?tag=%s" % ",".join(query['tags'])
34         return self._get(url)
35
36     def drafts(self):
37         return self._get("drafts")
38
39     def mail(self, mail_id):
40         return self._get("messages/%s" % mail_id)
41
42     def thread(self, thread_id):
43         return self._get("threads/%s" % thread_id)
44
45     def mark_as_read(self, mail_id):
46         mail_to_mark = self.mail(mail_id)
47         self._put("messages/%s" % mail_id, {"unread": False})
48         self.remove_tag_from_thread(mail_to_mark["thread"], "unread")
49
50     def tags_for_thread(self, thread):
51         url = "threads/%s" % thread
52         tags = self._get(url)['tags']
53         return [tag['name'] for tag in tags]
54
55     def add_tag_to_thread(self, thread_id, tag):
56         url = "threads/%s" % thread_id
57         response = self._put(url, {'add_tags': [tag]})
58         return response
59
60     def remove_tag_from_thread(self, thread_id, tag):
61         url = "threads/%s" % thread_id
62         response = self._put(url, {'remove_tags': [tag]})
63         return response
64
65     def delete_mail(self, mail_id):
66         thread_id = self.mail(mail_id)['thread']
67         tags = self.tags_for_thread(thread_id)
68         if('trash' in tags):
69             self.add_tag_to_thread(thread_id, 'delete')
70         else:
71             self.add_tag_to_thread(thread_id, 'trash')
72         return None
73
74     def save_draft(self, draft):
75         if 'id' in draft and draft['id']:
76             url = 'drafts/%s' % draft["id"]
77         else:
78             url = "drafts"
79         result = self._post(url, draft)
80         self.mark_as_read(result['id'])
81         return result['id']
82
83     def send_draft(self, draft):
84         new_draft_id = self.save_draft(draft)
85         response = self._post("send", {"draft_id": new_draft_id})
86         return response
87
88     def draft_reply_for(self, mail_id):
89         thread = self.thread(self.mail(mail_id)["thread"])
90         if thread['drafts']:
91             response = self.mail(thread['drafts'][0])
92         else:
93             response = None
94         return response
95
96     def all_tags(self):
97         return self._get("tags")
98
99     def all_contacts(self, query):
100         return self._get("contacts?filter=%s&order_by=rank" % query['general'])