From 51cc7e93b3588e820985ee83e412464455655598 Mon Sep 17 00:00:00 2001 From: isotr0py <2037008807@qq.com> Date: Wed, 25 Sep 2024 14:48:42 +0800 Subject: [PATCH 1/4] set proxy from envs when init session --- earthaccess/auth.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/earthaccess/auth.py b/earthaccess/auth.py index 4332379a..fa237ecd 100644 --- a/earthaccess/auth.py +++ b/earthaccess/auth.py @@ -45,6 +45,12 @@ def __init__( super().__init__() self.headers.update({"User-Agent": user_agent}) + proxy = { + "http": os.getenv("HTTP_PROXY", ""), + "https": os.getenv("HTTPS_PROXY", ""), + } + self.proxies.update({k: v for k, v in proxy.items() if v}) + if username and password: self.auth = (username, password) From 47b88ad42f80bb54f6a485563593383a0e6ec346 Mon Sep 17 00:00:00 2001 From: isotr0py <2037008807@qq.com> Date: Wed, 25 Sep 2024 16:24:37 +0800 Subject: [PATCH 2/4] add changelog --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a397ba3..79ffad1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,9 @@ [**@chuckwondo**](https://github.com/chuckwondo)) ### Added - +- Support setting proxy from environment variables for request session + ([#501](https://github.com/nsidc/earthaccess/issues/501)) + ([**@Isotr0py**](https://github.com/Isotr0py)) - Added Issue Templates ([#281](https://github.com/nsidc/earthaccess/issues/281)) ([**@Sherwin-14**](https://github.com/Sherwin-14)) From c9e804d2e0c83f3546c58bd2c5a6c4348069616f Mon Sep 17 00:00:00 2001 From: isotr0py <2037008807@qq.com> Date: Wed, 25 Sep 2024 16:25:26 +0800 Subject: [PATCH 3/4] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79ffad1a..acad2f9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ [**@chuckwondo**](https://github.com/chuckwondo)) ### Added + - Support setting proxy from environment variables for request session ([#501](https://github.com/nsidc/earthaccess/issues/501)) ([**@Isotr0py**](https://github.com/Isotr0py)) From eee2cfa847d2e34d436fea62f87c899e9ce94368 Mon Sep 17 00:00:00 2001 From: isotr0py <2037008807@qq.com> Date: Thu, 26 Sep 2024 13:39:49 +0800 Subject: [PATCH 4/4] cache get_session --- earthaccess/auth.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/earthaccess/auth.py b/earthaccess/auth.py index fa237ecd..3e722f51 100644 --- a/earthaccess/auth.py +++ b/earthaccess/auth.py @@ -4,6 +4,7 @@ import os import platform import shutil +from functools import lru_cache from netrc import NetrcParseError from pathlib import Path from typing import Any, Dict, List, Mapping, Optional @@ -45,12 +46,6 @@ def __init__( super().__init__() self.headers.update({"User-Agent": user_agent}) - proxy = { - "http": os.getenv("HTTP_PROXY", ""), - "https": os.getenv("HTTPS_PROXY", ""), - } - self.proxies.update({k: v for k, v in proxy.items() if v}) - if username and password: self.auth = (username, password) @@ -203,6 +198,7 @@ def get_s3_credentials( logger.info("We need to authenticate with EDL first") return {} + @lru_cache def get_session(self, bearer_token: bool = True) -> requests.Session: """Returns a new request session instance. @@ -213,6 +209,13 @@ def get_session(self, bearer_token: bool = True) -> requests.Session: class Session instance with Auth and bearer token headers """ session = SessionWithHeaderRedirection() + + proxy = { + "http": os.getenv("HTTP_PROXY", ""), + "https": os.getenv("HTTPS_PROXY", ""), + } + session.proxies.update({k: v for k, v in proxy.items() if v}) + if bearer_token and self.token: # This will avoid the use of the netrc after we are logged in session.trust_env = False