forked from XiaoMi/ha_xiaomi_home
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
44 lines (34 loc) · 1.25 KB
/
common.py
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
# -*- coding: utf-8 -*-
"""Common functions."""
import json
import yaml
from urllib.parse import urlencode
from urllib.request import Request, urlopen
def load_yaml_file(yaml_file: str) -> dict:
with open(yaml_file, 'r', encoding='utf-8') as file:
return yaml.safe_load(file)
def save_yaml_file(yaml_file: str, data: dict) -> None:
with open(yaml_file, 'w', encoding='utf-8') as file:
yaml.safe_dump(
data=data, stream=file, allow_unicode=True)
def load_json_file(json_file: str) -> dict:
with open(json_file, 'r', encoding='utf-8') as file:
return json.load(file)
def save_json_file(json_file: str, data: dict) -> None:
with open(json_file, 'w', encoding='utf-8') as file:
json.dump(data, file, ensure_ascii=False, indent=4)
def http_get(
url: str, params: dict = None, headers: dict = None
) -> dict:
if params:
encoded_params = urlencode(params)
full_url = f'{url}?{encoded_params}'
else:
full_url = url
request = Request(full_url, method='GET', headers=headers or {})
content: bytes = None
with urlopen(request) as response:
content = response.read()
return (
json.loads(str(content, 'utf-8'))
if content is not None else None)