Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

praekelt / go-http-api / 187

11 Nov 2015 - 8:48 coverage: 99.378%. First build
187

Pull #20

travis-ci

337cadae5be17c19e2defb615d7de9f8?size=18&default=identiconhodgestar
Merge branch 'develop' into feature/issue-20-optout-support
Pull Request #20: Add support for optout API

115 of 118 new or added lines in 3 files covered. (97.46%)

799 of 804 relevant lines covered (99.38%)

1.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.74
/go_http/optouts.py
1
""" Client for Vumi Go's opt out API.
2
"""
3

4
import json
2×
5
import urllib
2×
6

7
import requests
2×
8

9

10
class OptOutsApiClient(object):
2×
11
    """
12
    Client for Vumi Go's opt out API.
13

14
    :param str auth_token:
15
        An OAuth 2 access token.
16

17
    :param str api_url:
18
        The full URL of the HTTP API. Defaults to
19
        ``https://go.vumi.org/api/v1/go``.
20

21
    :type session:
22
        :class:`requests.Session`
23
    :param session:
24
        Requests session to use for HTTP requests. Defaults to a new session.
25
    """
26

27
    def __init__(self, auth_token, api_url=None, session=None):
2×
28
        self.auth_token = auth_token
2×
29
        if api_url is None:
2×
30
            api_url = "https://go.vumi.org/api/v1/go"
2×
31
        self.api_url = api_url.rstrip('/')
2×
32
        if session is None:
2×
33
            session = requests.Session()
2×
34
        self.session = session
2×
35

36
    def _api_request(self, method, path, data=None):
2×
37
        url = "%s/%s" % (self.api_url, urllib.quote(path))
2×
38
        headers = {
2×
39
            "Content-Type": "application/json; charset=utf-8",
40
            "Authorization": "Bearer %s" % (self.auth_token,),
41
        }
42
        if method is "GET" and data is not None:
2×
NEW
43
            r = self.session.request(method, url, params=data, headers=headers)
!
44
        else:
45
            if data is not None:
2×
NEW
46
                data = json.dumps(data)
!
47
            r = self.session.request(method, url, data=data, headers=headers)
2×
48
        r.raise_for_status()
2×
49
        return r.json()
2×
50

51
    def get_optout(self, address_type, address):
2×
52
        """
53
        Retrieve an opt out record.
54

55
        :param str address_type:
56
            Type of address, e.g. `msisdn`.
57
        :param str address:
58
            The address to retrieve an opt out for, e.g. `+271235678`.
59

60
        :return:
61
            The opt out record (a dict).
62

63
        Example::
64

65
            >>> client.get_optout('msisdn', '+12345')
66
            {
67
                u'created_at': u'2015-11-10 20:33:03.742409',
68
                u'message': None,
69
                u'user_account': u'fxxxeee',
70
            }
71
        """
72
        uri = "optouts/%s/%s" % (address_type, address)
2×
73
        result = self._api_request("GET", uri)
2×
74
        return result["opt_out"]
2×
75

76
    def set_optout(self, address_type, address):
2×
77
        """
78
        Register an address as having opted out.
79

80
        :param str address_type:
81
            Type of address, e.g. `msisdn`.
82
        :param str address:
83
            The address to store an opt out for, e.g. `+271235678`.
84

85
        :return:
86
            The created opt out record (a dict).
87

88
        Example::
89

90
            >>> client.set_optout('msisdn', '+12345')
91
            {
92
                u'created_at': u'2015-11-10 20:33:03.742409',
93
                u'message': None,
94
                u'user_account': u'fxxxeee',
95
            }
96
        """
97
        uri = "optouts/%s/%s" % (address_type, address)
2×
98
        result = self._api_request("PUT", uri)
2×
99
        return result["opt_out"]
2×
100

101
    def delete_optout(self, address_type, address):
2×
102
        """
103
        Remove an out opt record.
104

105
        :param str address_type:
106
            Type of address, e.g. `msisdn`.
107
        :param str address:
108
            The address to remove the opt out record for, e.g. `+271235678`.
109

110
        :return:
111
            The deleted opt out record (a dict).
112

113
        Example::
114

115
            >>> client.delete_optout('msisdn', '+12345')
116
            {
117
                u'created_at': u'2015-11-10 20:33:03.742409',
118
                u'message': None,
119
                u'user_account': u'fxxxeee',
120
            }
121
        """
122
        uri = "optouts/%s/%s" % (address_type, address)
2×
123
        result = self._api_request("DELETE", uri)
2×
124
        return result["opt_out"]
2×
125

126
    def count(self):
2×
127
        """
128
        Return a count of the total number of opt out records.
129

130
        :return:
131
            The total number of opt outs (an integer).
132

133
        Example::
134

135
            >>> client.count()
136
            215
137
        """
138
        uri = "optouts/count"
2×
139
        result = self._api_request("GET", uri)
2×
140
        return result["opt_out_count"]
2×
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2023 Coveralls, Inc