1+ from universalclient import Client , jsonFilter
12import json
23from .version import __version__
3- from socket import timeout
4- try :
5- import urllib .request as urllib_request
6- from urllib .parse import urlencode
7- from urllib .error import HTTPError
8- except ImportError : # Python 2
9- import urllib2 as urllib_request
10- from urllib2 import HTTPError
11- from urllib import urlencode
12-
13- from .exceptions import SendGridClientError , SendGridServerError
14- from .resources .api_keys import APIKeys
15- from .resources .asm_groups import ASMGroups
16- from .resources .asm_suppressions import ASMSuppressions
17- from .resources .asm_global_suppressions import ASMGlobalSuppressions
18- from .resources .suppressions import Suppressions
19- from .resources .stats import Stats
20-
214class SendGridAPIClient (object ):
225
236 """SendGrid API."""
247
258 def __init__ (self , apikey , ** opts ):
269 """
27- Construct SendGrid API object.
10+ Construct SendGrid v3 API object.
2811
2912 Args:
3013 apikey: SendGrid API key
31- opts: You can pass in host or proxies
14+ opts: You can pass in a different host
3215 """
3316 self ._apikey = apikey
3417 self .useragent = 'sendgrid/' + __version__ + ';python_v3'
35- self .host = opts .get ('host' , 'https://api.sendgrid.com' )
36- # urllib cannot connect to SSL servers using proxies
37- self .proxies = opts .get ('proxies' , None )
18+ self .host = opts .get ('host' , 'https://api.sendgrid.com/v3/' )
19+ self .version = __version__
3820
39- self .apikeys = APIKeys (self )
40- self .asm_groups = ASMGroups (self )
41- self .asm_suppressions = ASMSuppressions (self )
42- self .asm_global_suppressions = ASMGlobalSuppressions (self )
43- self .suppressions = Suppressions (self )
44- self .stats = Stats (self )
21+ headers = "{\" Authorization\" : \" Bearer " + self ._apikey + "\" , \
22+ \" Content-Type\" : \" application/json\" , \
23+ \" User-agent\" : \" " + self .useragent + "\" }"
24+
25+ self .client = Client (self .host , dataFilter = jsonFilter , headers = json .loads (headers ))
4526
4627 @property
4728 def apikey (self ):
4829 return self ._apikey
4930
5031 @apikey .setter
5132 def apikey (self , value ):
52- self ._apikey = value
53-
54- def _build_request (self , url , json_header = False , method = 'GET' , data = None ):
55- if self .proxies :
56- proxy_support = urllib_request .ProxyHandler (self .proxies )
57- opener = urllib_request .build_opener (proxy_support )
58- urllib_request .install_opener (opener )
59- req = urllib_request .Request (url )
60- req .get_method = lambda : method
61- req .add_header ('User-Agent' , self .useragent )
62- req .add_header ('Authorization' , 'Bearer ' + self .apikey )
63- if json_header :
64- req .add_header ('Content-Type' , 'application/json' )
65- try :
66- if data :
67- response = urllib_request .urlopen (req , json .dumps (data ))
68- else :
69- response = urllib_request .urlopen (req , timeout = 10 )
70- except HTTPError as e :
71- if 400 <= e .code < 500 :
72- raise SendGridClientError (e .code , e .read ())
73- elif 500 <= e .code < 600 :
74- raise SendGridServerError (e .code , e .read ())
75- else :
76- assert False
77- except timeout as e :
78- raise SendGridClientError (408 , 'Request timeout' )
79- body = response .read ()
80- return response .getcode (), body
81-
82- def get (self , api ):
83- url = self .host + api .endpoint
84- response , body = self ._build_request (url , False , 'GET' )
85- return response , body
86-
87- def post (self , api , data ):
88- url = self .host + api .endpoint
89- response , body = self ._build_request (url , True , 'POST' , data )
90- return response , body
91-
92- def delete (self , api ):
93- url = self .host + api .endpoint
94- response , body = self ._build_request (url , False , 'DELETE' )
95- return response , body
96-
97- def patch (self , api , data ):
98- url = self .host + api .endpoint
99- response , body = self ._build_request (url , True , 'PATCH' , data )
100- return response , body
33+ self ._apikey = value
0 commit comments