本文整理汇总了Python中request.Broker类的典型用法代码示例。如果您正苦于以下问题:Python Broker类的具体用法?Python Broker怎么用?Python Broker使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Broker类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_generator
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)
示例2: save
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)
示例3: send
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)
示例4: objects
def objects(cls, text=None, strict_text=False, type_=None, threat_type=None,
fields=None, limit=None, since=None, until=None, __raw__=None,
full_response=False, dict_generator=False, retries=None):
"""
Get objects from ThreatExchange.
:param text: The text used for limiting the search.
:type text: str
:param strict_text: Whether we should use strict searching.
:type strict_text: bool, str, int
:param type_: The Indicator type to limit to.
:type type_: str
:param threat_type: The Threat type to limit to.
:type threat_type: str
:param fields: Select specific fields to pull
:type fields: str, list
:param limit: The maximum number of objects to return.
:type limit: int, str
:param since: The timestamp to limit the beginning of the search.
:type since: str
:param until: The timestamp to limit the end of the search.
:type until: str
:param __raw__: Provide a dictionary to force as GET parameters.
Overrides all other arguments.
:type __raw__: dict
: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
:returns: Generator, dict (using json.loads())
"""
if __raw__:
if isinstance(__raw__, dict):
params = __raw__
else:
raise pytxValueError('__raw__ must be of type dict')
else:
params = Broker.build_get_parameters(
text=text,
strict_text=strict_text,
type_=type_,
threat_type=threat_type,
fields=fields,
limit=limit,
since=since,
until=until,
)
if full_response:
return Broker.get(cls._URL, params=params, retries=retries)
else:
return Broker.get_generator(cls,
cls._URL,
to_dict=dict_generator,
params=params,
retries=retries)
示例5: expire
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)
示例6: mine
def mine(cls,
role=None,
full_response=False,
dict_generator=False,
retries=None,
headers=None,
proxies=None,
verify=None):
"""
Find all of the Threat Privacy Groups that I am either the owner or a
member.
:param role: Whether you are an 'owner' or a 'member'
:type role: 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
:returns: Generator, dict (using json.loads())
"""
if role is None:
raise pytxValueError('Must provide a role')
app_id = get_app_id() + '/'
if role == 'owner':
role = t.THREAT_PRIVACY_GROUPS_OWNER
elif role == 'member':
role = t.THREAT_PRIVACY_GROUPS_MEMBER
else:
raise pytxValueError('Role must be "owner" or "member"')
params = {'fields': ','.join(cls._fields)}
url = t.URL + t.VERSION + app_id + role
if full_response:
return Broker.get(url,
params=params,
retries=retries,
headers=headers,
proxies=proxies,
verify=verify)
else:
return Broker.get_generator(cls,
url,
params=params,
to_dict=dict_generator,
retries=retries,
headers=headers,
proxies=proxies,
verify=verify)
示例7: expire
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)
self.set(ti.EXPIRED_ON, timestamp)
self.save()
示例8: expire
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)
示例9: save
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)
示例10: new
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)
示例11: add_connection
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)
示例12: save
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)
示例13: delete_connection
def delete_connection(self,
object_id,
retries=None,
headers=None,
proxies=None,
verify=None):
"""
Use HTTP DELETE and remove the connection to another object.
: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.delete(self._RELATED,
params=params,
retries=retries,
headers=headers,
proxies=proxies,
verify=verify)
示例14: get_members
def get_members(self,
retries=None,
headers=None,
proxies=None,
verify=None):
"""
Get the members of a Threat Privacy Group
: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
"""
url = self._DETAILS + tpg.MEMBERS
results = Broker.get(url,
retries=retries,
headers=headers,
proxies=proxies,
verify=verify)
if t.DATA in results:
return results[t.DATA]
else:
return []
示例15: false_positive
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)