Skip to main content

CSession

The csession package implements a simple wrapper class around the Session class of the requests module called CustomSession that give more freedom to the programmer to customize his session by for example setting a default timeout or headers.

Installation

pip install csession

Usage

The core of the package is the CustomSession class which mostly acts just like a normal Session

from csession import CustomSession

csess = CustomSession()

_ = csess.get("http://myurl.com")
_ = csess.post("http://myurl.com")

additionally, the CustomSesion accepts all the parameters for a request in the constructor to be set as default

from csession import CustomSession

csess = CustomSession(timeout=30, headers={'Content-type': 'application/json'})

# all upcoming requests will timeout after 30 seconds and have a Content-type json header

...

the CustomSession can also set a default prepare methode to manipulate the request before they are sent. This is for example useful for authentication purposes or url prefixing. Here is a example for a session that is prefixing every request and also adding a default password an appId to the json body of a request

from csession import CustomSession

config = {...}

# prepare function accepts methode url and params and also returns those again
# params are formatted as a dict
def prepare_microlog_request(methode, url, params):
    url = "https://my-default-url_prefix"+ url
    params["json"] = dict(params["json"], **{
        'appId': config['APP_ID'],
        'password': config['PASSWORD'],
    })
    return methode, url, params


# Use this Session to make requests to microlog
csess = CustomSession(
    timeout=100,
    headers={'Content-type': 'application/json'},
    prepare=prepare_microlog_request
)

csess.get("/path/to/endpoint")

if for a single call the prepare methode should be suppressed you can do:

from csession import CustomSession
csess = CustomSession(prepare=...)

csess.use_prepare = False
csess.get("http://www.url_without_prepare.com")
csess.use_prepare = True

or equivalently use the provided context manager:

from csession import CustomSession, without_preparation
csess = CustomSession(prepare=...)

with without_preparation(csess) as sess:
    sess.get("http://www.url_without_prepare.com")

The CustomSession also provides a simple history function in for of a deque. History is deactivated by default but can be activated in the constructor by setting save_last_requests=

from csession import CustomSession, without_preparation

csess = CustomSession(save_last_requests=3) # the last 3 requests are stored

csess.get("http://www.myurl.com")

print(csess.history)

Metadata

Release files for csession 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for csession 0.1.4
File Size Uploaded
csession-0.1.4.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for csession 0.1.4
File Interpreter ABI Platform
csession-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 10.0 kB

Release files / csession-0.1.4.tar.gz

Download URL csession-0.1.4.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
dd20b6c19e77c44444231cddf8876c10d90b6c4f334d3be05c1e94acb9b14c73
BLAKE2b-256 checksum
How to use checksums
82a5c99b665c5350b58886a1423d802e8ff5a31ce911d2b50c6eb88dbecf59b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.10

Release files / csession-0.1.4-py3-none-any.whl

Download URL csession-0.1.4-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
066a41edb5a11625bf78d31b23b992b98445441fd6c0361b84474fbbe8fae428
BLAKE2b-256 checksum
How to use checksums
8a1f166302285c89df400b8e2a96ac48bba9b7682332bfb8bcb13deebd7c78bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page