def purchase(self, status_callback_url=None, **kwargs): """ Attempt to purchase the specified number. The only required parameters are **either** phone_number or area_code :returns: Returns a :class:`PhoneNumber` instance on success, :data:`False` on failure :raises: A :exc:`TypeError` if neither phone_number or area_code is specified. """ kwargs["StatusCallback"] = kwargs.get("status_callback", status_callback_url) if 'phone_number' not in kwargs and 'area_code' not in kwargs: raise TypeError("phone_number or area_code is required") number_type = kwargs.pop('type', False) uri = self.uri if number_type: uri = "%s/%s" % (self.uri, TYPES[number_type]) params = transform_params(kwargs) resp, instance = self.request('POST', uri, data=params) return self.load_instance(instance)
def iter(self, **kwargs): """ Return all instance resources using an iterator This will fetch a page of resources from the API and yield them in turn. When the page is exhausted, this will make a request to the API to retrieve the next page. Hence you may notice a pattern - the library will loop through 50 objects very quickly, but there will be a delay retrieving the 51st as the library must make another request to the API for resources. Example usage: .. code-block:: python for message in client.messages: print message.sid """ params = transform_params(kwargs) while True: resp, page = self.request("GET", self.uri, params=params) if self.key not in page: raise StopIteration() for ir in page[self.key]: yield self.load_instance(ir) if not page.get('next_page_uri', ''): raise StopIteration() o = urlparse(page['next_page_uri']) params.update(parse_qs(o.query))
def update(self, **kwargs): """ Update your Twilio Sandbox """ resp, entry = self.request("POST", self.uri, body=transform_params(kwargs)) return self.create_instance(entry)
def update_instance(self, sid, body): """ Update an InstanceResource via a POST sid: string -- String identifier for the list resource body: dictionary -- Dict of items to POST """ uri = "%s/%s" % (self.uri, sid) resp, entry = self.request("POST", uri, data=transform_params(body)) return self.load_instance(entry)
def create_instance(self, body): """ Create an InstanceResource via a POST to the List Resource :param dict body: Dictionary of POST data """ resp, instance = self.request("POST", self.uri, data=transform_params(body)) if resp.status_code not in (200, 201): raise TwilioRestException(resp.status_code, self.uri, "Resource not created") return self.load_instance(instance)
def create_instance(self, body): """ Create an InstanceResource via a POST to the List Resource :param dict body: Dictionary of POST data """ resp, instance = self.request("POST", self.uri, data=transform_params(body)) if resp.status_code != 201: raise TwilioRestException(resp.status, self.uri, "Resource not created") return self.load_instance(instance)
def list(self, type="local", country="US", region=None, postal_code=None, lata=None, rate_center=None, **kwargs): """ Search for phone numbers """ kwargs["in_region"] = kwargs.get("in_region", region) kwargs["in_postal_code"] = kwargs.get("in_postal_code", postal_code) kwargs["in_lata"] = kwargs.get("in_lata", lata) kwargs["in_rate_center"] = kwargs.get("in_rate_center", rate_center) params = transform_params(kwargs) uri = "%s/%s/%s" % (self.uri, country, self.types[type]) resp, page = self.request("GET", uri, params=params) return [self.load_instance(i) for i in page[self.key]]
def list(self, type=None, **kwargs): """ :param phone_number: Show phone numbers that match this pattern. :param friendly_name: Show phone numbers with this friendly name :param type: Filter numbers by type. Available types are 'local', 'mobile', or 'tollfree' You can specify partial numbers and use '*' as a wildcard. """ uri = self.uri if type: uri = "%s/%s" % (self.uri, TYPES[type]) params = transform_params(kwargs) resp, page = self.request("GET", uri, params=params) return [self.load_instance(i) for i in page[self.key]]
def iter(self, **kwargs): """ Return all instance resources using an iterator """ params = transform_params(kwargs) while True: resp, page = self.request("GET", self.uri, params=params) if self.key not in page: raise StopIteration() for ir in page[self.key]: yield self.load_instance(ir) if not page.get('next_page_uri', ''): raise StopIteration() o = urlparse(page['next_page_uri']) params.update(parse_qs(o.query))
def get_instances(self, params): """ Query the list resource for a list of InstanceResources. Raises a :exc:`~twilio.TwilioRestException` if requesting a page of results that does not exist. :param dict params: List of URL parameters to be included in request :param int page: The page of results to retrieve (most recent at 0) :param int page_size: The number of results to be returned. :returns: -- the list of resources """ params = transform_params(params) resp, page = self.request("GET", self.uri, params=params) if self.key not in page: raise TwilioException("Key %s not present in response" % self.key) return [self.load_instance(ir) for ir in page[self.key]]
def get_instances(self, params): """ Query the list resource for a list of InstanceResources. Raises a TwilioRestException if requesting a page of results that does not exist. :param dict params: List of URL parameters to be included in request :param int page: The page of results to retrieve (most recent at 0) :param int page_size: The number of results to be returned. :returns: -- the list of resources """ params = transform_params(params) resp, page = self.request("GET", self.uri, params=params) if self.key not in page: raise TwilioException("Key %s not present in response" % self.key) return [self.load_instance(ir) for ir in page[self.key]]