Skip to content

Commit

Permalink
NG Generation 1
Browse files Browse the repository at this point in the history
  • Loading branch information
matt committed Sep 18, 2015
1 parent 1a730d4 commit dc0df9f
Show file tree
Hide file tree
Showing 147 changed files with 13,975 additions and 14,801 deletions.
18 changes: 18 additions & 0 deletions twilio/http/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os


def get_cert_file():
""" Get the cert file location or bail """
# XXX - this currently fails test coverage because we don't actually go
# over the network anywhere. Might be good to have a test that stands up a
# local server and authenticates against it.
try:
# Apparently __file__ is not available in all places so wrapping this
# in a try/catch
current_path = os.path.realpath(__file__)
ca_cert_path = os.path.join(current_path, "..", "conf", "cacert.pem")
return os.path.abspath(ca_cert_path)
except Exception:
# None means use the default system file
return None

81 changes: 81 additions & 0 deletions twilio/http/httplib2_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from urllib import urlencode
from urlparse import urlparse
import httplib2
from six import integer_types, string_types, iteritems
from six import binary_type
from twilio.http import get_cert_file
from twilio.http.response import Response


class Httplib2Client(object):

def __init__(self, proxy_info=httplib2.proxy_info_from_environment):
self.proxy_info = proxy_info

def request(self,
method,
url,
params=None,
data=None,
headers=None,
auth=None,
timeout=None,
allow_redirects=False):
"""Sends an HTTP request
:param str method: The HTTP method to use
:param str url: The URL to request
:param dict params: Query parameters to append to the URL
:param dict data: Parameters to go in the body of the HTTP request
:param dict headers: HTTP Headers to send with the request
:param tuple auth: Basic Auth arguments
:param float timeout: Socket/Read timeout for the request
:param boolean allow_redirects: Whether or not to allow redirects
:return: An http response
:rtype: A :class:`Response <twilio.rest.http.response.Response>` object
See the requests documentation for explanation of all these parameters
"""
http = httplib2.Http(
timeout=timeout,
ca_certs=get_cert_file(),
proxy_info=self.proxy_info,
)
http.follow_redirects = allow_redirects

if auth is not None:
http.add_credentials(auth[0], auth[1])

if data is not None:
udata = {}
for k, v in iteritems(data):
key = k.encode('utf-8')
if isinstance(v, (list, tuple, set)):
udata[key] = [self.encode_atom(x) for x in v]
elif isinstance(v, (integer_types, binary_type, string_types)):
udata[key] = self.encode_atom(v)
else:
raise ValueError('data should be an integer, '
'binary, or string, or sequence ')
data = urlencode(udata, doseq=True)

if params is not None:
enc_params = urlencode(params, doseq=True)
if urlparse(url).query:
url = '%s&%s' % (url, enc_params)
else:
url = '%s?%s' % (url, enc_params)

resp, content = http.request(url, method, headers=headers, body=data)

return Response(int(resp.status), content.decode('utf-8'))

@classmethod
def encode_atom(cls, atom):
if isinstance(atom, (integer_types, binary_type)):
return atom
elif isinstance(atom, string_types):
return atom.encode('utf-8')
else:
raise ValueError('list elements should be an integer, binary, or string')
9 changes: 9 additions & 0 deletions twilio/http/response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Response(object):
"""
"""
def __init__(self, status_code, content):
self.content = content
self.cached = False
self.status_code = status_code
self.ok = self.status_code < 400
149 changes: 141 additions & 8 deletions twilio/rest/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,141 @@
from .base import set_twilio_proxy
from .client import TwilioRestClient
from .lookups import TwilioLookupsClient
from .pricing import TwilioPricingClient
from .taskrouter import TwilioTaskRouterClient

_hush_pyflakes = [set_twilio_proxy, TwilioRestClient, TwilioLookupsClient,
TwilioPricingClient, TwilioTaskRouterClient]
# coding=utf-8
"""
This code was generated by
\ / _ _ _| _ _
| (_)\/(_)(_|\/| |(/_ v1.0.0
/ /
"""

import os
from twilio.exceptions import TwilioException
from twilio.http.httplib2_client import Httplib2Client
from twilio.rest.conversations import Conversations
from twilio.rest.lookups import Lookups
from twilio.rest.monitor import Monitor
from twilio.rest.pricing import Pricing
from twilio.rest.taskrouter import Taskrouter
from twilio.rest.trunking import Trunking
from twilio.rest.v2010 import V2010


class Twilio(object):
""" A client for accessing the Twilio API. """

def __init__(self, account_sid=None, auth_token=None, http_client=None,
environment=None):
environment = environment or os.environ

account_sid = account_sid or environment.get('TWILIO_ACCOUNT_SID')
auth_token = auth_token or environment.get('TWILIO_AUTH_TOKEN')

if not account_sid or not auth_token:
raise TwilioException("Credentials are required to create a TwilioClient")

self.auth = (account_sid, auth_token)
self.http_client = http_client or Httplib2Client()

self._conversations = None
self._lookups = None
self._monitor = None
self._pricing = None
self._taskrouter = None
self._trunking = None
self._v2010 = None

def request(self, method, uri, params=None, data=None, headers=None, auth=None,
timeout=None, allow_redirects=False):
auth = auth or self.auth
return self.http_client.request(
method,
uri,
params=params,
data=data,
headers=headers,
auth=auth,
timeout=timeout,
allow_redirects=allow_redirects
)

@property
def conversations(self):
"""
Access the Conversations Twilio Domain
:returns: Conversations Twilio Domain
:rtype: twilio.rest.conversations.Conversations
"""
if self._conversations is None:
self._conversations = Conversations(self)
return self._conversations

@property
def lookups(self):
"""
Access the Lookups Twilio Domain
:returns: Lookups Twilio Domain
:rtype: twilio.rest.lookups.Lookups
"""
if self._lookups is None:
self._lookups = Lookups(self)
return self._lookups

@property
def monitor(self):
"""
Access the Monitor Twilio Domain
:returns: Monitor Twilio Domain
:rtype: twilio.rest.monitor.Monitor
"""
if self._monitor is None:
self._monitor = Monitor(self)
return self._monitor

@property
def pricing(self):
"""
Access the Pricing Twilio Domain
:returns: Pricing Twilio Domain
:rtype: twilio.rest.pricing.Pricing
"""
if self._pricing is None:
self._pricing = Pricing(self)
return self._pricing

@property
def taskrouter(self):
"""
Access the Taskrouter Twilio Domain
:returns: Taskrouter Twilio Domain
:rtype: twilio.rest.taskrouter.Taskrouter
"""
if self._taskrouter is None:
self._taskrouter = Taskrouter(self)
return self._taskrouter

@property
def trunking(self):
"""
Access the Trunking Twilio Domain
:returns: Trunking Twilio Domain
:rtype: twilio.rest.trunking.Trunking
"""
if self._trunking is None:
self._trunking = Trunking(self)
return self._trunking

@property
def v2010(self):
"""
Access the V2010 Twilio Domain
:returns: V2010 Twilio Domain
:rtype: twilio.rest.v2010.V2010
"""
if self._v2010 is None:
self._v2010 = V2010(self)
return self._v2010
Loading

0 comments on commit dc0df9f

Please sign in to comment.