Browse Source

rewrite yourls response handler (#19)

Signed-off-by: r4sas <r4sas@i2pmail.org>
dependabot/add-v2-config-file
R4SAS 5 years ago
parent
commit
fb7a93732d
Signed by untrusted user: r4sas
GPG Key ID: 66F6C87B98EBCFE2
  1. 35
      pbincli/api.py

35
pbincli/api.py

@ -1,4 +1,5 @@
import requests import requests
from requests import HTTPError
from pbincli.utils import PBinCLIError from pbincli.utils import PBinCLIError
class PrivateBin: class PrivateBin:
@ -85,13 +86,14 @@ class Shortener:
PBinCLIError("YOURLS: An API URL is required") PBinCLIError("YOURLS: An API URL is required")
self.apiurl = settings['short_url'] self.apiurl = settings['short_url']
if not settings['short_user'] or not settings['short_pass']: if settings['short_user'] and settings['short_pass'] and settings['short_token'] is None:
if not settings['short_token']: self.auth_args = {'username': settings['short_user'], 'password': settings['short_pass']}
PBinCLIError("YOURLS: username and password or token are required") elif settings['short_user'] is None and settings['short_pass'] is None and settings['short_token']:
else:
self.auth_args = {'signature': settings['short_token']} self.auth_args = {'signature': settings['short_token']}
elif settings['short_user'] is None and settings['short_pass'] is None and settings['short_token'] is None:
self.auth_args = {}
else: else:
self.auth_args = {'username': settings['short_user'], 'password': settings['short_pass']} PBinCLIError("YOURLS: either username and password or token are required. Otherwise set to default (None)")
if settings['proxy']: if settings['proxy']:
self.proxy = {settings['proxy'].split('://')[0]: settings['proxy']} self.proxy = {settings['proxy'].split('://')[0]: settings['proxy']}
@ -116,15 +118,30 @@ class Shortener:
data = request) data = request)
try: try:
result.raise_for_status()
except HTTPError as http_exc:
try:
response = result.json()
except:
PBinCLIError("YOURLS: Unable parse response. Received (size = {}):\n{}".format(len(result.text), result.text))
else:
PBinCLIError("YOURLS: Received error from API: {} with JSON {}".format(result, response))
else:
response = result.json() response = result.json()
if response['status'] == 'fail' and response['code'] == 'error:keyword':
if {'status', 'code', 'message'} <= set(response.keys()):
status = response['status']
code = response['code']
message = response['message']
if status == 'fail':
PBinCLIError("YOURLS: Received error from API: {}".format(response['message'])) PBinCLIError("YOURLS: Received error from API: {}".format(response['message']))
if not 'shorturl' in response: if not 'shorturl' in response:
PBinCLIError("YOURLS: Unknown error: {}".format(response['message'])) PBinCLIError("YOURLS: Unknown error: {}".format(response['message']))
else:
print("Short Link:\t{}".format(response['shorturl'])) print("Short Link:\t{}".format(response['shorturl']))
except ValueError: else:
PBinCLIError("YOURLS: Unable parse response. Received (size = {}):\n{}".format(len(result.text), result.text)) PBinCLIError("YOURLS: No status, code and message fields in response! Received:\n{}".format(response))
elif self.api == 'clckru': elif self.api == 'clckru':
# from urllib.parse import quote_plus # from urllib.parse import quote_plus

Loading…
Cancel
Save