Skip to content

nasa_utils

SessionWithHeaderRedirection

Bases: Session

NASA Session generator

Parameters:

Name Type Description Default
username type

Description of parameter username.

required
password type

Description of parameter password.

required

Attributes:

Name Type Description
auth type

Description of attribute auth.

AUTH_HOST type

Description of attribute AUTH_HOST.

Source code in monetio/sat/nasa_utils.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class SessionWithHeaderRedirection(requests.Session):
    """NASA Session generator

    Parameters
    ----------
    username : type
        Description of parameter `username`.
    password : type
        Description of parameter `password`.

    Attributes
    ----------
    auth : type
        Description of attribute `auth`.
    AUTH_HOST : type
        Description of attribute `AUTH_HOST`.

    """

    AUTH_HOST = "urs.earthdata.nasa.gov"

    def __init__(self, username, password):
        super().__init__()
        self.auth = (username, password)

    # Overrides from the library to keep headers when redirected to or from
    # the NASA auth host.
    def rebuild_auth(self, prepared_request, response):
        headers = prepared_request.headers
        url = prepared_request.url
        if "Authorization" in headers:
            original_parsed = requests.utils.urlparse(response.request.url)
            redirect_parsed = requests.utils.urlparse(url)
            if (
                (original_parsed.hostname != redirect_parsed.hostname)
                and redirect_parsed.hostname != self.AUTH_HOST
                and original_parsed.hostname != self.AUTH_HOST
            ):
                del headers["Authorization"]

        return