def all(self): """Return a CollectionProxy for the resource.""" collection = utils.resource_class_to_collection_name( self.collection_class) finder_url = "/%s" % (collection) return CollectionProxy( self.client, self.collection_class, collection, finder_url)
def scroll(self, **params): """Find all instances of the resource based on the supplied parameters.""" collection_name = utils.resource_class_to_collection_name( self.collection_class) finder_url = "/{}/scroll".format(collection_name) return ScrollCollectionProxy(self.client, self.collection_class, collection_name, finder_url)
def reply(self, **reply_data): from intercom import Intercom collection = utils.resource_class_to_collection_name(self.__class__) url = "/%s/%s/reply" % (collection, self.id) reply_data['conversation_id'] = self.id response = Intercom.post(url, **reply_data) return self.from_response(response)
def submit_bulk_job(self, create_items=[], delete_items=[], job_id=None): """Submit a Bulk API job.""" from intercom import event from intercom.errors import HttpError from intercom.job import Job if self.collection_class == event.Event and delete_items: raise Exception("Events do not support bulk delete operations.") data_type = utils.resource_class_to_name(self.collection_class) collection_name = utils.resource_class_to_collection_name( self.collection_class) create_items = [ item_for_api('post', data_type, item) for item in create_items ] delete_items = [ item_for_api('delete', data_type, item) for item in delete_items ] bulk_request = {'items': create_items + delete_items} if job_id: bulk_request['job'] = {'id': job_id} response = self.client.post('/bulk/%s' % (collection_name), bulk_request) if not response: raise HttpError('HTTP Error - No response entity returned.') return Job().from_response(response)
def create(cls, **params): from intercom import Intercom collection = utils.resource_class_to_collection_name(cls) response = Intercom.post("/%s/" % (collection), **params) if response: # may be empty if we received a 202 return cls(**response)
def by_tag(self, _id): """Return a CollectionProxy to all the tagged resources.""" collection = utils.resource_class_to_collection_name( self.collection_class) finder_url = "/%s?tag_id=%s" % (collection, _id) return CollectionProxy(self.client, self.collection_class, collection, finder_url)
def users(self, id): """Return a CollectionProxy to all the users for the specified Company.""" collection = utils.resource_class_to_collection_name( self.collection_class) finder_url = "/%s/%s/users" % (collection, id) return CollectionProxy(self.client, self.collection_class, "users", finder_url)
def __init__(self, client, collection_cls, collection, finder_url, finder_params={}): self.client = client # resource name self.resource_name = utils.resource_class_to_collection_name( collection_cls) # resource class self.resource_class = collection_cls # needed to create class instances of the resource self.collection_cls = collection_cls # needed to reference the collection in the response self.collection = collection # the original URL to retrieve the resources self.finder_url = finder_url # the params to filter the resources self.finder_params = finder_params # an iterator over the resources found in the response self.resources = None # a link to the next page of results self.next_page = None
def scroll(self, **params): """Find all instances of the resource based on the supplied parameters.""" collection_name = utils.resource_class_to_collection_name( self.collection_class) finder_url = "/{}/scroll".format(collection_name) return ScrollCollectionProxy( self.client, self.collection_class, collection_name, finder_url)
def create(self, **params): """Create an instance of the resource from the supplied parameters.""" collection = utils.resource_class_to_collection_name( self.collection_class) response = self.client.post("/%s/" % (collection), params) if response: # may be empty if we received a 202 return self.collection_class(**response)
def users(self, id): """Return a CollectionProxy to all the users for the specified Company.""" collection = utils.resource_class_to_collection_name( self.collection_class) finder_url = "/%s/%s/users" % (collection, id) return CollectionProxy( self.client, user.User, "users", finder_url)
def find_all(cls, **params): collection = utils.resource_class_to_collection_name(cls) if 'id' in params and 'type' not in params: finder_url = "/%s/%s" % (collection, params['id']) else: finder_url = "/%s" % (collection) finder_params = params return CollectionProxy(cls, collection, finder_url, finder_params)
def find_all(self, **params): """Find all instances of the resource based on the supplied parameters.""" collection = utils.resource_class_to_collection_name( self.collection_class) if 'id' in params and 'type' not in params: finder_url = "/%s/%s" % (collection, params['id']) else: finder_url = "/%s" % (collection) finder_params = params return self.proxy_class( self.client, self.collection_class, collection, finder_url, finder_params)
def find(cls, **params): from intercom import Intercom collection = utils.resource_class_to_collection_name(cls) if 'id' in params: response = Intercom.get("/%s/%s" % (collection, params['id'])) else: response = Intercom.get("/%s" % (collection), **params) if response is None: raise HttpError('Http Error - No response entity returned') return cls(**response)
def save(self): from intercom import Intercom collection = utils.resource_class_to_collection_name(self.__class__) params = self.attributes if self.id_present and not self.posted_updates: # update response = Intercom.put('/%s/%s' % (collection, self.id), **params) else: # create params.update(self.identity_hash) response = Intercom.post('/%s' % (collection), **params) if response: return self.from_response(response)
def save(self, obj): collection = utils.resource_class_to_collection_name( obj.__class__) params = obj.attributes if self.id_present(obj) and not self.posted_updates(obj): # update response = self.client.put('/%s/%s' % (collection, obj.id), params) else: # create params.update(self.identity_hash(obj)) response = self.client.post('/%s' % (collection), params) if response: return obj.from_response(response)
def find(self, **params): collection = utils.resource_class_to_collection_name( self.collection_class) if 'id' in params: response = self.client.get( "/%s/%s" % (collection, params['id']), {}) else: response = self.client.get("/%s" % (collection), params) if response is None: raise HttpError('Http Error - No response entity returned') return self.collection_class(**response)
def save(self, obj): """Save the instance of the resource.""" collection = utils.resource_class_to_collection_name(obj.__class__) params = obj.attributes if self.id_present(obj) and not self.posted_updates(obj): # update response = self.client.put('/%s/%s' % (collection, obj.id), params) else: # create params.update(self.identity_hash(obj)) response = self.client.post('/%s' % (collection), params) if response: return obj.from_response(response)
def load(self): from intercom import Intercom cls = self.__class__ collection = utils.resource_class_to_collection_name(cls) if hasattr(self, 'id'): response = Intercom.get("/%s/%s" % (collection, self.id)) else: raise Exception( "Cannot load %s as it does not have a valid id." % (cls)) if response is None: raise HttpError('Http Error - No response entity returned') return cls(**response)
def find(self, **params): """Find the instance of the resource based on the supplied parameters.""" collection = utils.resource_class_to_collection_name( self.collection_class) if 'id' in params: response = self.client.get( "/%s/%s" % (collection, params['id']), {}) else: response = self.client.get("/%s" % (collection), params) if response is None: raise HttpError('Http Error - No response entity returned') return self.collection_class(**response)
def load(self): from intercom import Intercom cls = self.__class__ collection = utils.resource_class_to_collection_name(cls) if hasattr(self, 'id'): response = Intercom.get("/%s/%s" % (collection, self.id)) else: raise Exception("Cannot load %s as it does not have a valid id." % (cls)) if response is None: raise HttpError('Http Error - No response entity returned') return cls(**response)
def find(self, **params): """Find the instance of the resource based on the supplied parameters.""" collection = utils.resource_class_to_collection_name( self.collection_class) if 'id' in params: response = self.client.get("/%s/%s" % (collection, params['id']), {}) else: response = self.client.get("/%s" % (collection), params) if response is None: raise HttpError('Http Error - No response entity returned') return self.collection_class(**response)
def load(self, resource): """Load the resource from the latest data in Intercom.""" collection = utils.resource_class_to_collection_name( self.collection_class) if hasattr(resource, 'id'): response = self.client.get("/%s/%s" % (collection, resource.id), {}) # noqa else: raise Exception( "Cannot load %s as it does not have a valid id." % ( self.collection_class)) if response is None: raise HttpError('Http Error - No response entity returned') return resource.from_response(response)
def load(self, resource): """Load the resource from the latest data in Intercom.""" collection = utils.resource_class_to_collection_name( self.collection_class) if hasattr(resource, 'id'): response = self.client.get("/%s/%s" % (collection, resource.id), {}) # noqa else: raise Exception("Cannot load %s as it does not have a valid id." % (self.collection_class)) if response is None: raise HttpError('Http Error - No response entity returned') return resource.from_response(response)
def _tag_collection(cls, collection_name, name, objects, untag=False): from intercom import Intercom collection = utils.resource_class_to_collection_name(cls) object_ids = [] for obj in objects: if not hasattr(obj, 'keys'): obj = {'id': obj} if untag: obj['untag'] = True object_ids.append(obj) params = { 'name': name, collection_name: object_ids, } response = Intercom.post("/%s" % (collection), **params) return cls(**response)
def _tag_collection( cls, collection_name, name, objects, untag=False): from intercom import Intercom collection = utils.resource_class_to_collection_name(cls) object_ids = [] for obj in objects: if not hasattr(obj, 'keys'): obj = {'id': obj} if untag: obj['untag'] = True object_ids.append(obj) params = { 'name': name, collection_name: object_ids, } response = Intercom.post("/%s" % (collection), **params) return cls(**response)
def submit_bulk_job(self, create_items=[], delete_items=[], job_id=None): """Submit a Bulk API job.""" from intercom import event from intercom.errors import HttpError from intercom.job import Job if self.collection_class == event.Event and delete_items: raise Exception("Events do not support bulk delete operations.") data_type = utils.resource_class_to_name(self.collection_class) collection_name = utils.resource_class_to_collection_name(self.collection_class) create_items = [item_for_api('post', data_type, item) for item in create_items] delete_items = [item_for_api('delete', data_type, item) for item in delete_items] bulk_request = { 'items': create_items + delete_items } if job_id: bulk_request['job'] = {'id': job_id} response = self.client.post('/bulk/%s' % (collection_name), bulk_request) if not response: raise HttpError('HTTP Error - No response entity returned.') return Job().from_response(response)
def delete(self, obj): """Delete the specified instance of this resource.""" collection = utils.resource_class_to_collection_name( self.collection_class) self.client.delete("/%s/%s" % (collection, obj.id), {}) return obj
def all(cls): collection = utils.resource_class_to_collection_name(cls) finder_url = "/%s" % (collection) return CollectionProxy(cls, collection, finder_url)
def users(self, id): collection = utils.resource_class_to_collection_name( self.collection_class) finder_url = "/%s/%s/users" % (collection, id) return CollectionProxy( self.client, self.collection_class, "users", finder_url)
def delete(self, obj): collection = utils.resource_class_to_collection_name( self.collection_class) self.client.delete("/%s/%s" % (collection, obj.id), {}) return obj
def create(self, **params): collection = utils.resource_class_to_collection_name( self.collection_class) response = self.client.post("/%s/" % (collection), params) if response: # may be empty if we received a 202 return self.collection_class(**response)
def users(self): collection = utils.resource_class_to_collection_name(self.__class__) finder_url = "/%s/%s/users" % (collection, self.id) return CollectionProxy(User, "users", finder_url)
def delete(self): from intercom import Intercom collection = utils.resource_class_to_collection_name(self.__class__) Intercom.delete("/%s/%s/" % (collection, self.id)) return self
def collection(self): """Return the name of the collection.""" return utils.resource_class_to_collection_name(self.collection_class)