File: base.py

package info (click to toggle)
python-sparkpost 1.3.10-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 512 kB
  • sloc: python: 2,528; makefile: 31; sh: 10
file content (57 lines) | stat: -rw-r--r-- 1,555 bytes parent folder | download | duplicates (5)
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
import sparkpost

from .exceptions import SparkPostAPIException


class RequestsTransport(object):
    def __init__(self):
        import requests
        self.sess = requests.Session()

    def request(self, method, uri, headers, **kwargs):
        response = self.sess.request(method, uri, headers=headers, **kwargs)
        if response.status_code == 204:
            return True
        if not response.ok:
            raise SparkPostAPIException(response)
        if 'results' in response.json():
            return response.json()['results']
        return response.json()


class Resource(object):
    key = ""

    def __init__(self, base_uri, api_key, transport_class=RequestsTransport):
        self.base_uri = base_uri
        self.api_key = api_key
        self.transport = transport_class()

    @property
    def uri(self):
        return "%s/%s" % (self.base_uri, self.key)

    def request(self, method, uri, **kwargs):
        headers = {
            'User-Agent': 'python-sparkpost/' + sparkpost.__version__,
            'Content-Type': 'application/json',
            'Authorization': self.api_key
        }
        response = self.transport.request(method, uri, headers=headers,
                                          **kwargs)
        return response

    def get(self):
        raise NotImplementedError

    def list(self):
        raise NotImplementedError

    def create(self):
        raise NotImplementedError

    def update(self):
        raise NotImplementedError

    def delete(self):
        raise NotImplementedError