Set User-Agent according to twtxt.disclose_identity
This commit is contained in:
parent
0247de2875
commit
15f5af95b5
|
@ -18,4 +18,3 @@ To-do
|
|||
|
||||
* Parsing, enhanced outputs and porcelain mode, just like with `twtxt`_
|
||||
* Verbose output and logging
|
||||
* Set user-agent depending on twtxt's ``disclose_identity``
|
||||
|
|
|
@ -1 +1,7 @@
|
|||
from twtxt_registry_client.client import RegistryClient # noqa: F401
|
||||
|
||||
|
||||
__title__ = 'twtxt-registry-client'
|
||||
__version__ = VERSION = '0.1.0'
|
||||
__author__ = 'Lucidiot'
|
||||
__license__ = 'GNU GPL 3'
|
||||
|
|
|
@ -7,6 +7,7 @@ from twtxt_registry_client import RegistryClient
|
|||
|
||||
@click.group(name='twtxt-registry')
|
||||
@click.argument('registry_url', required=True)
|
||||
@click.version_option()
|
||||
@click.option('-k', '--insecure', is_flag=True)
|
||||
@click.pass_context
|
||||
def cli(ctx, registry_url, insecure):
|
||||
|
|
|
@ -1,14 +1,34 @@
|
|||
import urllib
|
||||
import requests
|
||||
from twtxt.config import Config
|
||||
|
||||
|
||||
class RegistryClient(object):
|
||||
|
||||
def __init__(self, registry_url, insecure=False):
|
||||
def __init__(self, registry_url, insecure=False, disclose_identity=None):
|
||||
self.registry_url = registry_url
|
||||
self.session = requests.Session()
|
||||
self.session.verify = not insecure
|
||||
|
||||
from twtxt_registry_client import __version__
|
||||
if disclose_identity or disclose_identity is None:
|
||||
try:
|
||||
config = Config.discover()
|
||||
except ValueError:
|
||||
disclose_identity = False
|
||||
else:
|
||||
disclose_identity = config.disclose_identity
|
||||
|
||||
if disclose_identity:
|
||||
user_agent = 'twtxt-registry/{} (+{}; @{})'.format(
|
||||
__version__,
|
||||
config.twturl,
|
||||
config.nick,
|
||||
)
|
||||
else:
|
||||
user_agent = 'twtxt-registry/{}'.format(__version__)
|
||||
self.session.headers['User-Agent'] = user_agent
|
||||
|
||||
def request(self, method, endpoint, *, format='plain', **params):
|
||||
resp = method(
|
||||
'/'.join([self.registry_url, format, endpoint]),
|
||||
|
|
Reference in New Issue