Repository navigation
Expand file tree
/
Copy pathjitsi.py
More file actions
75 lines (60 loc) · 2.34 KB
/
Copy pathjitsi.py
File metadata and controls
75 lines (60 loc) · 2.34 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
70
71
72
73
74
75
#! /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/lib/blob/main/CONTRIBUTING.md
"""This library collects some Jitsi related functions that are
needed by more than one Jitsi consumer."""
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2026101201'
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.
- `PROXY` (`str`, optional): Proxy to reach the target through, overriding the
proxy the environment names.
_type : str, optional
Either `'json'` for JSON parsing or anything else for raw fetch. Defaults to `'json'`.
Returns
-------
tuple (bool, dict or str)
- **success** (`bool`): Whether the request succeeded.
- **result** (`dict` or `str`): Parsed JSON or raw response, or the error message.
Examples
--------
>>> 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,
proxy=getattr(args, 'PROXY', None),
timeout=args.TIMEOUT,
)
if not success:
return (False, result)
return (True, result)