Beispiel #1
0
    def save(self):
        """
        Save this object. If it is a new object, use self._fields to build the
        POST and submit to the appropriate URL. If it is an update to an
        existing object, use self._changed to only submit the modified
        attributes in the POST.

        :returns: dict (using json.loads())
        """

        if self._new:
            params = dict(
                (n, getattr(self, n)) for n in self._fields if n != c.ID)
            if ti.PRIVACY_TYPE not in params:
                raise pytxValueError('Must provide a %s' % ti.PRIVACY_TYPE)
                pass
            else:
                if (params[ti.PRIVACY_TYPE] != pt.VISIBLE
                        and len(params[ti.PRIVACY_MEMBERS].split(',')) < 1):
                    raise pytxValueError('Must provide %s' %
                                         ti.PRIVACY_MEMBERS)
            result = Broker.post(self._URL, params=params)
            if r.ID in result:
                self.set(ti.ID, result[r.ID])
            return result
        else:
            params = dict(
                (n, getattr(self, n)) for n in self._changed if n != c.ID)
            if (ti.PRIVACY_TYPE in params
                    and params[ti.PRIVACY_TYPE] != pt.VISIBLE
                    and len(params[ti.PRIVACY_MEMBERS].split(',')) < 1):
                raise pytxValueError('Must provide %s' % ti.PRIVACY_MEMBERS)
            return Broker.post(self._DETAILS, params=params)
Beispiel #2
0
    def save(self):
        """
        Save this object. If it is a new object, use self._fields to build the
        POST and submit to the appropriate URL. If it is an update to an
        existing object, use self._changed to only submit the modified
        attributes in the POST.

        :returns: dict (using json.loads())
        """

        if self._new:
            params = dict(
                (n, getattr(self, n)) for n in self._fields if n != c.ID
            )
            if ti.PRIVACY_TYPE not in params:
                raise pytxValueError('Must provide a %s' % ti.PRIVACY_TYPE)
                pass
            else:
                if (params[ti.PRIVACY_TYPE] != pt.VISIBLE and
                        len(params[ti.PRIVACY_MEMBERS].split(',')) < 1):
                    raise pytxValueError('Must provide %s' % ti.PRIVACY_MEMBERS)
            return Broker.post(self._URL, params=params)
        else:
            params = dict(
                (n, getattr(self, n)) for n in self._changed if n != c.ID
            )
            if (ti.PRIVACY_TYPE in params and
                    params[ti.PRIVACY_TYPE] != pt.VISIBLE and
                    len(params[ti.PRIVACY_MEMBERS].split(',')) < 1):
                raise pytxValueError('Must provide %s' % ti.PRIVACY_MEMBERS)
            return Broker.post(self._DETAILS, params=params)
Beispiel #3
0
    def save(cls_or_self, params):
        """
        Submit params to the graph to add/update an object. If this is an
        uninstantiated class then we will submit to the object URL (used for
        creating new objects in the graph). If this is an instantiated class
        object then we will determine the Details URL and submit there (used for
        updating an existing object).

        :param params: The parameters to submit.
        :type params: dict
        :returns: dict (using json.loads())
        """

        if isinstance(cls_or_self, type):
            url = t.URL + t.VERSION + id + '/'
        else:
            url = cls_or_self._DETAILS
        if ti.PRIVACY_TYPE not in params:
            raise pytxValueError('Must provide a %s' % ti.PRIVACY_TYPE)
            pass
        else:
            if (params[ti.PRIVACY_TYPE] != pt.VISIBLE and
                    len(params[ti.PRIVACY_MEMBERS].split(',')) < 1):
                raise pytxValueError('Must provide %s' % ti.PRIVACY_MEMBERS)
        return Broker.post(url, params=params)
Beispiel #4
0
    def new(cls, params, retries=None, proxies=None, verify=None):
        """
        Submit params to the graph to add an object. We will submit to the
        object URL used for creating new objects in the graph. When submitting
        new objects you must provide privacy type and privacy members if the
        privacy type is something other than visible.

        :param params: The parameters to submit.
        :type params: dict
        :param retries: Number of retries to submit before stopping.
        :type retries: int
        :param proxies: proxy info for requests.
        :type proxies: dict
        :param verify: verify info for requests.
        :type verify: bool, str
        :returns: dict (using json.loads())
        """

        if cls.__name__ != 'ThreatPrivacyGroup':
            if td.PRIVACY_TYPE not in params:
                raise pytxValueError('Must provide a %s' % td.PRIVACY_TYPE)
                pass
            else:
                if (params[td.PRIVACY_TYPE] != pt.VISIBLE and
                        len(params[td.PRIVACY_MEMBERS].split(',')) < 1):
                    raise pytxValueError('Must provide %s' % td.PRIVACY_MEMBERS)
        return Broker.post(cls._URL, params=params, retries=retries,
                           proxies=proxies, verify=verify)
Beispiel #5
0
    def send(cls_or_self, id_=None, params=None, type_=None):
        """
        Send custom params to the object URL. If `id` is provided it will be
        appended to the URL. If this is an uninstantiated class we will use the
        object type url (ex: /threat_descriptors/). If this is an instantiated
        object we will use the details URL. The type_ should be either GET or
        POST. We will default to GET if this is an uninstantiated class, and
        POST if this is an instantiated class.

        :param id_: ID of a graph object.
        :type id_: str
        :param params: Parameters to submit in the request.
        :type params: dict
        :param type_: GET or POST
        :type type_: str

        :returns: dict (using json.loads())
        """

        if isinstance(cls_or_self, type):
            url = cls_or_self._URL
            if type_ is None:
                type_ = 'GET'
        else:
            url = cls_or_self._DETAILS
            if type_ is None:
                type_ = 'POST'
        if id_ is not None and len(id_) > 0:
            url = url + id_ + '/'
        if params is None:
            params = {}
        if type_ == 'GET':
            return Broker.get(url, params=params)
        else:
            return Broker.post(url, params=params)
    def false_positive(self,
                       object_id,
                       retries=None,
                       headers=None,
                       proxies=None,
                       verify=None):
        """
        Mark an object as a false positive by setting the status to
        UNKNOWN.

        :param object_id: The object-id of the object to mark.
        :type object_id: str
        :param retries: Number of retries to submit 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: dict (using json.loads())
        """

        params = {
            c.STATUS: s.UNKNOWN
        }
        return Broker.post(self._DETAILS,
                           params=params,
                           retries=retries,
                           headers=headers,
                           proxies=proxies,
                           verify=verify)
    def add_connection(self,
                       object_id,
                       retries=None,
                       headers=None,
                       proxies=None,
                       verify=None):
        """
        Use HTTP POST and add a connection between two objects.

        :param object_id: The other object-id in the connection.
        :type object_id: str
        :param retries: Number of retries to submit 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: dict (using json.loads())
        """

        params = {
            t.RELATED_ID: object_id
        }
        return Broker.post(self._RELATED,
                           params=params,
                           retries=retries,
                           headers=headers,
                           proxies=proxies,
                           verify=verify)
    def expire(self,
               timestamp,
               retries=None,
               headers=None,
               proxies=None,
               verify=None):
        """
        Expire by setting the 'expired_on' timestamp.

        :param timestamp: The timestamp to set for an expiration date.
        :type timestamp: str
        :param retries: Number of retries to submit 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: dict (using json.loads())
        """

        Broker.is_timestamp(timestamp)
        params = {
            td.EXPIRED_ON: timestamp
        }
        return Broker.post(self._DETAILS,
                           params=params,
                           retries=retries,
                           headers=headers,
                           proxies=proxies,
                           verify=verify)
    def set_members(self,
                    members=None,
                    retries=None,
                    headers=None,
                    proxies=None,
                    verify=None):
        """
        Set the members of a Threat Privacy Group

        :param members: str or list of member IDs to add as members.
        :type members: str or list
        :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: list
        """

        if members is None:
            raise pytxValueError('Must provide members as a str or list')
        elif isinstance(members, list):
            members = ','.join(members)
        return Broker.post(self._DETAILS,
                           params={'members': members},
                           retries=retries,
                           headers=headers,
                           proxies=proxies,
                           verify=verify)
    def set_members(self,
                    members=None,
                    retries=None,
                    headers=None,
                    proxies=None,
                    verify=None):
        """
        Set the members of a Threat Privacy Group

        :param members: str or list of member IDs to add as members.
        :type members: str or list
        :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: list
        """

        if members is None:
            raise pytxValueError('Must provide members as a str or list')
        elif isinstance(members, list):
            members = ','.join(members)
        return Broker.post(self._DETAILS,
                           params={'members': members},
                           retries=retries,
                           headers=headers,
                           proxies=proxies,
                           verify=verify)
Beispiel #11
0
    def false_positive(self,
                       object_id,
                       retries=None,
                       proxies=None,
                       verify=None):
        """
        Mark an object as a false positive by setting the status to
        UNKNOWN.

        :param object_id: The object-id of the object to mark.
        :type object_id: str
        :param retries: Number of retries to submit before stopping.
        :type retries: int
        :param proxies: proxy info for requests.
        :type proxies: dict
        :param verify: verify info for requests.
        :type verify: bool, str
        :returns: dict (using json.loads())
        """

        params = {c.STATUS: s.UNKNOWN}
        return Broker.post(self._DETAILS,
                           params=params,
                           retries=retries,
                           proxies=proxies,
                           verify=verify)
Beispiel #12
0
    def save(self, params=None, retries=None, proxies=None, verify=None):
        """
        Submit changes to the graph to update an object. We will determine the
        Details URL and submit there (used for updating an existing object). If
        no parameters are provided, we will try to use get_changed() which may
        or may not be accurate (you have been warned!).

        :param params: The parameters to submit.
        :type params: dict
        :param retries: Number of retries to submit before stopping.
        :type retries: int
        :param proxies: proxy info for requests.
        :type proxies: dict
        :param verify: verify info for requests.
        :type verify: bool, str
        :returns: dict (using json.loads())
        """

        if params is None:
            params = self.get_changed()
        return Broker.post(self._DETAILS,
                           params=params,
                           retries=retries,
                           proxies=proxies,
                           verify=verify)
    def save(self,
             params=None,
             retries=None,
             headers=None,
             proxies=None,
             verify=None):
        """
        Submit changes to the graph to update an object. We will determine the
        Details URL and submit there (used for updating an existing object). If
        no parameters are provided, we will try to use get_changed() which may
        or may not be accurate (you have been warned!).

        :param params: The parameters to submit.
        :type params: dict
        :param retries: Number of retries to submit 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: dict (using json.loads())
        """

        if params is None:
            params = self.get_changed()
        return Broker.post(self._DETAILS,
                           params=params,
                           retries=retries,
                           headers=headers,
                           proxies=proxies,
                           verify=verify)
Beispiel #14
0
    def add_connection(self,
                       object_id,
                       retries=None,
                       proxies=None,
                       verify=None):
        """
        Use HTTP POST and add a connection between two objects.

        :param object_id: The other object-id in the connection.
        :type object_id: str
        :param retries: Number of retries to submit before stopping.
        :type retries: int
        :param proxies: proxy info for requests.
        :type proxies: dict
        :param verify: verify info for requests.
        :type verify: bool, str
        :returns: dict (using json.loads())
        """

        params = {t.RELATED_ID: object_id}
        return Broker.post(self._RELATED,
                           params=params,
                           retries=retries,
                           proxies=proxies,
                           verify=verify)
Beispiel #15
0
    def expire(self,
               timestamp,
               retries=None,
               headers=None,
               proxies=None,
               verify=None):
        """
        Expire by setting the 'expired_on' timestamp.

        :param timestamp: The timestamp to set for an expiration date.
        :type timestamp: str
        :param retries: Number of retries to submit 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: dict (using json.loads())
        """

        Broker.is_timestamp(timestamp)
        params = {
            td.EXPIRED_ON: timestamp
        }
        return Broker.post(self._DETAILS,
                           params=params,
                           retries=retries,
                           headers=headers,
                           proxies=proxies,
                           verify=verify)
Beispiel #16
0
    def new(cls, params, retries=None, proxies=None, verify=None):
        """
        Submit params to the graph to add an object. We will submit to the
        object URL used for creating new objects in the graph. When submitting
        new objects you must provide privacy type and privacy members if the
        privacy type is something other than visible.

        :param params: The parameters to submit.
        :type params: dict
        :param retries: Number of retries to submit before stopping.
        :type retries: int
        :param proxies: proxy info for requests.
        :type proxies: dict
        :param verify: verify info for requests.
        :type verify: bool, str
        :returns: dict (using json.loads())
        """

        if cls.__name__ != 'ThreatPrivacyGroup':
            if td.PRIVACY_TYPE not in params:
                raise pytxValueError('Must provide a %s' % td.PRIVACY_TYPE)
                pass
            else:
                if (params[td.PRIVACY_TYPE] != pt.VISIBLE
                        and len(params[td.PRIVACY_MEMBERS].split(',')) < 1):
                    raise pytxValueError('Must provide %s' %
                                         td.PRIVACY_MEMBERS)
        return Broker.post(cls._URL,
                           params=params,
                           retries=retries,
                           proxies=proxies,
                           verify=verify)
Beispiel #17
0
    def send(cls_or_self,
             id_=None,
             params=None,
             type_=None,
             retries=None,
             headers=None,
             proxies=None,
             verify=None):
        """
        Send custom params to the object URL. If `id` is provided it will be
        appended to the URL. If this is an uninstantiated class we will use the
        object type url (ex: /threat_descriptors/). If this is an instantiated
        object we will use the details URL. The type_ should be either GET or
        POST. We will default to GET if this is an uninstantiated class, and
        POST if this is an instantiated class.

        :param id_: ID of a graph object.
        :type id_: str
        :param params: Parameters to submit in the request.
        :type params: dict
        :param type_: GET or POST
        :type type_: str
        :param retries: Number of retries to submit 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: dict (using json.loads())
        """

        if isinstance(cls_or_self, type):
            url = cls_or_self._URL
            if type_ is None:
                type_ = 'GET'
        else:
            url = cls_or_self._DETAILS
            if type_ is None:
                type_ = 'POST'
        if id_ is not None and len(id_) > 0:
            url = url + id_ + '/'
        if params is None:
            params = {}
        if type_ == 'GET':
            return Broker.get(url,
                              params=params,
                              retries=retries,
                              headers=headers,
                              proxies=proxies,
                              verify=verify)
        else:
            return Broker.post(url,
                               params=params,
                               retries=retries,
                               headers=headers,
                               proxies=proxies,
                               verify=verify)
Beispiel #18
0
    def add_connection(self, object_id):
        """
        Use HTTP POST and add a connection between two objects.

        :param object_id: The other object-id in the connection.
        :type object_id: str
        :returns: dict (using json.loads())
        """

        params = {t.RELATED_ID: object_id}
        return Broker.post(self._RELATED, params=params)
Beispiel #19
0
    def add_connection(self, object_id):
        """
        Use HTTP POST and add a connection between two objects.

        :param object_id: The other object-id in the connection.
        :type object_id: str
        :returns: dict (using json.loads())
        """

        params = {
            t.RELATED_ID: object_id
        }
        return Broker.post(self._RELATED, params=params)
Beispiel #20
0
    def expire(self, timestamp):
        """
        Expire by setting the 'expired_on' timestamp.

        :param timestamp: The timestamp to set for an expiration date.
        :type timestamp: str
        """

        Broker.is_timestamp(timestamp)
        params = {
            ti.EXPIRED_ON: timestamp
        }
        return Broker.post(self._DETAILS, params=params)
Beispiel #21
0
    def false_positive(self, object_id):
        """
        Mark an object as a false positive by setting the status to
        UNKNOWN.

        :param object_id: The object-id of the object to mark.
        :type object_id: str
        """

        params = {
            c.STATUS: s.UNKNOWN
        }
        return Broker.post(self._DETAILS, params=params)
Beispiel #22
0
    def add_connection(self, object_id, retries=None):
        """
        Use HTTP POST and add a connection between two objects.

        :param object_id: The other object-id in the connection.
        :type object_id: str
        :param retries: Number of retries to submit before stopping.
        :type retries: int
        :returns: dict (using json.loads())
        """

        params = {t.RELATED_ID: object_id}
        return Broker.post(self._RELATED, params=params, retries=retries)
Beispiel #23
0
    def expire(self, timestamp, retries=None):
        """
        Expire by setting the 'expired_on' timestamp.

        :param timestamp: The timestamp to set for an expiration date.
        :type timestamp: str
        :param retries: Number of retries to submit before stopping.
        :type retries: int
        :returns: dict (using json.loads())
        """

        Broker.is_timestamp(timestamp)
        params = {td.EXPIRED_ON: timestamp}
        return Broker.post(self._DETAILS, params=params, retries=retries)
Beispiel #24
0
    def false_positive(self, object_id, retries=None):
        """
        Mark an object as a false positive by setting the status to
        UNKNOWN.

        :param object_id: The object-id of the object to mark.
        :type object_id: str
        :param retries: Number of retries to submit before stopping.
        :type retries: int
        :returns: dict (using json.loads())
        """

        params = {c.STATUS: s.UNKNOWN}
        return Broker.post(self._DETAILS, params=params, retries=retries)
Beispiel #25
0
    def save(self, params=None):
        """
        Submit changes to the graph to update an object. We will determine the
        Details URL and submit there (used for updating an existing object). If
        no parameters are provided, we will try to use get_changed() which may
        or may not be accurate (you have been warned!).

        :param params: The parameters to submit.
        :type params: dict
        :returns: dict (using json.loads())
        """

        if params is None:
            params = self.get_changed()
        return Broker.post(self._DETAILS, params=params)
Beispiel #26
0
    def add_connection(self, object_id, retries=None):
        """
        Use HTTP POST and add a connection between two objects.

        :param object_id: The other object-id in the connection.
        :type object_id: str
        :param retries: Number of retries to submit before stopping.
        :type retries: int
        :returns: dict (using json.loads())
        """

        params = {
            t.RELATED_ID: object_id
        }
        return Broker.post(self._RELATED, params=params, retries=retries)
Beispiel #27
0
    def expire(self, timestamp, retries=None):
        """
        Expire by setting the 'expired_on' timestamp.

        :param timestamp: The timestamp to set for an expiration date.
        :type timestamp: str
        :param retries: Number of retries to submit before stopping.
        :type retries: int
        :returns: dict (using json.loads())
        """

        Broker.is_timestamp(timestamp)
        params = {
            td.EXPIRED_ON: timestamp
        }
        return Broker.post(self._DETAILS, params=params, retries=retries)
Beispiel #28
0
    def false_positive(self, object_id, retries=None):
        """
        Mark an object as a false positive by setting the status to
        UNKNOWN.

        :param object_id: The object-id of the object to mark.
        :type object_id: str
        :param retries: Number of retries to submit before stopping.
        :type retries: int
        :returns: dict (using json.loads())
        """

        params = {
            c.STATUS: s.UNKNOWN
        }
        return Broker.post(self._DETAILS, params=params, retries=retries)
Beispiel #29
0
    def new(cls, params):
        """
        Submit params to the graph to add an object. We will submit to the
        object URL used for creating new objects in the graph. When submitting
        new objects you must provide privacy type and privacy members if the
        privacy type is something other than visible.

        :param params: The parameters to submit.
        :type params: dict
        :returns: dict (using json.loads())
        """

        if td.PRIVACY_TYPE not in params:
            raise pytxValueError('Must provide a %s' % td.PRIVACY_TYPE)
            pass
        else:
            if (params[td.PRIVACY_TYPE] != pt.VISIBLE and
                    len(params[td.PRIVACY_MEMBERS].split(',')) < 1):
                raise pytxValueError('Must provide %s' % td.PRIVACY_MEMBERS)
        return Broker.post(cls._URL, params=params)
Beispiel #30
0
    def submit(cls, *args, **kwargs):
        """
        Submit batch request. All non-named args are considered to be
        dictionaries containing the following:

            type: The request type (GET, POST, etc.).
            url: The full or relative URL for the API call.
            body: If the type is POST this is the body that will be used.

        If you use "method" instead of "type" and/or "relative_urL" instead of
        "url" (which is accurate to the Graph API) we will use them
        appropriately.

        If you pass a named argument, we will consider the name as the name you
        wish to include in that specific request. This is useful for referencing
        a request in another request in the Batch (see FB documentation).

        The following named args are considered to be the options below.

        :param include_headers: Include headers in response.
        :type include_headers: bool
        :param omit_response: Omit response on success.
        :type omit_response: bool
        :param retries: Number of retries 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: dict (using json.loads())
        """

        batch = []
        retries = kwargs.get('retries', None)
        if retries:
            del kwargs['retries']
        headers = kwargs.get('headers', None)
        if headers:
            del kwargs['headers']
        proxies = kwargs.get('proxies', None)
        if proxies:
            del kwargs['proxies']
        verify = kwargs.get('verify', None)
        if verify:
            del kwargs['verify']
        include_headers = kwargs.get('include_headers', None)
        if include_headers:
            del kwargs['include_headers']
            include_headers = Broker.sanitize_bool(include_headers)
        omit_response = kwargs.get('omit_response', None)
        if omit_response:
            del kwargs['omit_response']
            omit_response = Broker.sanitize_bool(omit_response)

        for arg in args:
            batch.append(Batch.prepare_single_request(arg))
        for key, value in kwargs.iteritems():
            batch.append(Batch.prepare_single_request(value, name=key))
        params = {
            t.ACCESS_TOKEN: get_access_token(),
            t.BATCH: json.dumps(batch),
            t.INCLUDE_HEADERS: include_headers,
            t.OMIT_RESPONSE_ON_SUCCESS: omit_response
        }
        try:
            return Broker.post(t.URL,
                               params=params,
                               retries=retries,
                               headers=headers,
                               proxies=proxies,
                               verify=verify)
        except:
            raise pytxFetchError('Error with batch request.')
Beispiel #31
0
    def send(cls_or_self,
             id_=None,
             params=None,
             type_=None,
             request_dict=False,
             retries=None,
             headers=None,
             proxies=None,
             verify=None):
        """
        Send custom params to the object URL. If `id` is provided it will be
        appended to the URL. If this is an uninstantiated class we will use the
        object type url (ex: /threat_descriptors/). If this is an instantiated
        object we will use the details URL. The type_ should be either GET or
        POST. We will default to GET if this is an uninstantiated class, and
        POST if this is an instantiated class.

        :param id_: ID of a graph object.
        :type id_: str
        :param params: Parameters to submit in the request.
        :type params: dict
        :param type_: GET or POST
        :type type_: str
        :param request_dict: Return a request dictionary only.
        :type request_dict: bool
        :param retries: Number of retries to submit 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: dict (using json.loads()), str
        """

        if isinstance(cls_or_self, type):
            url = cls_or_self._URL
            if type_ is None:
                type_ = 'GET'
        else:
            url = cls_or_self._DETAILS
            if type_ is None:
                type_ = 'POST'
        if id_ is not None and len(id_) > 0:
            url = url + id_ + '/'
        if params is None:
            params = {}
        if type_ == 'GET':
            if request_dict:
                return Broker.request_dict('GET',
                                           url,
                                           params=params)
            return Broker.get(url,
                              params=params,
                              retries=retries,
                              headers=headers,
                              proxies=proxies,
                              verify=verify)
        else:
            if request_dict:
                return Broker.request_dict('POST',
                                           url,
                                           body=params)
            return Broker.post(url,
                               params=params,
                               retries=retries,
                               headers=headers,
                               proxies=proxies,
                               verify=verify)
Beispiel #32
0
    def submit(cls,
               *args,
               **kwargs):
        """
        Submit batch request. All non-named args are considered to be
        dictionaries containing the following:

            type: The request type (GET, POST, etc.).
            url: The full or relative URL for the API call.
            body: If the type is POST this is the body that will be used.

        If you use "method" instead of "type" and/or "relative_urL" instead of
        "url" (which is accurate to the Graph API) we will use them
        appropriately.

        If you pass a named argument, we will consider the name as the name you
        wish to include in that specific request. This is useful for referencing
        a request in another request in the Batch (see FB documentation).

        The following named args are considered to be the options below.

        :param include_headers: Include headers in response.
        :type include_headers: bool
        :param omit_response: Omit response on success.
        :type omit_response: bool
        :param retries: Number of retries 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: dict (using json.loads())
        """

        batch = []
        retries = kwargs.get('retries', None)
        if retries:
            del kwargs['retries']
        headers = kwargs.get('headers', None)
        if headers:
            del kwargs['headers']
        proxies = kwargs.get('proxies', None)
        if proxies:
            del kwargs['proxies']
        verify = kwargs.get('verify', None)
        if verify:
            del kwargs['verify']
        include_headers = kwargs.get('include_headers', None)
        if include_headers:
            del kwargs['include_headers']
            include_headers = Broker.sanitize_bool(include_headers)
        omit_response = kwargs.get('omit_response', None)
        if omit_response:
            del kwargs['omit_response']
            omit_response = Broker.sanitize_bool(omit_response)

        for arg in args:
            batch.append(Batch.prepare_single_request(arg))
        for key, value in kwargs.iteritems():
            batch.append(Batch.prepare_single_request(value, name=key))
        params = {t.ACCESS_TOKEN: get_access_token(),
                  t.BATCH: json.dumps(batch),
                  t.INCLUDE_HEADERS: include_headers,
                  t.OMIT_RESPONSE_ON_SUCCESS: omit_response}
        try:
            return Broker.post(t.URL,
                               params=params,
                               retries=retries,
                               headers=headers,
                               proxies=proxies,
                               verify=verify)
        except:
            raise pytxFetchError('Error with batch request.')