summaryrefslogtreecommitdiff
path: root/inboxapp-service/app/inboxapp/client.py
blob: cf96a4982ff52fac911179a537db39c480768f77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import json
import urllib2
import requests


class Client:

    INBOX_APP_ROOT = 'http://localhost:5555/n'

    def _get_user(self, account):
        accounts = json.load(urllib2.urlopen(self.INBOX_APP_ROOT))
        return [a for a in accounts if a['email_address'] == account][0]

    def __init__(self, account):
        self.user = self._get_user(account)
        self.namespace = self.user['namespace']

    def _get(self, append_url):
        url = "%s/%s/%s" % (self.INBOX_APP_ROOT, self.namespace, append_url)
        return requests.get(url).json()

    def _post(self, append_url, body):
        url = "%s/%s/%s" % (self.INBOX_APP_ROOT, self.namespace, append_url)
        return requests.post(url, json.dumps(body)).json()

    def _put(self, append_url, body):
        url = "%s/%s/%s" % (self.INBOX_APP_ROOT, self.namespace, append_url)
        return requests.put(url, json.dumps(body)).json()

    def mails(self, query):
        url = "messages"
        if('tags' in query and len(query['tags']) > 0):
            url = url + "?tag=%s" % ",".join(query['tags'])
        return self._get(url)

    def drafts(self):
        return self._get("drafts")

    def mail(self, mail_id):
        return self._get("messages/%s" % mail_id)

    def thread(self, thread_id):
        return self._get("threads/%s" % thread_id)

    def mark_as_read(self, mail_id):
        mail_to_mark = self.mail(mail_id)
        self._put("messages/%s" % mail_id, {"unread": False})
        self.remove_tag_from_thread(mail_to_mark["thread"], "unread")

    def tags_for_thread(self, thread):
        url = "threads/%s" % thread
        tags = self._get(url)['tags']
        return [tag['name'] for tag in tags]

    def add_tag_to_thread(self, thread_id, tag):
        url = "threads/%s" % thread_id
        response = self._put(url, {'add_tags': [tag]})
        return response

    def remove_tag_from_thread(self, thread_id, tag):
        url = "threads/%s" % thread_id
        response = self._put(url, {'remove_tags': [tag]})
        return response

    def delete_mail(self, mail_id):
        thread_id = self.mail(mail_id)['thread']
        tags = self.tags_for_thread(thread_id)
        if('trash' in tags):
            self.add_tag_to_thread(thread_id, 'delete')
        else:
            self.add_tag_to_thread(thread_id, 'trash')
        return None

    def save_draft(self, draft):
        if 'id' in draft and draft['id']:
            url = 'drafts/%s' % draft["id"]
        else:
            url = "drafts"
        result = self._post(url, draft)
        self.mark_as_read(result['id'])
        return result['id']

    def send_draft(self, draft):
        new_draft_id = self.save_draft(draft)
        response = self._post("send", {"draft_id": new_draft_id})
        return response

    def draft_reply_for(self, mail_id):
        thread = self.thread(self.mail(mail_id)["thread"])
        if thread['drafts']:
            response = self.mail(thread['drafts'][0])
        else:
            response = None
        return response

    def all_tags(self):
        return self._get("tags")

    def all_contacts(self, query):
        return self._get("contacts?filter=%s&order_by=rank" % query['general'])