added http api compatibility test

This commit is contained in:
DYefremov
2020-01-06 13:17:56 +03:00
parent bcbe2b3f46
commit 1d62e2660b
3 changed files with 29 additions and 10 deletions

View File

@@ -45,6 +45,10 @@ class TestException(Exception):
pass
class HttpApiException(Exception):
pass
def download_data(*, settings, download_type=DownloadType.ALL, callback=print):
with FTP(host=settings.host, user=settings.user, passwd=settings.password) as ftp:
ftp.encoding = "utf-8"
@@ -329,17 +333,30 @@ def test_ftp(host, port, user, password, timeout=5):
def test_http(host, port, user, password, timeout=5, use_ssl=False, skip_message=False):
params = urlencode({"text": "Connection test", "type": 2, "timeout": timeout})
params = "statusinfo" if skip_message else "message?{}".format(params)
base_url = "http{}://{}:{}".format("s" if use_ssl else "", host, port)
# authentication
init_auth(user, password, base_url, use_ssl)
try:
params = urlencode({"text": "Connection test", "type": 2, "timeout": timeout})
params = "statusinfo" if skip_message else "message?{}".format(params)
url = "http{}://{}:{}/api/".format("s" if use_ssl else "", host, port)
# authentication
init_auth(user, password, url, use_ssl)
with urlopen("{}{}".format(url, params), timeout=5) as f:
with urlopen("{}/api/{}".format(base_url, params), timeout=5) as f:
return json.loads(f.read().decode("utf-8")).get("message", "")
except HTTPError as e:
if e.code == 404:
return test_api("{}/web/{}".format(base_url, params))
raise TestException(e)
except (RemoteDisconnected, URLError) as e:
raise TestException(e)
def test_api(url):
""" Additional HTTP API compatibility test. """
try:
with urlopen(url, timeout=5) as f:
pass # NOP
except (RemoteDisconnected, URLError, HTTPError) as e:
raise TestException(e)
raise HttpApiException("HTTP API is not supported yet for this receiver!")
def init_auth(user, password, url, use_ssl=False):

View File

@@ -8,7 +8,7 @@ from gi.repository import GLib, Gio
from app.commons import run_idle, log, run_task, run_with_delay, init_logger
from app.connections import HttpAPI, HttpRequestType, download_data, DownloadType, upload_data, test_http, \
TestException
TestException, HttpApiException
from app.eparser import get_blacklist, write_blacklist, parse_m3u
from app.eparser import get_services, get_bouquets, write_bouquets, write_services, Bouquets, Bouquet, Service
from app.eparser.ecommons import CAS, Flag, BouquetService
@@ -836,7 +836,7 @@ class Application(Gtk.Application):
host, port, user, password = opts.host, opts.http_port, opts.http_user, opts.http_password
try:
test_http(host, port, user, password, use_ssl=opts.http_use_ssl, skip_message=True)
except TestException:
except (TestException, HttpApiException):
use_http = False
upload_data(settings=opts,

View File

@@ -3,7 +3,7 @@ from enum import Enum
from pathlib import Path
from app.commons import run_task, run_idle
from app.connections import test_telnet, test_ftp, TestException, test_http
from app.connections import test_telnet, test_ftp, TestException, test_http, HttpApiException
from app.settings import SettingsType, Settings
from app.ui.dialogs import show_dialog, DialogType
from .main_helper import update_entry_data, scroll_to
@@ -271,6 +271,8 @@ class SettingsDialog:
self.show_info_message(test_http(host, port, user, password, use_ssl=use_ssl), Gtk.MessageType.INFO)
except TestException as e:
self.show_info_message(str(e), Gtk.MessageType.ERROR)
except HttpApiException as e:
self.show_info_message(str(e), Gtk.MessageType.WARNING)
finally:
self.show_spinner(False)