sentinelhub.download.session

Module implementing Sentinel Hub session object

class sentinelhub.download.session.SentinelHubSession(config=None, refresh_before_expiry=120, *, _token=None)[source]

Bases: object

Sentinel Hub authentication class

The class will do OAuth2 authentication with Sentinel Hub service and store the token. It is able to refresh the token before it expires and decode user information from the token.

For more info about Sentinel Hub authentication check service documentation.

Parameters:
  • config (SHConfig | None) – A config object containing Sentinel Hub OAuth credentials and the base URL of the service.

  • refresh_before_expiry (float | None) – A number of seconds before authentication token expiry at which time a refreshing mechanism is activated. When this is activated it means that whenever a valid token will be again required the SentinelHubSession will re-authenticate to Sentinel Hub service and obtain a new token. By default, the parameter is set to 60 seconds. If this parameter is set to None it will deactivate token refreshing and SentinelHubSession might provide a token that is already expired. This can be used to avoid re-authenticating too many times.

  • _token (JsonDict | None) –

DEFAULT_SECONDS_BEFORE_EXPIRY = 120
DEFAULT_HEADERS: ClassVar[dict[str, str]] = {'Content-Type': 'application/x-www-form-urlencoded'}
classmethod from_token(token)[source]

Create a session object from the given token. The created session is configured not to refresh its token.

Parameters:

token (Dict[str, Any]) – A dictionary containing token object.

Return type:

SentinelHubSession

property token: Dict[str, Any]

Always up-to-date session’s token

Returns:

A token in a form of dictionary of parameters

info()[source]

Decode token to get token info

Return type:

Dict[str, Any]

property session_headers: dict[str, str]

Provides session authorization headers

Returns:

A dictionary with authorization headers.

class sentinelhub.download.session.SessionSharingThread(session, memory_name='sh-session-token', **kwargs)[source]

Bases: Thread

A thread for sharing a token from SentinelHubSession object in a shared memory object that can be accessed by other Python processes during multiprocessing parallelization.

How to use it:

thread = SessionSharingThread(session)
thread.start()

# Run a parallelization process here
# Use collect_shared_session() to retrieve the session with other processes

thread.join()
Parameters:
  • session (SentinelHubSession) – A Sentinel Hub session to be used for sharing its authentication token.

  • memory_name (str) – A unique name for the requested shared memory block.

  • kwargs (Any) – Keyword arguments to be propagated to threading.Thread parent class.

start()[source]

Start running the thread.

After starting the thread it also waits for the token to be shared. This way no other process would try to access the memory before it even exists.

Return type:

None

run()[source]

A running thread is running an infinite loop of sharing a token and waiting for token to expire. The loop ends only when the thread is stopped.

Return type:

None

join(timeout=None)[source]

The method stops the thread that would otherwise run indefinitely and joins it with the main thread.

Parameters:

timeout (float | None) – Parameter that is propagated to threading.Thread.join method.

Return type:

None

class sentinelhub.download.session.SessionSharing(session, **kwargs)[source]

Bases: object

An object that in the background runs a SessionSharingThread which shares a Sentinel Hub authentication token in a shared memory object that can be accessed by other Python processes during multiprocessing parallelization. The object also makes sure that the thread is always closed at the end.

How to use it:

with SessionSharing(session):
    # Run a parallelization process here
Parameters:
  • args – A Sentinel Hub session to be used for sharing its authentication token.

  • kwargs (Any) – Keyword arguments to be propagated to SessionSharingThread.

  • session (SentinelHubSession) –

sentinelhub.download.session.collect_shared_session(memory_name='sh-session-token')[source]

This utility function is meant to be used in combination with SessionSharingThread. It retrieves an authentication token from the shared memory and returns it in an SentinelHubSession object.

Parameters:

memory_name (str) – A unique name of the requested shared memory block from where to read the session. It should match the one used in SessionSharingThread.

Returns:

An instance of SentinelHubSession that contains the shared token but is not self-refreshing.

Return type:

SentinelHubSession