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

praekelt / go-http-api / 189

11 Nov 2015 - 9:09 coverage: 99.392%. First build
189

Pull #20

travis-ci

337cadae5be17c19e2defb615d7de9f8?size=18&default=identiconhodgestar
Return None instead of 404.
Pull Request #20: Add support for optout API

133 of 136 new or added lines in 3 files covered. (97.79%)

817 of 822 relevant lines covered (99.39%)

1.99 hits per line

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

95.45
/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, none_for_statuses=()):
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
        if r.status_code in none_for_statuses:
2×
49
            return None
2×
50
        r.raise_for_status()
2×
51
        return r.json()
2×
52

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

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

62
        :return:
63
            The opt out record (a dict) or `None` if the API returned a 404
64
            HTTP response.
65

66
        Example::
67

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

81
    def set_optout(self, address_type, address):
2×
82
        """
83
        Register an address as having opted out.
84

85
        :param str address_type:
86
            Type of address, e.g. `msisdn`.
87
        :param str address:
88
            The address to store an opt out for, e.g. `+271235678`.
89

90
        :return:
91
            The created opt out record (a dict).
92

93
        Example::
94

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

106
    def delete_optout(self, address_type, address):
2×
107
        """
108
        Remove an out opt record.
109

110
        :param str address_type:
111
            Type of address, e.g. `msisdn`.
112
        :param str address:
113
            The address to remove the opt out record for, e.g. `+271235678`.
114

115
        :return:
116
            The deleted opt out record (a dict) or None if the API returned
117
            an HTTP 404 reponse.
118

119
        Example::
120

121
            >>> client.delete_optout('msisdn', '+12345')
122
            {
123
                u'created_at': u'2015-11-10 20:33:03.742409',
124
                u'message': None,
125
                u'user_account': u'fxxxeee',
126
            }
127
        """
128
        uri = "optouts/%s/%s" % (address_type, address)
2×
129
        result = self._api_request("DELETE", uri, none_for_statuses=(404,))
2×
130
        if result is None:
2×
131
            return None
2×
132
        return result["opt_out"]
2×
133

134
    def count(self):
2×
135
        """
136
        Return a count of the total number of opt out records.
137

138
        :return:
139
            The total number of opt outs (an integer).
140

141
        Example::
142

143
            >>> client.count()
144
            215
145
        """
146
        uri = "optouts/count"
2×
147
        result = self._api_request("GET", uri)
2×
148
        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