From 0acffe533a1e793158a0ff0450c82e9c026f8872 Mon Sep 17 00:00:00 2001 From: Shane Jaroch Date: Tue, 13 Feb 2024 21:14:15 -0500 Subject: [PATCH] wip bug report --- ntclient/services/api/__init__.py | 39 +++++++++++++++++++++++++++++++ ntclient/services/api/funcs.py | 12 ---------- ntclient/services/bugs.py | 8 ++++--- requirements-lint.txt | 1 + requirements.txt | 1 + 5 files changed, 46 insertions(+), 15 deletions(-) delete mode 100644 ntclient/services/api/funcs.py diff --git a/ntclient/services/api/__init__.py b/ntclient/services/api/__init__.py index 83fe132..c75d952 100644 --- a/ntclient/services/api/__init__.py +++ b/ntclient/services/api/__init__.py @@ -5,3 +5,42 @@ Created on Tue Feb 13 14:28:20 2024 @author: shane """ +import requests + +URL_API = "https://api.nutra.tk" +REQUEST_READ_TIMEOUT = 18 +REQUEST_CONNECT_TIMEOUT = 5 + + +class ApiClient: + """Client for connecting to the remote server/API.""" + + def __init__( + self, + host: str = URL_API, + ): + self.host = host + + def get(self, path: str) -> dict: + """Get data from the API.""" + req = requests.get( + f"{self.host}/{path}", + timeout=(REQUEST_CONNECT_TIMEOUT, REQUEST_READ_TIMEOUT), + ) + req.raise_for_status() + return dict(req.json()) + + def post(self, path: str, data: dict) -> dict: + """Post data to the API.""" + req = requests.post( + f"{self.host}/{path}", + json=data, + timeout=(REQUEST_CONNECT_TIMEOUT, REQUEST_READ_TIMEOUT), + ) + req.raise_for_status() + return dict(req.json()) + + def post_bug(self, bug: tuple) -> None: + """Post a bug report to the developer.""" + print("posting bug report...") + self.post("bug", dict(bug)) diff --git a/ntclient/services/api/funcs.py b/ntclient/services/api/funcs.py deleted file mode 100644 index c955dd0..0000000 --- a/ntclient/services/api/funcs.py +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -""" -Created on Tue Feb 13 14:28:44 2024 - -@author: shane -""" - - -def post_bug(bug: tuple) -> None: - """Post a bug report to the developer.""" - print("posting bug report...") diff --git a/ntclient/services/bugs.py b/ntclient/services/bugs.py index 0b32a11..7f2bd39 100644 --- a/ntclient/services/bugs.py +++ b/ntclient/services/bugs.py @@ -9,7 +9,7 @@ import os import sqlite3 import traceback -import ntclient.services.api.funcs +import ntclient.services.api from ntclient.persistence.sql.nt import sql as sql_nt @@ -46,12 +46,14 @@ INSERT INTO bug def submit() -> int: """Submit bug reports to developer, return n_submitted.""" - n_submitted = 0 sql_bugs = sql_nt("SELECT * FROM bug WHERE submitted = 0") + api_client = ntclient.services.api.ApiClient() + + n_submitted = 0 print(f"submitting {len(sql_bugs)} bug reports...") for bug in sql_bugs: # print(", ".join(str(x) for x in bug)) - ntclient.services.api.funcs.post_bug(bug) + api_client.post_bug(bug) n_submitted += 1 # 1 / 0 # force exception # raise Exception("submitting bug reports failed") diff --git a/requirements-lint.txt b/requirements-lint.txt index cf4d870..71f17b8 100644 --- a/requirements-lint.txt +++ b/requirements-lint.txt @@ -6,5 +6,6 @@ mypy==1.8.0 pylint==3.0.3 types-colorama==0.4.15.12 types-psycopg2==2.9.21.20 +types-requests==2.31.0.20240125 types-setuptools==69.0.0.0 types-tabulate==0.9.0.3 diff --git a/requirements.txt b/requirements.txt index 6a6c215..c2010f5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ argcomplete>=1.8.2,<=1.12.3 colorama>=0.1.17,<=0.4.1 fuzzywuzzy>=0.3.0 +requests>=2.0.0 tabulate>=0.4.3,<=0.8.9 -- 2.52.0