pytx.request package

Module contents

class pytx.request.Broker[source]

Bases: object

The Broker handles validation and submission of requests as well as consumption and returning of the result. It is leveraged by the other classes.

Since the Broker takes care of the entire request/response cycle, it can be used on its own to interact with the ThreatExchange API without the need for the other classes if a developer wishes to use it.

classmethod build_get_parameters(text=None, strict_text=None, type_=None, threat_type=None, fields=None, limit=None, since=None, until=None)[source]

Validate arguments and convert them into GET parameters.

Parameters:
  • text (str) – The text used for limiting the search.
  • strict_text (bool, str, int) – Whether we should use strict searching.
  • type (str) – The Indicator type to limit to.
  • threat_type (str) – The Threat type to limit to.
  • fields (str, list) – Select specific fields to pull
  • limit (int, str) – The maximum number of objects to return.
  • since (str) – The timestamp to limit the beginning of the search.
  • until (str) – The timestamp to limit the end of the search.
Returns:

dict

classmethod delete(url, params=None)[source]

Send a DELETE request.

Parameters:
  • url (str) – The URL to send the DELETE request to.
  • params (dict) – The DELETE parameters to send in the request.
Returns:

dict (using json.loads())

classmethod get(url, params=None)[source]

Send a GET request.

Parameters:
  • url (str) – The URL to send the GET request to.
  • params (dict) – The GET parameters to send in the request.
Returns:

dict (using json.loads())

classmethod get_generator(klass, url, total, to_dict=False, params=None)[source]

Generator for managing GET requests. For each GET request it will yield the next object in the results until there are no more objects. If the GET response contains a ‘next’ value in the ‘paging’ section, the generator will automatically fetch the next set of results and continue the process until the total limit has been reached or there is no longer a ‘next’ value.

Parameters:
  • url (str) – The URL to send the GET request to.
  • total (None, int) – The total number of objects to return (-1 to disable).
  • to_dict (bool) – Return a dictionary instead of an instantiated class.
  • params (dict) – The GET parameters to send in the request.
Returns:

Generator

static get_new(klass, attrs)[source]

Return a new instance of klass.

Parameters:
  • klass – The class to create a new instance of.
  • attrs (dict) – The attributes to set for this new instance.
Returns:

new instance of klass

static handle_results(resp)[source]

Handle the results of a request.

Parameters:resp (response object) – The HTTP response.
Returns:dict (using json.loads())
static is_timestamp(timestamp)[source]

Verifies the timestamp provided is a valid timestamp.

Valid timestamps are based on PHP’s “strtotime” function. As of right now even with python’s “dateutil” library there are some strtotime valid strings that do not validate properly. Until such a time as this can become accurate and robust enough to have feature parity with strtotime, this will always return True and leave proper timestamps to the API user.

Parameters:timestamp (str) – Value to verify is a timestamp.
Returns:True
classmethod post(url, params=None)[source]

Send a POST request.

Parameters:
  • url (str) – The URL to send the POST request to.
  • params (dict) – The POST parameters to send in the request.
Returns:

dict (using json.loads())

static sanitize_strict(strict_text)[source]

If strict_text is provided, sanitize it.

‘true’ will be used if strict_text is in [True, ‘true’, ‘True’, 1]. ‘false’ will be used if strict_text is in [False, ‘false’, ‘False’, 0].

If we receive any other value strict_text will be set to None and ignored when building the GET request.

Parameters:strict_text (bool, str, int) – The value to sanitize.
Returns:str, None
classmethod validate_get(limit, since, until)[source]

Executes validation for the GET parameters: limit, since, until.

Parameters:
  • limit (int, str) – The limit to validate.
  • since (str) – The since timestamp to validate.
  • until (str) – The until timestamp to validate.
static validate_limit(limit)[source]

Verifies the limit provided is valid and within the max limit Facebook will allow you to use.

Parameters:limit (int, str) – Value to verify is a valid limit.
Returns:pytxValueError if invalid.

Table Of Contents

This Page