rbw / aiosnow

Asynchronous ServiceNow Library
MIT License
73 stars 12 forks source link

Reusing http session (feature request) #64

Closed trosos closed 4 years ago

trosos commented 4 years ago

I would appreciate having an option to use an existing, possibly already cookie-authenticated aiohttp session (without the need of subclassing snow.Application or monkey-patching snow.Application.get_session).

The Application instantiation then might look like:

http_session = aiohttp.ClientSession()

async with http_session.post(
    "https://<instance_name>.service-now.com/api/now/amb/session/start",
    auth=aiohttp.BasicAuth("<username>", "<password>"),
    skip_auto_headers={"Content-Type"},
) as response:
    assert response.status//100 == 2
    await response.text()

app = snow.Application({"http_session": http_session})

Usecase: I want to share the http session with aiocometd, which needs to perform http authentication only once (at POST /api/now/amb/session/start), and then uses cookies for authenticating any further communication; this also works for ServiceNow Table API.

rbw commented 4 years ago

Sounds like a good suggestion, thanks.

I'm actually reworking the request-response code a bit right now, I'll add support for this while I'm at it.

I'll let you know when there's a PR.

rbw commented 4 years ago

I've reworked the Application argument handling a bit and added support for passing custom session objects.

73