Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support setting proxy from environment variables for request session #823

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

### 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))
Expand Down
9 changes: 9 additions & 0 deletions earthaccess/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -197,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.

Expand All @@ -207,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
Expand Down
Loading