Skip to content

Commit

Permalink
send Horizon client fingerprint (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed Feb 10, 2019
1 parent 25037c1 commit d3052c8
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions stellar_base/horizon.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
DEFAULT_REQUEST_TIMEOUT = 11 # two ledgers + 1 sec, let's retry faster and not wait 60 secs.
DEFAULT_NUM_RETRIES = 3
DEFAULT_BACKOFF_FACTOR = 0.5
USER_AGENT = 'py-stellar-base-{}'.format(__version__)
USER_AGENT = {
'X-Client-Name': 'py-stellar-base',
'X-Client-Version': __version__
}


class Horizon(object):
Expand All @@ -35,7 +38,7 @@ def __init__(self,
num_retries=DEFAULT_NUM_RETRIES,
request_timeout=DEFAULT_REQUEST_TIMEOUT,
backoff_factor=DEFAULT_BACKOFF_FACTOR,
user_agent=USER_AGENT):
user_agent=None):
"""The :class:`Horizon` object, which represents the interface for
making requests to a Horizon server instance.
Expand All @@ -54,9 +57,12 @@ def __init__(self,
:param int pool_size: persistent connection to Horizon and connection pool
:param int num_retries: configurable request retry functionality
:param float backoff_factor: a backoff factor to apply between attempts after the second try
:param str user_agent: String representing the user-agent you want, such as "py-stellar-base"
:param dict user_agent: representing the user-agent you want,
such as `{'X-Client-Name': 'py-stellar-base', 'X-Client-Version': __version__}`
"""
if user_agent is None:
self.user_agent = USER_AGENT
if horizon_uri is None:
self.horizon_uri = HORIZON_TEST
else:
Expand Down Expand Up @@ -89,7 +95,7 @@ def __init__(self,
session = requests.Session()

# set default headers
session.headers.update({'User-Agent': user_agent})
session.headers.update(self.user_agent)

session.mount('http://', adapter)
session.mount('https://', adapter)
Expand All @@ -104,7 +110,7 @@ def __init__(self,
pool_maxsize=self.pool_size,
max_retries=sse_retry)
sse_session = requests.Session()
sse_session.headers.update({'User-Agent': user_agent})
sse_session.headers.update(self.user_agent)
sse_session.mount('http://', sse_adapter)
sse_session.mount('https://', sse_adapter)
self._sse_session = sse_session
Expand Down Expand Up @@ -174,6 +180,9 @@ def _query(self, url, params=None, sse=False):
if SSEClient is None:
raise ImportError('SSE not supported, missing `stellar-base-sseclient` module')

# If SSE is enabled, Horizon will fetch the user-agent from the URL query params. Maybe it's not a good design.
params.update(self.user_agent)

return SSEClient(url, retry=0, session=self._sse_session, connect_retry=-1, params=params)

def account(self, address):
Expand Down

0 comments on commit d3052c8

Please sign in to comment.