Add basic error handling, close #2
This commit is contained in:
parent
1b99dffd88
commit
d0173641ef
|
@ -1,6 +1,7 @@
|
|||
#!/usr/bin/env python3
|
||||
from urllib.parse import urlsplit, urlunsplit
|
||||
from objtools.collections import Namespace
|
||||
from requests.exceptions import HTTPError
|
||||
from twtxt.config import Config
|
||||
from twtxt_registry_client import RegistryClient, output
|
||||
import click
|
||||
|
@ -54,7 +55,7 @@ def register(ctx, nickname, url):
|
|||
url = url or config.twturl
|
||||
|
||||
click.echo(ctx.obj.formatter.format_response(
|
||||
ctx.obj.client.register(nickname, url)
|
||||
ctx.obj.client.register(nickname, url, raise_exc=False)
|
||||
))
|
||||
|
||||
|
||||
|
@ -62,18 +63,24 @@ def register(ctx, nickname, url):
|
|||
@click.option('-q', '--query')
|
||||
@click.pass_context
|
||||
def users(ctx, query):
|
||||
try:
|
||||
click.echo(ctx.obj.formatter.format_users(
|
||||
ctx.obj.client.list_users(q=query)
|
||||
))
|
||||
except HTTPError as e:
|
||||
click.echo(ctx.obj.formatter.format_response(e.response))
|
||||
|
||||
|
||||
@cli.command()
|
||||
@click.option('-q', '--query')
|
||||
@click.pass_context
|
||||
def tweets(ctx, query):
|
||||
try:
|
||||
click.echo(ctx.obj.formatter.format_tweets(
|
||||
ctx.obj.client.list_tweets(q=query)
|
||||
))
|
||||
except HTTPError as e:
|
||||
click.echo(ctx.obj.formatter.format_response(e.response))
|
||||
|
||||
|
||||
@cli.command()
|
||||
|
@ -103,18 +110,24 @@ def mentions(ctx, name_or_url):
|
|||
)
|
||||
url = config.twturl
|
||||
|
||||
try:
|
||||
click.echo(ctx.obj.formatter.format_tweets(
|
||||
ctx.obj.client.list_mentions(url)
|
||||
))
|
||||
except HTTPError as e:
|
||||
click.echo(ctx.obj.formatter.format_response(e.response))
|
||||
|
||||
|
||||
@cli.command()
|
||||
@click.argument('name', required=True)
|
||||
@click.pass_context
|
||||
def tag(ctx, name):
|
||||
try:
|
||||
click.echo(ctx.obj.formatter.format_tweets(
|
||||
ctx.obj.client.list_tag_tweets(name)
|
||||
))
|
||||
except HTTPError as e:
|
||||
click.echo(ctx.obj.formatter.format_response(e.response))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
@ -29,13 +29,15 @@ class RegistryClient(object):
|
|||
user_agent = 'twtxt-registry/{}'.format(__version__)
|
||||
self.session.headers['User-Agent'] = user_agent
|
||||
|
||||
def request(self, method, endpoint, *, format='plain', **params):
|
||||
def request(self, method, endpoint,
|
||||
*, format='plain', raise_exc=True, **params):
|
||||
resp = method(
|
||||
'/'.join([self.registry_url, format, endpoint]),
|
||||
# Ignore parameters with None values
|
||||
params={k: v for k, v in params.items() if v},
|
||||
)
|
||||
resp.raise_for_status()
|
||||
if raise_exc:
|
||||
resp.raise_for_status()
|
||||
return resp
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
|
@ -44,37 +46,17 @@ class RegistryClient(object):
|
|||
def post(self, *args, **kwargs):
|
||||
return self.request(self.session.post, *args, **kwargs)
|
||||
|
||||
def register(self, nickname, url, *, format='plain'):
|
||||
return self.post(
|
||||
'users',
|
||||
format=format,
|
||||
nickname=nickname,
|
||||
url=url,
|
||||
)
|
||||
def register(self, nickname, url, **kwargs):
|
||||
return self.post('users', nickname=nickname, url=url, **kwargs)
|
||||
|
||||
def list_users(self, *, q=None, format='plain'):
|
||||
return self.get(
|
||||
'users',
|
||||
q=q,
|
||||
format=format,
|
||||
)
|
||||
def list_users(self, *, q=None, **kwargs):
|
||||
return self.get('users', q=q, **kwargs)
|
||||
|
||||
def list_tweets(self, *, q=None, format='plain'):
|
||||
return self.get(
|
||||
'tweets',
|
||||
q=q,
|
||||
format=format,
|
||||
)
|
||||
def list_tweets(self, *, q=None, **kwargs):
|
||||
return self.get('tweets', q=q, **kwargs)
|
||||
|
||||
def list_mentions(self, url, *, format='plain'):
|
||||
return self.get(
|
||||
'mentions',
|
||||
url=url,
|
||||
format=format,
|
||||
)
|
||||
def list_mentions(self, url, **kwargs):
|
||||
return self.get('mentions', url=url, **kwargs)
|
||||
|
||||
def list_tag_tweets(self, name, *, format='plain'):
|
||||
return self.get(
|
||||
'tags/{}'.format(urllib.parse.quote(name)),
|
||||
format=format,
|
||||
)
|
||||
def list_tag_tweets(self, name, **kwargs):
|
||||
return self.get('tags/{}'.format(urllib.parse.quote(name)), **kwargs)
|
||||
|
|
Reference in New Issue