misc minor code cosmetic : no change

This commit is contained in:
Emmanuel Viennet 2023-03-09 14:24:12 +01:00
parent d97f04a8ad
commit f628478b14
4 changed files with 23 additions and 16 deletions

View File

@ -18,6 +18,8 @@ def get_token():
@token_auth.login_required @token_auth.login_required
def revoke_token(): def revoke_token():
"révoque le jeton de l'utilisateur courant" "révoque le jeton de l'utilisateur courant"
token_auth.current_user().revoke_token() user = token_auth.current_user()
user.revoke_token()
db.session.commit() db.session.commit()
log(f"API: revoking token for {user}")
return "", 204 return "", 204

View File

@ -307,7 +307,7 @@ class User(UserMixin, db.Model):
@staticmethod @staticmethod
def check_token(token): def check_token(token):
"""Retreive user for given token, chek token's validity """Retreive user for given token, check token's validity
and returns the user object. and returns the user object.
""" """
user = User.query.filter_by(token=token).first() user = User.query.filter_by(token=token).first()

View File

@ -28,6 +28,7 @@ avec la config du client API:
""" """
from pprint import pprint as pp from pprint import pprint as pp
import requests
import sys import sys
import urllib3 import urllib3
from setup_test_api import ( from setup_test_api import (
@ -43,6 +44,11 @@ from setup_test_api import (
) )
def logout_api_user():
r = requests.delete(API_URL + "/tokens", headers=HEADERS, verify=CHECK_CERTIFICATE)
assert r.status_code == 204
if not CHECK_CERTIFICATE: if not CHECK_CERTIFICATE:
urllib3.disable_warnings() urllib3.disable_warnings()

View File

@ -66,33 +66,32 @@ def api_admin_headers() -> dict:
def GET(path: str, headers: dict = None, errmsg=None, dept=None): def GET(path: str, headers: dict = None, errmsg=None, dept=None):
"""Get and returns as JSON """Get and returns as JSON
Special case for non json result (image or pdf): return Content-Disposition string (inline or attachment) Special case for non json result (image or pdf):
return Content-Disposition string (inline or attachment)
""" """
if dept: if dept:
url = SCODOC_URL + f"/ScoDoc/{dept}/api" + path url = SCODOC_URL + f"/ScoDoc/{dept}/api" + path
else: else:
url = API_URL + path url = API_URL + path
r = requests.get(url, headers=headers or {}, verify=CHECK_CERTIFICATE) reply = requests.get(url, headers=headers or {}, verify=CHECK_CERTIFICATE)
if r.status_code != 200: if reply.status_code != 200:
raise APIError(errmsg or f"""erreur status={r.status_code} !""", r.json()) raise APIError(
errmsg or f"""erreur status={reply.status_code} !""", reply.json()
)
if r.headers.get("Content-Type", None) == "application/json": if reply.headers.get("Content-Type", None) == "application/json":
return r.json() # decode la reponse JSON return reply.json() # decode la reponse JSON
elif r.headers.get("Content-Type", None) in [ elif reply.headers.get("Content-Type", None) in [
"image/jpg", "image/jpg",
"image/png", "image/png",
"application/pdf", "application/pdf",
]: ]:
retval = { retval = {
"Content-Type": r.headers.get("Content-Type", None), "Content-Type": reply.headers.get("Content-Type", None),
"Content-Disposition": r.headers.get("Content-Disposition", None), "Content-Disposition": reply.headers.get("Content-Disposition", None),
} }
return retval return retval
else: raise APIError("Unknown returned content {r.headers.get('Content-Type', None} !\n")
raise APIError(
"Unknown returned content {r.headers.get('Content-Type', None} !\n"
)
return r.json() # decode la reponse JSON
def POST_JSON(path: str, data: dict = {}, headers: dict = None, errmsg=None, dept=None): def POST_JSON(path: str, data: dict = {}, headers: dict = None, errmsg=None, dept=None):