示例#1
0
    def __init__(self, **opts):
        """
        Construct SendGrid v3 API object.

        :params host: Base URL for the API call
        :type host: string
        """
        self.path = opts.get(
            'path', os.path.abspath(os.path.dirname(__file__)))
        self._apikey = opts.get('apikey', os.environ.get('SENDGRID_API_KEY'))
        # Support v2 api_key naming
        self._apikey = opts.get('api_key', self._apikey)
        self._api_key = self._apikey
        self.useragent = 'sendgrid/{0};python'.format(__version__)
        self.host = opts.get('host', 'https://api.sendgrid.com')
        self.version = __version__

        headers = {
            "Authorization": 'Bearer {0}'.format(self._apikey),
            "User-agent": self.useragent,
            "Accept": 'application/json'
        }

        self.client = python_http_client.Client(host=self.host,
                                                request_headers=headers,
                                                version=3)
示例#2
0
    def __init__(self,
                 api_key=None,
                 host='https://api.sendgrid.com',
                 impersonate_subuser=None):
        """
        Construct the Twilio SendGrid v3 API object.
        Note that the underlying client is being set up during initialization,
        therefore changing attributes in runtime will not affect HTTP client
        behaviour.

        :param api_key: Twilio SendGrid API key to use. If not provided, key will be
                        read from environment variable "SENDGRID_API_KEY"
        :type api_key: string
        :param impersonate_subuser: the subuser to impersonate. Will be passed
                                    by "On-Behalf-Of" header by underlying
                                    client. See
                                    https://sendgrid.com/docs/User_Guide/Settings/subusers.html
                                    for more details
        :type impersonate_subuser: string
        :param host: base URL for API calls
        :type host: string
        """
        from . import __version__
        self.api_key = api_key or os.environ.get('SENDGRID_API_KEY')
        self.impersonate_subuser = impersonate_subuser
        self.host = host
        self.version = __version__
        self.useragent = 'sendgrid/{};python'.format(self.version)

        self.client = python_http_client.Client(
            host=self.host, request_headers=self._default_headers, version=3)
示例#3
0
    def __init__(self, **opts):
        """
        Construct SendGrid v3 API object.

        :params host: Base URL for the API call
        :type host: string

        """
        self.path = opts.get('path',
                             os.path.abspath(os.path.dirname(__file__)))
        python_http_client.Config(self.path)
        self.useragent = 'sendgrid/{0};python_v3'.format(__version__)
        self.host = opts.get('host', 'https://api.sendgrid.com')
        self.version = __version__

        if (opts.get('apikey') != None):
            self._apikey = opts.get('apikey',
                                    os.environ.get('SENDGRID_API_KEY'))
            authorization = 'Bearer {0}'.format(self._apikey)
        else:
            login = opts.get('login')
            password = opts.get('password')
            self._auth_token = base64.b64encode(login + ':' + password)
            authorization = 'Basic {0}'.format(self._auth_token)

        headers = {
            "Authorization": authorization,
            "Content-Type": "application/json",
            "User-agent": self.useragent
        }

        self.client = python_http_client.Client(host=self.host,
                                                request_headers=headers,
                                                version=3)
示例#4
0
    def __init__(self, **opts):
        """
        Construct SendGrid v3 API object.
        :params host: Base URL for the API call
        :type host: string
        :params apikey: SendGrid API key to use.  Defaults to environment var.
        :type apikey: string
        """
        self.path = opts.get(
            'path', os.path.abspath(os.path.dirname(__file__)))
        self._apikey = opts.get('apikey', os.environ.get('SENDGRID_API_KEY'))
        # Support v2 api_key naming
        self._apikey = opts.get('api_key', self._apikey)
        self._api_key = self._apikey
        # Support impersonation of subusers
        self._impersonate_subuser = opts.get('impersonate_subuser', None)
        self.useragent = 'sendgrid/{0};python'.format(__version__)
        self.host = opts.get('host', 'https://api.sendgrid.com')
        self.version = __version__

        headers = self._get_default_headers()

        self.client = python_http_client.Client(host=self.host,
                                                request_headers=headers,
                                                version=3)
    def __init__(self, auth, host, impersonate_subuser):
        """
        Construct the Twilio SendGrid v3 API object.
        Note that the underlying client is being set up during initialization,
        therefore changing attributes in runtime will not affect HTTP client
        behaviour.

        :param auth: the authorization header
        :type auth: string
        :param impersonate_subuser: the subuser to impersonate. Will be passed
                                    by "On-Behalf-Of" header by underlying
                                    client. See
                                    https://sendgrid.com/docs/User_Guide/Settings/subusers.html
                                    for more details
        :type impersonate_subuser: string
        :param host: base URL for API calls
        :type host: string
        """
        from . import __version__
        self.auth = auth
        self.host = host
        self.impersonate_subuser = impersonate_subuser
        self.version = __version__
        self.useragent = 'sendgrid/{};python'.format(self.version)

        self.client = python_http_client.Client(
            host=self.host,
            request_headers=self._default_headers,
            version=3)
示例#6
0
    def client(self):
        if not self._client:
            self._client = python_http_client.Client(
                host=SANDBOX_HOST if self._use_sandbox else PRODUCTION_HOST)

            self._client.request_headers = self._auth.request_authorization_header(
                self._client)

        return self._client
示例#7
0
def attrs_for_year(year):
    records = {}
    for m in range(1, 13):
        records[m] = {}
        for d in range(1, 32):
            records[m][d] = {}
    for attr in attributes:
        path = gen_path(year, attr)
        url = host + path
        client = python_http_client.Client(host=url,
                                           request_headers=global_headers)
        response = client.get()
        if response.status_code != 200:
            raise Exception('Got a nasty response')
        json_response = json.loads(response.body)
        for result in json_response['results']:
            date = parse(result['date'])
            records[date.month][date.day][attr] = result['value']
    return records
示例#8
0
    def __init__(
            self,
            apikey=None,
            api_key=None,
            impersonate_subuser=None,
            host='https://api.sendgrid.com',
            **opts):  # TODO: remove **opts for 6.x release
        """
        Construct SendGrid v3 API object.
        Note that underlying client being set up during initialization, therefore changing
            attributes in runtime will not affect HTTP client behaviour.
        :param apikey: SendGrid API key to use. If not provided, key will be read from
            environment variable "SENDGRID_API_KEY"
        :type apikey: basestring
        :param api_key: SendGrid API key to use. Provides backward compatibility
            .. deprecated:: 5.3
                Use apikey instead
        :type api_key: basestring
        :param impersonate_subuser: the subuser to impersonate. Will be passed by
            "On-Behalf-Of" header by underlying client.
            See https://sendgrid.com/docs/User_Guide/Settings/subusers.html for more details
        :type impersonate_subuser: basestring
        :param host: base URL for API calls
        :type host: basestring
        :param opts: dispatcher for deprecated arguments. Added for backward-compatibility
            with `path` parameter. Should be removed during 6.x release
        """
        if opts:
            warnings.warn(
                'Unsupported argument(s) provided: {}'.format(list(opts.keys())),
                DeprecationWarning)
        self.apikey = apikey or api_key or os.environ.get('SENDGRID_API_KEY')
        self.impersonate_subuser = impersonate_subuser
        self.host = host
        self.useragent = 'sendgrid/{0};python'.format(__version__)
        self.version = __version__

        self.client = python_http_client.Client(host=self.host,
                                                request_headers=self._default_headers,
                                                version=3)
示例#9
0
import os
import json
import python_http_client

host = "https://api.sendgrid.com"
api_key = os.environ.get('SENDGRID_API_KEY')
request_headers = {"Authorization": 'Bearer {0}'.format(api_key)}
version = 3  # we could also use client.version(3)
client = python_http_client.Client(host=host,
                                   request_headers=request_headers,
                                   version=version)

# GET collection
response = client.api_keys.get()
print(response.status_code)
print(response.headers)
print(response.body)

# POST
data = {
    "name": "My API Key",
    "scopes": ["mail.send", "alerts.create", "alerts.read"]
}
response = client.api_keys.post(request_body=data)
print(response.status_code)
print(response.headers)
print(response.body)
json_response = json.loads(response.body)
api_key_id = json_response['api_key_id']

# GET single
示例#10
0
 def get_client(self):
     return python_http_client.Client(host=self.host,
                                      request_headers=self._default_headers,
                                      version=3)