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

Allow for login with user/password to access restricted dataset #303

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
11 changes: 9 additions & 2 deletions erddapy/core/url.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""URL handling."""

import os
import copy
import functools
import io
from datetime import datetime
from typing import BinaryIO, Dict, List, Optional, Tuple, Union
from urllib.parse import quote_plus
from urllib.parse import quote_plus, urlparse

import httpx
import pytz
Expand All @@ -17,7 +18,13 @@

@functools.lru_cache(maxsize=128)
def _urlopen(url: str, auth: Optional[tuple] = None, **kwargs: Dict) -> BinaryIO:
response = httpx.get(url, follow_redirects=True, auth=auth, **kwargs)
with httpx.Client() as client:
p = urlparse(url)
protocol = 'tabledap' if 'tabledap' in p.path else 'griddap'
login_page = "%s://%s%s/login.html" % (p.scheme, p.netloc, p.path.split('/%s/' % protocol)[0])
client.post(login_page, data={'user': os.getenv("ERDDAP_USERNAME"), 'password': os.getenv("ERDDAP_PASSWORD")})
response = client.get(url, follow_redirects=True, auth=auth, **kwargs)

try:
response.raise_for_status()
except httpx.HTTPError as err:
Expand Down