Skip to content

Commit 51f1e0b

Browse files
committed
allow custom session, re-use session if it exists
1 parent e81cd9c commit 51f1e0b

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

pyiceberg/catalog/rest/__init__.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
from pyiceberg import __version__
3434
from pyiceberg.catalog import (
35+
BOTOCORE_SESSION,
3536
TOKEN,
3637
URI,
3738
WAREHOUSE_LOCATION,
@@ -53,6 +54,7 @@
5354
TableAlreadyExistsError,
5455
UnauthorizedError,
5556
)
57+
from pyiceberg.io import AWS_ACCESS_KEY_ID, AWS_REGION, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN
5658
from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionSpec, assign_fresh_partition_spec_ids
5759
from pyiceberg.schema import Schema, assign_fresh_schema_ids
5860
from pyiceberg.table import (
@@ -72,7 +74,7 @@
7274
from pyiceberg.typedef import EMPTY_DICT, UTF8, IcebergBaseModel, Identifier, Properties
7375
from pyiceberg.types import transform_dict_value_to_str
7476
from pyiceberg.utils.deprecated import deprecation_message
75-
from pyiceberg.utils.properties import get_header_properties, property_as_bool
77+
from pyiceberg.utils.properties import get_first_property_value, get_header_properties, property_as_bool
7678

7779
if TYPE_CHECKING:
7880
import pyarrow as pa
@@ -390,11 +392,17 @@ class SigV4Adapter(HTTPAdapter):
390392
def __init__(self, **properties: str):
391393
super().__init__()
392394
self._properties = properties
395+
self._boto_session = boto3.Session(
396+
region_name=get_first_property_value(self._properties, AWS_REGION),
397+
botocore_session=self._properties.get(BOTOCORE_SESSION),
398+
aws_access_key_id=get_first_property_value(self._properties, AWS_ACCESS_KEY_ID),
399+
aws_secret_access_key=get_first_property_value(self._properties, AWS_SECRET_ACCESS_KEY),
400+
aws_session_toxken=get_first_property_value(self._properties, AWS_SESSION_TOKEN),
401+
)
393402

394403
def add_headers(self, request: PreparedRequest, **kwargs: Any) -> None: # pylint: disable=W0613
395-
boto_session = boto3.Session()
396-
credentials = boto_session.get_credentials().get_frozen_credentials()
397-
region = self._properties.get(SIGV4_REGION, boto_session.region_name)
404+
credentials = self._boto_session.get_credentials().get_frozen_credentials()
405+
region = self._properties.get(SIGV4_REGION, self._boto_session.region_name)
398406
service = self._properties.get(SIGV4_SERVICE, "execute-api")
399407

400408
url = str(request.url).split("?")[0]

0 commit comments

Comments
 (0)