-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathjitsi.py
More file actions
69 lines (55 loc) · 2.17 KB
/
jitsi.py
File metadata and controls
69 lines (55 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#! /usr/bin/env python3
# -*- coding: utf-8; py-indent-offset: 4 -*-
#
# Author: Linuxfabrik GmbH, Zurich, Switzerland
# Contact: info (at) linuxfabrik (dot) ch
# https://www.linuxfabrik.ch/
# License: The Unlicense, see LICENSE file.
# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.rst
"""This library collects some Jitsi related functions that are
needed by more than one Jitsi plugin."""
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2025042001'
import base64 # pylint: disable=C0413
from . import url
def get_data(args, _type='json'):
"""
Fetch data from a URL, with optional authentication and content type handling.
This function calls the provided URL and returns the data in either JSON format (default) or
raw format, based on the specified type.
### Parameters
- **args** (`object`):
An object containing:
- `URL` (`str`): URL to fetch.
- `USERNAME` (`str`): Username for Basic Auth (optional).
- `PASSWORD` (`str`): Password for Basic Auth (optional).
- `TIMEOUT` (`int`): Request timeout in seconds.
- `INSECURE` (`bool`): Disable SSL verification.
- `NO_PROXY` (`bool`): Ignore proxy settings.
- **_type** (`str`, optional):
Either `'json'` for JSON parsing or anything else for raw fetch. Defaults to `'json'`.
### Returns
- **tuple**:
- **success** (`bool`): Whether the request succeeded.
- **result** (`dict` or `str`): Parsed JSON or raw response.
- **False** (`bool`): False if fetch failed.
### Example
>>> success, result = get_data(args, _type='json')
>>> print(result)
"""
headers = {}
if args.USERNAME:
auth = f'{args.USERNAME}:{args.PASSWORD}'
headers['Authorization'] = f'Basic {base64.b64encode(auth.encode()).decode()}'
fetch_func = url.fetch_json if _type == 'json' else url.fetch
success, result = fetch_func(
args.URL,
extended=True,
header=headers,
insecure=args.INSECURE,
no_proxy=args.NO_PROXY,
timeout=args.TIMEOUT,
)
if not success:
return (success, result, False)
return (True, result)