utils: Add format_user_agent helper
This commit is contained in:
parent
a48aadaaed
commit
5153d9e19f
@ -1,5 +1,11 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import platform
|
||||
|
||||
import mopidy
|
||||
|
||||
"Helpers for configuring HTTP clients used in Mopidy extensions."
|
||||
|
||||
|
||||
def format_proxy(proxy_config):
|
||||
"""Convert a Mopidy proxy config to the commonly used proxy string format.
|
||||
@ -23,3 +29,17 @@ def format_proxy(proxy_config):
|
||||
username=proxy_config.get('username'),
|
||||
password=proxy_config.get('password'),
|
||||
hostname=proxy_config['hostname'], port=port)
|
||||
|
||||
|
||||
def format_user_agent(name=None):
|
||||
"""Construct a User-Agent suitable for use in client code.
|
||||
|
||||
This will identify use by the provided name (which should be
|
||||
``dist_name/version``), Mopidy version and Python version.
|
||||
"""
|
||||
parts = ['Mopidy/%s' % (mopidy.__version__),
|
||||
'%s/%s' % (platform.python_implementation(),
|
||||
platform.python_version())]
|
||||
if name:
|
||||
parts.insert(0, name)
|
||||
return ' '.join(parts)
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import re
|
||||
|
||||
import pytest
|
||||
|
||||
from mopidy.utils import http
|
||||
@ -18,3 +20,12 @@ from mopidy.utils import http
|
||||
])
|
||||
def test_format_proxy(config, expected):
|
||||
assert http.format_proxy(config) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name,expected", [
|
||||
(None, r'^Mopidy/[^ ]+ CPython|/[^ ]+$'),
|
||||
('Foo', r'^Foo Mopidy/[^ ]+ CPython|/[^ ]+$'),
|
||||
('Foo/1.2.3', r'^Foo/1.2.3 Mopidy/[^ ]+ CPython|/[^ ]+$'),
|
||||
])
|
||||
def test_format_user_agent(name, expected):
|
||||
assert re.match(expected, http.format_user_agent(name))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user