Skip to content

Commit d2ad250

Browse files
committed
refactor: make session auth to use base service's http client
1 parent 8210e82 commit d2ad250

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

ibmcloudant/cloudant_base_service.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ def __init__(
9898
if isinstance(authenticator, CouchDbSessionAuthenticator):
9999
# Replacing BaseService's http.cookiejar.CookieJar as RequestsCookieJar supports update(CookieJar)
100100
self.jar = RequestsCookieJar(self.jar)
101-
self.authenticator.set_jar(self.jar) # Authenticators don't have access to cookie jars by default
101+
# Make token manager of CouchDbSessionAuthenticator to use the same http client as main service
102+
self.authenticator.set_http_client(self.get_http_client())
102103
add_hooks(self)
103104

104105
def set_service_url(self, service_url: str):

ibmcloudant/couchdb_session_authenticator.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# coding: utf-8
22

3-
# © Copyright IBM Corporation 2020, 2022.
3+
# © Copyright IBM Corporation 2020, 2025.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616
"""
1717
Module for handling session authentication
1818
"""
19-
from requests import Request
19+
from requests import Request, Session
2020

2121
from ibm_cloud_sdk_core.authenticators import Authenticator
2222
from .couchdb_session_token_manager import CouchDbSessionTokenManager
@@ -46,20 +46,21 @@ def __init__(self,
4646
if not isinstance(disable_ssl_verification, bool):
4747
raise TypeError('disable_ssl_verification must be a bool')
4848

49-
self.jar = None
50-
5149
self.token_manager = CouchDbSessionTokenManager(
5250
username,
5351
password,
5452
disable_ssl_verification=disable_ssl_verification
5553
)
5654
self.validate()
5755

58-
def set_jar(self, jar):
59-
"""Sets the cookie jar for the authenticator.
56+
def set_http_client(self, http_client: Session) -> None:
57+
"""Sets base serivice's http client for the authenticator.
6058
This is an internal method called by BaseService. Not to be called directly.
6159
"""
62-
self.jar = jar
60+
if isinstance(http_client, Session):
61+
self.token_manager.http_client = http_client
62+
else:
63+
raise TypeError("http_client parameter must be a requests.sessions.Session")
6364

6465
def validate(self):
6566
"""Validates the username, and password for session token requests.
@@ -82,11 +83,7 @@ def authenticate(self, req: Request):
8283
Args:
8384
req: Ignored. BaseService uses the cookie jar for every request
8485
"""
85-
jar = self.token_manager.get_token()
86-
# Requests seem to save cookies only for Sessions. BaseService is
87-
# hard-coded to work with "regular" requests requests so updating
88-
# the jar manually is necessary
89-
self.jar.update(jar)
86+
self.token_manager.get_token()
9087

9188
def authentication_type(self) -> str:
9289
"""Returns this authenticator's type ('COUCHDB_SESSION')."""

ibmcloudant/couchdb_session_token_manager.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# coding: utf-8
22

3-
# © Copyright IBM Corporation 2020, 2022.
3+
# © Copyright IBM Corporation 2020, 2025.
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616
"""
1717
Module for managing session authentication token
1818
"""
19+
from requests import Session
20+
1921
from ibm_cloud_sdk_core.token_managers.token_manager import TokenManager
2022

2123

@@ -52,6 +54,7 @@ def __init__(self, username: str, password: str,
5254

5355
self.token = None
5456

57+
self.http_client = Session()
5558
self.http_config = {}
5659
self.headers = None
5760

@@ -63,7 +66,7 @@ def request_token(self):
6366
A CookieJar of Cookies the server sent back.
6467
"""
6568

66-
response = self._request(
69+
response = self.http_client.request(
6770
method='POST',
6871
url=self.url + "/_session",
6972
headers=self.headers,

0 commit comments

Comments
 (0)