HTTPTask#

top.web.task.HTTPTask class.

class HTTPTask[source]#

Bases: Task

A task for tracking HTTP requests.

__init__(task_id=None, task_name=None, host_name=None, process_id=None, thread_id=None, process_internal_id=None, processor_name=None, started_at=None, updated_at=None, ended_at=None, recorded_successfully=None, tags=None, command_line=None, protocol=None, host=None, method=None, path=None, params=None, uri=None, client_ip_address=None, request_headers=None, status_code=None, status_message=None, response_headers=None)#
Parameters:
Return type:

None

Methods

__init__([task_id, task_name, host_name, ...])

create_from_current_thread(task_id[, ...])

Create a task and assuming the processor is the current OS thread.

deserialise(blob)

Serialise using dataclasses_json

from_dict(kvs, *[, infer_missing])

from_json(s, *[, parse_float, parse_int, ...])

get_accept_encoding()

HTTP request header shortcut method.

get_ago()

Get how long ago this task finished.

get_content_length()

Get the content length of the response.

get_duration()

Get the duration of this task.

get_flag()

Return the Unicode flag emoticon based on the country IP address.

get_host()

HTTP request header shortcut method.

get_ip_country()

Return the country code of the client IP address.

get_original_ip()

Get the originating IP address of the requestor.

get_processor_tracking_id()

Get the key used in our tracking table (Redis) for this task.

get_single_request_header(name)

Get a value of a single HTTP header in a request.

get_single_response_header(name)

Get a value of a single HTTP header in a response.

get_user_agent()

HTTP request header shortcut method.

schema(*[, infer_missing, only, exclude, ...])

serialise()

Serialise using dataclasS_json

to_dict([encode_json])

to_json(*[, skipkeys, ensure_ascii, ...])

Attributes

client_ip_address

IP address that connected to the web server.

command_line

Application command line

ended_at

When this task was ended.

host

Host from incoming HTTP request

host_name

Host name on multiserver deployments.

method

HTTP method like GET, POST, put

params

HTTP GET request params

path

Request path, like /api/my-func

process_id

OS process id that started this task.

process_internal_id

If the application provides further ids for the processes.

processor_name

Human readable of the processor name is available

protocol

Protocol from incoming HTTP request

recorded_successfully

Did this task success?

request_headers

Request HTTP headers.

response_headers

Response HTTP headers.

started_at

When this task was started.

status_code

When response has been generated, what code did we sent.

status_message

Server status message

tags

Generic tracking tags that can be associated with tasks.

task_id

Unique identified for this task.

task_name

method and path.

thread_id

OS thread that started this task.

updated_at

When this task was last updated.

uri

The full request URI if available

protocol: Optional[str] = None#

Protocol from incoming HTTP request

host: Optional[str] = None#

Host from incoming HTTP request

method: Optional[str] = None#

HTTP method like GET, POST, put

path: Optional[str] = None#

Request path, like /api/my-func

params: Optional[dict] = None#

HTTP GET request params

uri: Optional[str] = None#

The full request URI if available

client_ip_address: Optional[str] = None#

IP address that connected to the web server.

This is the direct IP address of the TCP/IP connection. You probably want get_original_ip() in most of the cases.

request_headers: Optional[List[Tuple[str, str]]] = None#

Request HTTP headers.

Available as key value mapping.

Note that in HTTP protocol a header can appear twice.

Uppercase all key names.

status_code: Optional[int] = None#

When response has been generated, what code did we sent. Only available when the request processing has finished.

status_message: Optional[str] = None#

Server status message

response_headers: Optional[List[Tuple[str, str]]] = None#

Response HTTP headers.

Available as key value mapping.

Note that in HTTP protocol a header can appear twice. Uppercase all key names.

get_single_request_header(name)[source]#

Get a value of a single HTTP header in a request.

Parameters:

name (str) – Case insensitive HTTP header name

Raises:

AssertionError – If the same header appears twice

Returns:

The header value

Return type:

Optional[str]

get_single_response_header(name)[source]#

Get a value of a single HTTP header in a response.

Parameters:

name (str) – Case insensitive HTTP header name

Raises:

AssertionError – If the same header appears twice

Returns:

The header value

Return type:

Optional[str]

get_host()[source]#

HTTP request header shortcut method.

Return type:

str

get_user_agent()[source]#

HTTP request header shortcut method.

Return type:

str

get_accept_encoding()[source]#

HTTP request header shortcut method.

Return type:

str

get_content_length()[source]#

Get the content length of the response.

If not set return None.

Return type:

Optional[int]

get_ip_country()[source]#

Return the country code of the client IP address.

Currently only supports cf-ipcountry header.

Returns:

ISO 3166-1 Alpha 2 country code of the client as uppercase. Special codes like T1 are returned for Tor network, etc.

Return type:

str

get_flag()[source]#

Return the Unicode flag emoticon based on the country IP address.

Based on emoji-country-flag.

See get_ip_country()

Return type:

str

get_original_ip()[source]#

Get the originating IP address of the requestor.

In the case the HTTP request was forwarded through services like Cloudflare and reverse proxies like Nginx and Apache, get the user IP address.

Return type:

str

__init__(task_id=None, task_name=None, host_name=None, process_id=None, thread_id=None, process_internal_id=None, processor_name=None, started_at=None, updated_at=None, ended_at=None, recorded_successfully=None, tags=None, command_line=None, protocol=None, host=None, method=None, path=None, params=None, uri=None, client_ip_address=None, request_headers=None, status_code=None, status_message=None, response_headers=None)#
Parameters:
Return type:

None