mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2025-04-14 16:27:13 +03:00
user_agent: Set this once in config.py and use it everywhere
* Also flake8/mypy pass on timeout_session.py
This commit is contained in:
parent
2fd72fde8e
commit
4ab6d62926
@ -30,7 +30,7 @@ import requests
|
|||||||
|
|
||||||
import config as conf_module
|
import config as conf_module
|
||||||
import protocol
|
import protocol
|
||||||
from config import appname, appversion, config
|
from config import config, user_agent
|
||||||
from edmc_data import companion_category_map as category_map
|
from edmc_data import companion_category_map as category_map
|
||||||
from EDMCLogging import get_main_logger
|
from EDMCLogging import get_main_logger
|
||||||
from monitor import monitor
|
from monitor import monitor
|
||||||
@ -54,7 +54,6 @@ auth_timeout = 30 # timeout for initial auth
|
|||||||
|
|
||||||
# Used by both class Auth and Session
|
# Used by both class Auth and Session
|
||||||
FRONTIER_AUTH_SERVER = 'https://auth.frontierstore.net'
|
FRONTIER_AUTH_SERVER = 'https://auth.frontierstore.net'
|
||||||
USER_AGENT = f'EDCD-{appname}-{appversion()}'
|
|
||||||
|
|
||||||
SERVER_LIVE = 'https://companion.orerve.net'
|
SERVER_LIVE = 'https://companion.orerve.net'
|
||||||
SERVER_BETA = 'https://pts-companion.orerve.net'
|
SERVER_BETA = 'https://pts-companion.orerve.net'
|
||||||
@ -305,7 +304,7 @@ class Auth(object):
|
|||||||
def __init__(self, cmdr: str) -> None:
|
def __init__(self, cmdr: str) -> None:
|
||||||
self.cmdr: str = cmdr
|
self.cmdr: str = cmdr
|
||||||
self.requests_session = requests.Session()
|
self.requests_session = requests.Session()
|
||||||
self.requests_session.headers['User-Agent'] = USER_AGENT
|
self.requests_session.headers['User-Agent'] = user_agent
|
||||||
self.verifier: Union[bytes, None] = None
|
self.verifier: Union[bytes, None] = None
|
||||||
self.state: Union[str, None] = None
|
self.state: Union[str, None] = None
|
||||||
|
|
||||||
@ -644,7 +643,7 @@ class Session(object):
|
|||||||
logger.debug('Starting session')
|
logger.debug('Starting session')
|
||||||
self.requests_session = requests.Session()
|
self.requests_session = requests.Session()
|
||||||
self.requests_session.headers['Authorization'] = f'Bearer {access_token}'
|
self.requests_session.headers['Authorization'] = f'Bearer {access_token}'
|
||||||
self.requests_session.headers['User-Agent'] = USER_AGENT
|
self.requests_session.headers['User-Agent'] = user_agent
|
||||||
self.state = Session.STATE_OK
|
self.state = Session.STATE_OK
|
||||||
|
|
||||||
def login(self, cmdr: str = None, is_beta: Optional[bool] = None) -> bool:
|
def login(self, cmdr: str = None, is_beta: Optional[bool] = None) -> bool:
|
||||||
|
@ -164,6 +164,9 @@ def appversion() -> semantic_version.Version:
|
|||||||
return _cached_version
|
return _cached_version
|
||||||
|
|
||||||
|
|
||||||
|
user_agent = f'EDCD-{appname}-{appversion()}'
|
||||||
|
|
||||||
|
|
||||||
def appversion_nobuild() -> semantic_version.Version:
|
def appversion_nobuild() -> semantic_version.Version:
|
||||||
"""
|
"""
|
||||||
Determine app version without *any* build meta data.
|
Determine app version without *any* build meta data.
|
||||||
|
@ -21,7 +21,7 @@ import killswitch
|
|||||||
import myNotebook as nb # noqa: N813
|
import myNotebook as nb # noqa: N813
|
||||||
import plug
|
import plug
|
||||||
from companion import CAPIData, category_map
|
from companion import CAPIData, category_map
|
||||||
from config import applongname, appname, appversion, appversion_nobuild, config, debug_senders
|
from config import applongname, appversion_nobuild, config, debug_senders, user_agent
|
||||||
from EDMCLogging import get_main_logger
|
from EDMCLogging import get_main_logger
|
||||||
from monitor import monitor
|
from monitor import monitor
|
||||||
from myNotebook import Frame
|
from myNotebook import Frame
|
||||||
@ -101,9 +101,6 @@ HORIZONS_SKU = 'ELITE_HORIZONS_V_PLANETARY_LANDINGS'
|
|||||||
# Thus do **NOT** use either of these in addition to the PLANETARY_LANDINGS
|
# Thus do **NOT** use either of these in addition to the PLANETARY_LANDINGS
|
||||||
# one.
|
# one.
|
||||||
|
|
||||||
# Custom user agent, to be clear in EDDN logs
|
|
||||||
USER_AGENT = f'EDCD-{appname}-{appversion()}'
|
|
||||||
|
|
||||||
|
|
||||||
# TODO: a good few of these methods are static or could be classmethods. they should be created as such.
|
# TODO: a good few of these methods are static or could be classmethods. they should be created as such.
|
||||||
|
|
||||||
@ -128,7 +125,7 @@ class EDDN:
|
|||||||
def __init__(self, parent: tk.Tk):
|
def __init__(self, parent: tk.Tk):
|
||||||
self.parent: tk.Tk = parent
|
self.parent: tk.Tk = parent
|
||||||
self.session = requests.Session()
|
self.session = requests.Session()
|
||||||
self.session.headers['User-Agent'] = USER_AGENT
|
self.session.headers['User-Agent'] = user_agent
|
||||||
self.replayfile: Optional[TextIO] = None # For delayed messages
|
self.replayfile: Optional[TextIO] = None # For delayed messages
|
||||||
self.replaylog: List[str] = []
|
self.replaylog: List[str] = []
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ import killswitch
|
|||||||
import myNotebook as nb # noqa: N813
|
import myNotebook as nb # noqa: N813
|
||||||
import plug
|
import plug
|
||||||
from companion import CAPIData
|
from companion import CAPIData
|
||||||
from config import applongname, appname, appversion, config, debug_senders
|
from config import applongname, appversion, config, debug_senders, user_agent
|
||||||
from edmc_data import DEBUG_WEBSERVER_HOST, DEBUG_WEBSERVER_PORT
|
from edmc_data import DEBUG_WEBSERVER_HOST, DEBUG_WEBSERVER_PORT
|
||||||
from EDMCLogging import get_main_logger
|
from EDMCLogging import get_main_logger
|
||||||
from ttkHyperlinkLabel import HyperlinkLabel
|
from ttkHyperlinkLabel import HyperlinkLabel
|
||||||
@ -37,8 +37,6 @@ logger = get_main_logger()
|
|||||||
EDSM_POLL = 0.1
|
EDSM_POLL = 0.1
|
||||||
_TIMEOUT = 20
|
_TIMEOUT = 20
|
||||||
DISCARDED_EVENTS_SLEEP = 10
|
DISCARDED_EVENTS_SLEEP = 10
|
||||||
# Custom user agent
|
|
||||||
USER_AGENT = f'EDCD-{appname}-{appversion()}'
|
|
||||||
|
|
||||||
# trace-if events
|
# trace-if events
|
||||||
CMDR_EVENTS = 'plugin.edsm.cmdr-events'
|
CMDR_EVENTS = 'plugin.edsm.cmdr-events'
|
||||||
@ -51,7 +49,7 @@ class This:
|
|||||||
self.shutting_down = False # Plugin is shutting down.
|
self.shutting_down = False # Plugin is shutting down.
|
||||||
|
|
||||||
self.session: requests.Session = requests.Session()
|
self.session: requests.Session = requests.Session()
|
||||||
self.session.headers['User-Agent'] = USER_AGENT
|
self.session.headers['User-Agent'] = user_agent
|
||||||
self.queue: Queue = Queue() # Items to be sent to EDSM by worker thread
|
self.queue: Queue = Queue() # Items to be sent to EDSM by worker thread
|
||||||
self.discarded_events: Set[str] = [] # List discarded events from EDSM
|
self.discarded_events: Set[str] = [] # List discarded events from EDSM
|
||||||
self.lastlookup: requests.Response # Result of last system lookup
|
self.lastlookup: requests.Response # Result of last system lookup
|
||||||
|
@ -1,22 +1,24 @@
|
|||||||
|
"""A requests.session with a TimeoutAdapter."""
|
||||||
import requests
|
import requests
|
||||||
from requests.adapters import HTTPAdapter
|
from requests.adapters import HTTPAdapter
|
||||||
|
|
||||||
|
from config import user_agent
|
||||||
|
|
||||||
REQUEST_TIMEOUT = 10 # reasonable timeout that all HTTP requests should use
|
REQUEST_TIMEOUT = 10 # reasonable timeout that all HTTP requests should use
|
||||||
|
|
||||||
|
|
||||||
class TimeoutAdapter(HTTPAdapter):
|
class TimeoutAdapter(HTTPAdapter):
|
||||||
"""
|
"""An HTTP Adapter that enforces an overridable default timeout on HTTP requests."""
|
||||||
TimeoutAdapter is an HTTP Adapter that enforces an overridable default timeout on HTTP requests.
|
|
||||||
"""
|
def __init__(self, timeout: int, *args, **kwargs):
|
||||||
def __init__(self, timeout, *args, **kwargs):
|
|
||||||
self.default_timeout = timeout
|
self.default_timeout = timeout
|
||||||
if kwargs.get("timeout") is not None:
|
if kwargs.get("timeout") is not None:
|
||||||
del kwargs["timeout"]
|
del kwargs["timeout"]
|
||||||
|
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
def send(self, *args, **kwargs):
|
def send(self, *args, **kwargs) -> requests.Response:
|
||||||
|
"""Send, but with a timeout always set."""
|
||||||
if kwargs["timeout"] is None:
|
if kwargs["timeout"] is None:
|
||||||
kwargs["timeout"] = self.default_timeout
|
kwargs["timeout"] = self.default_timeout
|
||||||
|
|
||||||
@ -25,7 +27,7 @@ class TimeoutAdapter(HTTPAdapter):
|
|||||||
|
|
||||||
def new_session(timeout: int = REQUEST_TIMEOUT, session: requests.Session = None) -> requests.Session:
|
def new_session(timeout: int = REQUEST_TIMEOUT, session: requests.Session = None) -> requests.Session:
|
||||||
"""
|
"""
|
||||||
new_session creates a new requests.Session and overrides the default HTTPAdapter with a TimeoutAdapter.
|
Create a new requests.Session and override the default HTTPAdapter with a TimeoutAdapter.
|
||||||
|
|
||||||
:param timeout: the timeout to set the TimeoutAdapter to, defaults to REQUEST_TIMEOUT
|
:param timeout: the timeout to set the TimeoutAdapter to, defaults to REQUEST_TIMEOUT
|
||||||
:param session: the Session object to attach the Adapter to, defaults to a new session
|
:param session: the Session object to attach the Adapter to, defaults to a new session
|
||||||
@ -33,6 +35,7 @@ def new_session(timeout: int = REQUEST_TIMEOUT, session: requests.Session = None
|
|||||||
"""
|
"""
|
||||||
if session is None:
|
if session is None:
|
||||||
session = requests.Session()
|
session = requests.Session()
|
||||||
|
session.headers['User-Agent'] = user_agent
|
||||||
|
|
||||||
adapter = TimeoutAdapter(timeout)
|
adapter = TimeoutAdapter(timeout)
|
||||||
session.mount("http://", adapter)
|
session.mount("http://", adapter)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user