def _get_generator(cls, url, to_dict=False, params=None):
        """
        Send the GET request and return a generator.

        :param url: The URL to send the GET request to.
        :type url: str
        :param to_dict: Return a dictionary instead of an instantiated class.
        :type to_dict: bool
        :param params: The GET parameters to send in the request.
        :type params: dict
        :returns: Generator, dict (using json.loads())
        """

        if not params:
            params = dict()

        members = Broker.get(url, params=params).get(t.DATA, [])
        total = len(members)
        if total == t.MIN_TOTAL:
            yield None
        else:
            for member in members:
                if to_dict:
                    yield member
                else:
                    yield Broker.get_new(cls, member)
    def _get_generator(cls, url, to_dict=False, params=None):
        """
        Send the GET request and return a generator.

        :param url: The URL to send the GET request to.
        :type url: str
        :param to_dict: Return a dictionary instead of an instantiated class.
        :type to_dict: bool
        :param params: The GET parameters to send in the request.
        :type params: dict
        :returns: Generator, dict (using json.loads())
        """

        if not params:
            params = dict()

        members = Broker.get(url, params=params).get(t.DATA, [])
        total = len(members)
        if total == t.MIN_TOTAL:
            yield None
        else:
            for member in members:
                if to_dict:
                    yield member
                else:
                    yield Broker.get_new(cls, member)
Exemplo n.º 3
0
    def _get_generator(cls,
                       url,
                       to_dict=False,
                       params=None,
                       retries=None,
                       headers=None,
                       proxies=None,
                       verify=None):
        """
        Send the GET request and return a generator.

        :param url: The URL to send the GET request to.
        :type url: str
        :param to_dict: Return a dictionary instead of an instantiated class.
        :type to_dict: bool
        :param params: The GET parameters to send in the request.
        :type params: dict
        :param retries: Number of retries to fetch a page before stopping.
        :type retries: int
        :param headers: header info for requests.
        :type headers: dict
        :param proxies: proxy info for requests.
        :type proxies: dict
        :param verify: verify info for requests.
        :type verify: bool, str
        :returns: Generator, dict (using json.loads())
        """

        if not params:
            params = dict()

        members = Broker.get(url,
                             params=params,
                             retries=retries,
                             headers=headers,
                             proxies=proxies,
                             verify=verify).get(t.DATA, [])
        total = len(members)
        if total == t.MIN_TOTAL:
            yield None
        else:
            for member in members:
                if to_dict:
                    yield member
                else:
                    yield Broker.get_new(cls, member)
    def _get_generator(cls,
                       url,
                       to_dict=False,
                       params=None,
                       retries=None,
                       headers=None,
                       proxies=None,
                       verify=None):
        """
        Send the GET request and return a generator.

        :param url: The URL to send the GET request to.
        :type url: str
        :param to_dict: Return a dictionary instead of an instantiated class.
        :type to_dict: bool
        :param params: The GET parameters to send in the request.
        :type params: dict
        :param retries: Number of retries to fetch a page before stopping.
        :type retries: int
        :param headers: header info for requests.
        :type headers: dict
        :param proxies: proxy info for requests.
        :type proxies: dict
        :param verify: verify info for requests.
        :type verify: bool, str
        :returns: Generator, dict (using json.loads())
        """

        if not params:
            params = dict()

        members = Broker.get(url,
                             params=params,
                             retries=retries,
                             headers=headers,
                             proxies=proxies,
                             verify=verify).get(t.DATA, [])
        total = len(members)
        if total == t.MIN_TOTAL:
            yield None
        else:
            for member in members:
                if to_dict:
                    yield member
                else:
                    yield Broker.get_new(cls, member)
Exemplo n.º 5
0
    def details(cls_or_self,
                id=None,
                fields=None,
                connection=None,
                full_response=False,
                dict_generator=False,
                retries=None,
                metadata=False):
        """
        Get object details. Allows you to limit the fields returned in the
        object's details. Also allows you to provide a connection. If a
        connection is provided, the related objects will be returned instead
        of the object itself.

        NOTE: This method can be used on both instantiated and uninstantiated
        classes like so:

            foo = ThreatIndicator(id='1234')
            foo.details()

            foo = ThreatIndicator.details(id='1234')


        BE AWARE: Due to the nature of ThreatExchange allowing you to query for
        an object by ID but not actually telling you the type of object returned
        to you, using an ID for an object of a different type (ex: ID for a
        Malware object using ThreatIndicator class) will result in the wrong
        object populated with only data that is common between the two objects.

        :param id: The ID of the object to get details for if the class is not
                   instantiated.
        :type id: str
        :param fields: The fields to limit the details to.
        :type fields: None, str, list
        :param connection: The connection to find other related objects with.
        :type connection: None, str
        :param full_response: Return the full response instead of the generator.
                              Takes precedence over dict_generator.
        :type full_response: bool
        :param dict_generator: Return a dictionary instead of an instantiated
                               object.
        :type dict_generator: bool
        :param retries: Number of retries to fetch a page before stopping.
        :type retries: int
        :param metadata: Get extra metadata in the response.
        :type metadata: bool
        :returns: Generator, dict, class
        """

        if isinstance(cls_or_self, type):
            url = t.URL + t.VERSION + id + '/'
        else:
            url = cls_or_self._DETAILS
        if connection:
            url = url + connection + '/'
        params = Broker.build_get_parameters()
        if isinstance(fields, basestring):
            fields = fields.split(',')
        if fields is not None and not isinstance(fields, list):
            raise pytxValueError('fields must be a list')
        if fields is not None:
            params[t.FIELDS] = ','.join(f.strip() for f in fields)
        if metadata:
            params[t.METADATA] = 1
        if full_response:
            return Broker.get(url, params=params, retries=retries)
        else:
            if connection:
                # Avoid circular imports
                from malware import Malware
                from malware_family import MalwareFamily
                from threat_indicator import ThreatIndicator
                from threat_descriptor import ThreatDescriptor
                conns = {
                    conn.DESCRIPTORS: ThreatDescriptor,
                    conn.DROPPED: Malware,
                    conn.DROPPED_BY: Malware,
                    conn.FAMILIES: MalwareFamily,
                    conn.MALWARE_ANALYSES: Malware,
                    conn.RELATED: ThreatIndicator,
                    conn.THREAT_INDICATORS: ThreatIndicator,
                    conn.VARIANTS: Malware,
                }
                klass = conns.get(connection, None)
                return Broker.get_generator(klass,
                                            url,
                                            to_dict=dict_generator,
                                            params=params,
                                            retries=retries)
            else:
                if isinstance(cls_or_self, type):
                    return Broker.get_new(
                        cls_or_self,
                        Broker.get(url, params=params, retries=retries))
                else:
                    cls_or_self.populate(
                        Broker.get(url, params=params, retries=retries))
                    cls_or_self._changed = []
Exemplo n.º 6
0
    def details(cls_or_self,
                id=None,
                fields=None,
                connection=None,
                full_response=False,
                dict_generator=False,
                retries=None,
                headers=None,
                proxies=None,
                verify=None,
                metadata=False):
        """
        Get object details. Allows you to limit the fields returned in the
        object's details. Also allows you to provide a connection. If a
        connection is provided, the related objects will be returned instead
        of the object itself.

        NOTE: This method can be used on both instantiated and uninstantiated
        classes like so:

            foo = ThreatIndicator(id='1234')
            foo.details()

            foo = ThreatIndicator.details(id='1234')


        BE AWARE: Due to the nature of ThreatExchange allowing you to query for
        an object by ID but not actually telling you the type of object returned
        to you, using an ID for an object of a different type (ex: ID for a
        Malware object using ThreatIndicator class) will result in the wrong
        object populated with only data that is common between the two objects.

        :param id: The ID of the object to get details for if the class is not
                   instantiated.
        :type id: str
        :param fields: The fields to limit the details to.
        :type fields: None, str, list
        :param connection: The connection to find other related objects with.
        :type connection: None, str
        :param full_response: Return the full response instead of the generator.
                              Takes precedence over dict_generator.
        :type full_response: bool
        :param dict_generator: Return a dictionary instead of an instantiated
                               object.
        :type dict_generator: bool
        :param retries: Number of retries to fetch a page before stopping.
        :type retries: int
        :param headers: header info for requests.
        :type headers: dict
        :param proxies: proxy info for requests.
        :type proxies: dict
        :param verify: verify info for requests.
        :type verify: bool, str
        :param metadata: Get extra metadata in the response.
        :type metadata: bool
        :returns: Generator, dict, class
        """

        if isinstance(cls_or_self, type):
            url = t.URL + t.VERSION + id + '/'
        else:
            url = cls_or_self._DETAILS
        if connection:
            url = url + connection + '/'
        params = Broker.build_get_parameters()
        if isinstance(fields, basestring):
            fields = fields.split(',')
        if fields is not None and not isinstance(fields, list):
            raise pytxValueError('fields must be a list')
        if fields is not None:
            params[t.FIELDS] = ','.join(f.strip() for f in fields)
        if metadata:
            params[t.METADATA] = 1
        if full_response:
            return Broker.get(url,
                              params=params,
                              retries=retries,
                              headers=headers,
                              proxies=proxies,
                              verify=verify)
        else:
            if connection:
                # Avoid circular imports
                from malware import Malware
                from malware_family import MalwareFamily
                from threat_indicator import ThreatIndicator
                from threat_descriptor import ThreatDescriptor
                conns = {
                    conn.DESCRIPTORS: ThreatDescriptor,
                    conn.DROPPED: Malware,
                    conn.DROPPED_BY: Malware,
                    conn.FAMILIES: MalwareFamily,
                    conn.MALWARE_ANALYSES: Malware,
                    conn.RELATED: ThreatIndicator,
                    conn.THREAT_INDICATORS: ThreatIndicator,
                    conn.VARIANTS: Malware,
                }
                klass = conns.get(connection, None)
                return Broker.get_generator(klass,
                                            url,
                                            to_dict=dict_generator,
                                            params=params,
                                            retries=retries,
                                            headers=headers,
                                            proxies=proxies,
                                            verify=verify)
            else:
                if isinstance(cls_or_self, type):
                    return Broker.get_new(cls_or_self,
                                          Broker.get(url,
                                                     params=params,
                                                     retries=retries,
                                                     headers=headers,
                                                     proxies=proxies,
                                                     verify=verify))
                else:
                    cls_or_self.populate(Broker.get(url,
                                                    params=params,
                                                    retries=retries,
                                                    headers=headers,
                                                    proxies=proxies,
                                                    verify=verify))
                    cls_or_self._changed = []
Exemplo n.º 7
0
    def details(cls_or_self,
                id=None,
                fields=None,
                full_response=False,
                dict_generator=False,
                retries=None,
                headers=None,
                proxies=None,
                verify=None,
                metadata=False):
        """
        Get object details. Allows you to limit the fields returned in the
        object's details.

        NOTE: This method can be used on both instantiated and uninstantiated
        classes like so:

            foo = ThreatIndicator(id='1234')
            foo.details()

            foo = ThreatIndicator.details(id='1234')


        BE AWARE: Due to the nature of ThreatExchange allowing you to query for
        an object by ID but not actually telling you the type of object returned
        to you, using an ID for an object of a different type (ex: ID for a
        Malware object using ThreatIndicator class) will result in the wrong
        object populated with only data that is common between the two objects.

        :param id: The ID of the object to get details for if the class is not
                   instantiated.
        :type id: str
        :param fields: The fields to limit the details to.
        :type fields: None, str, list
        :param full_response: Return the full response instead of the generator.
                              Takes precedence over dict_generator.
        :type full_response: bool
        :param dict_generator: Return a dictionary instead of an instantiated
                               object.
        :type dict_generator: bool
        :param retries: Number of retries to fetch a page before stopping.
        :type retries: int
        :param headers: header info for requests.
        :type headers: dict
        :param proxies: proxy info for requests.
        :type proxies: dict
        :param verify: verify info for requests.
        :type verify: bool, str
        :param metadata: Get extra metadata in the response.
        :type metadata: bool
        :returns: Generator, dict, class
        """

        if isinstance(cls_or_self, type):
            url = t.URL + t.VERSION + id + '/'
        else:
            url = cls_or_self._DETAILS
        params = Broker.build_get_parameters()
        if isinstance(fields, basestring):
            fields = fields.split(',')
        if fields is not None and not isinstance(fields, list):
            raise pytxValueError('fields must be a list')
        if fields is not None:
            params[t.FIELDS] = ','.join(f.strip() for f in fields)
        if metadata:
            params[t.METADATA] = 1
        if full_response:
            return Broker.get(url,
                              params=params,
                              retries=retries,
                              headers=headers,
                              proxies=proxies,
                              verify=verify)
        else:
            if isinstance(cls_or_self, type):
                return Broker.get_new(cls_or_self,
                                      Broker.get(url,
                                                 params=params,
                                                 retries=retries,
                                                 headers=headers,
                                                 proxies=proxies,
                                                 verify=verify))
            else:
                cls_or_self.populate(Broker.get(url,
                                                params=params,
                                                retries=retries,
                                                headers=headers,
                                                proxies=proxies,
                                                verify=verify))
                cls_or_self._changed = []
Exemplo n.º 8
0
    def details(cls_or_self, id=None, fields=None, connection=None,
                full_response=False, dict_generator=False, metadata=False):
        """
        Get object details. Allows you to limit the fields returned in the
        object's details. Also allows you to provide a connection. If a
        connection is provided, the related objects will be returned instead
        of the object itself.

        NOTE: This method can be used on both instantiated and uninstantiated
        classes like so:

            foo = ThreatIndicator(id='1234')
            foo.details()

            foo = ThreatIndicator.details(id='1234')


        BE AWARE: Due to the nature of ThreatExchange allowing you to query for
        an object by ID but not actually telling you the type of object returned
        to you, using an ID for an object of a different type (ex: ID for a
        Malware object using ThreatIndicator class) will result in the wrong
        object populated with only data that is common between the two objects.

        :param id: The ID of the object to get details for if the class is not
                   instantiated.
        :type id: str
        :param fields: The fields to limit the details to.
        :type fields: None, str, list
        :param connection: The connection to find other related objects with.
        :type connection: None, str
        :param full_response: Return the full response instead of the generator.
                              Takes precedence over dict_generator.
        :type full_response: bool
        :param dict_generator: Return a dictionary instead of an instantiated
                               object.
        :type dict_generator: bool
        :param metadata: Get extra metadata in the response.
        :type metadata: bool
        :returns: Generator, dict, class
        """

        if isinstance(cls_or_self, type):
            url = t.URL + t.VERSION + id + '/'
        else:
            url = cls_or_self._DETAILS
        if connection:
            url = url + connection + '/'
        params = Broker.build_get_parameters()
        if isinstance(fields, basestring):
            fields = fields.split(',')
        if fields is not None and not isinstance(fields, list):
            raise pytxValueError('fields must be a list')
        if fields is not None:
            params[t.FIELDS] = ','.join(f.strip() for f in fields)
        if metadata:
            params[t.METADATA] = 1
        if full_response:
            return Broker.get(url, params=params)
        else:
            if connection:
                return Broker.get_generator(cls_or_self,
                                            url,
                                            t.NO_TOTAL,
                                            to_dict=dict_generator,
                                            params=params)
            else:
                if isinstance(cls_or_self, type):
                    return Broker.get_new(cls_or_self,
                                          Broker.get(url,
                                                     params=params))
                else:
                    cls_or_self.populate(Broker.get(url, params=params))
Exemplo n.º 9
0
    def details(cls_or_self,
                id=None,
                fields=None,
                connection=None,
                full_response=False,
                dict_generator=False,
                metadata=False):
        """
        Get object details. Allows you to limit the fields returned in the
        object's details. Also allows you to provide a connection. If a
        connection is provided, the related objects will be returned instead
        of the object itself.

        NOTE: This method can be used on both instantiated and uninstantiated
        classes like so:

            foo = ThreatIndicator(id='1234')
            foo.details()

            foo = ThreatIndicator.details(id='1234')


        BE AWARE: Due to the nature of ThreatExchange allowing you to query for
        an object by ID but not actually telling you the type of object returned
        to you, using an ID for an object of a different type (ex: ID for a
        Malware object using ThreatIndicator class) will result in the wrong
        object populated with only data that is common between the two objects.

        :param id: The ID of the object to get details for if the class is not
                   instantiated.
        :type id: str
        :param fields: The fields to limit the details to.
        :type fields: None, str, list
        :param connection: The connection to find other related objects with.
        :type connection: None, str
        :param full_response: Return the full response instead of the generator.
                              Takes precedence over dict_generator.
        :type full_response: bool
        :param dict_generator: Return a dictionary instead of an instantiated
                               object.
        :type dict_generator: bool
        :param metadata: Get extra metadata in the response.
        :type metadata: bool
        :returns: Generator, dict, class
        """

        if isinstance(cls_or_self, type):
            url = t.URL + t.VERSION + id + '/'
        else:
            url = cls_or_self._DETAILS
        if connection:
            url = url + connection + '/'
        params = Broker.build_get_parameters()
        if isinstance(fields, basestring):
            fields = fields.split(',')
        if fields is not None and not isinstance(fields, list):
            raise pytxValueError('fields must be a list')
        if fields is not None:
            params[t.FIELDS] = ','.join(f.strip() for f in fields)
        if metadata:
            params[t.METADATA] = 1
        if full_response:
            return Broker.get(url, params=params)
        else:
            if connection:
                return Broker.get_generator(cls_or_self,
                                            url,
                                            t.NO_TOTAL,
                                            to_dict=dict_generator,
                                            params=params)
            else:
                if isinstance(cls_or_self, type):
                    return Broker.get_new(cls_or_self,
                                          Broker.get(url, params=params))
                else:
                    cls_or_self.populate(Broker.get(url, params=params))