Skip to content

Commit bf52d65

Browse files
Merge pull request #148 from sendgrid/email_activity
Stats GET endpoint
2 parents 96473dd + 5131b1a commit bf52d65

File tree

5 files changed

+114
-4
lines changed

5 files changed

+114
-4
lines changed

README.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,6 @@ Update the name of an existing API Key
276276

277277
Unsubscribe Manager gives your recipients more control over the types of emails they want to receive by letting them opt out of messages from a certain type of email.
278278

279-
More information_.
280-
281-
.. _information: https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/index.html
282-
283279
Unsubscribe Groups
284280
~~~~~~~~~~~~~~~~~~~
285281

@@ -361,6 +357,17 @@ Delete an email from the global suppression list.
361357
email = 'example@example.com'
362358
status, msg = client.asm_global_suppressions.delete(email)
363359
360+
`Global Stats`_
361+
~~~~~~~~~~~~~~~~~~~~~~~
362+
363+
Global Stats provide all of your user’s email statistics for a given date range.
364+
365+
.. code:: python
366+
start_date = '2015-10-01' # required
367+
end_date = None # optional
368+
aggregated_by = 'week' # optional, must be day, week or month
369+
status, msg = client.stats.get(start_date, end_date, aggregated_by)
370+
364371
SendGrid's `X-SMTPAPI`_
365372
-----------------------
366373

@@ -592,3 +599,5 @@ Deploying
592599
.. _`Web API v3 endpoints`: https://sendgrid.com/docs/API_Reference/Web_API_v3/index.html
593600
.. _TOX: https://testrun.org/tox/latest/
594601
.. _`few of the v3`: APIKeysAnchor_
602+
.. _`Suppression Management`: https://sendgrid.com/docs/API_Reference/Web_API_v3/Suppression_Management/index.html
603+
.. _`Global Stats`: https://sendgrid.com/docs/API_Reference/Web_API_v3/Stats/global.html

example_v3_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010

1111
client = sendgrid.SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
1212

13+
start_date = '2015-10-01'
14+
end_date = None
15+
aggregated_by = 'week' # must be day, week or month
16+
status, msg = client.stats.get(
17+
start_date=start_date,
18+
end_date=end_date,
19+
aggregated_by=aggregated_by)
20+
print status
21+
print msg
22+
1323
"""
1424
email = 'example@example.com'
1525
status, msg = client.asm_global_suppressions.delete(email)

sendgrid/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from .resources.asm_suppressions import ASMSuppressions
1717
from .resources.asm_global_suppressions import ASMGlobalSuppressions
1818
from .resources.suppressions import Suppressions
19+
from .resources.stats import Stats
1920

2021
class SendGridAPIClient(object):
2122

@@ -40,6 +41,7 @@ def __init__(self, apikey, **opts):
4041
self.asm_suppressions = ASMSuppressions(self)
4142
self.asm_global_suppressions = ASMGlobalSuppressions(self)
4243
self.suppressions = Suppressions(self)
44+
self.stats = Stats(self)
4345

4446
@property
4547
def apikey(self):

sendgrid/resources/stats.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
try:
2+
from urllib.parse import urlencode
3+
except ImportError: # Python 2
4+
from urllib import urlencode
5+
6+
class Stats(object):
7+
"""Global Stats provide all of your user's email statistics for a given date range."""
8+
9+
def __init__(self, client, **opts):
10+
"""
11+
Constructs SendGrid Stats object.
12+
13+
See https://sendgrid.com/docs/API_Reference/Web_API_v3/Stats/global.html
14+
"""
15+
self._name = None
16+
self._base_endpoint = "/v3/stats?"
17+
self._endpoint = "/v3/stats?"
18+
self._client = client
19+
20+
@property
21+
def base_endpoint(self):
22+
return self._base_endpoint
23+
24+
@property
25+
def endpoint(self):
26+
endpoint = self._endpoint
27+
return endpoint
28+
29+
@endpoint.setter
30+
def endpoint(self, value):
31+
self._endpoint = value
32+
33+
@property
34+
def client(self):
35+
return self._client
36+
37+
# Gets email statistics.
38+
def get(self, start_date, end_date=None, aggregated_by=None):
39+
# Required
40+
args = {'start_date': start_date}
41+
# Optional arguements
42+
if end_date:
43+
args['end_date'] = end_date
44+
if aggregated_by:
45+
args['aggregated_by'] = aggregated_by
46+
self._endpoint = self._base_endpoint + urlencode(args)
47+
return self.client.get(self)

test/test_stats.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from .base_test import BaseTest, MockSendGridAPIClientRequest
2+
import os
3+
try:
4+
import unittest2 as unittest
5+
except ImportError:
6+
import unittest
7+
try:
8+
from StringIO import StringIO
9+
except ImportError: # Python 3
10+
from io import StringIO
11+
12+
import sendgrid
13+
from sendgrid.client import SendGridAPIClient
14+
from sendgrid.version import __version__
15+
16+
SG_KEY = os.getenv('SG_KEY') or 'SENDGRID_APIKEY'
17+
18+
class TestStats(unittest.TestCase):
19+
def setUp(self):
20+
SendGridAPIClient = MockSendGridAPIClientRequest
21+
self.client = SendGridAPIClient(SG_KEY)
22+
23+
def test_stats_init(self):
24+
self.stats = self.client.stats
25+
self.assertEqual(self.stats.base_endpoint, "/v3/stats?")
26+
self.assertEqual(self.stats.endpoint, "/v3/stats?")
27+
self.assertEqual(self.stats.client, self.client)
28+
29+
def test_stats_get(self):
30+
status, msg = self.client.stats.get('2015-01-01')
31+
self.assertEqual(status, 200)
32+
status, msg = self.client.stats.get('2015-01-01', '2015-01-02')
33+
self.assertEqual(status, 200)
34+
status, msg = self.client.stats.get('2015-01-01', '2015-01-02', 'day')
35+
self.assertEqual(status, 200)
36+
status, msg = self.client.stats.get('2015-01-01', None, 'week')
37+
self.assertEqual(status, 200)
38+
status, msg = self.client.stats.get('2015-01-01', None, 'month')
39+
self.assertEqual(status, 200)
40+
41+
if __name__ == '__main__':
42+
unittest.main()

0 commit comments

Comments
 (0)