Module libbottles.utils.request
Expand source code
import urllib.request, json
from libbottles.utils import connection
from libbottles.exceptions import NoConnection
class Request:
_headers = {}
def __init__(self, headers: dict = None):
self._headers["User-Agent"] = "libbottles client (usebottles.com)"
if headers is not None:
self._envs = {**self._headers, **headers}
if not connection.check():
raise NoConnection()
def get(self, url: str):
req = urllib.request.Request(
url,
data=None,
headers=self._headers
)
with urllib.request.urlopen(req) as url:
data = json.loads(url.read().decode())
return data
Classes
class Request (headers: dict = None)
-
Expand source code
class Request: _headers = {} def __init__(self, headers: dict = None): self._headers["User-Agent"] = "libbottles client (usebottles.com)" if headers is not None: self._envs = {**self._headers, **headers} if not connection.check(): raise NoConnection() def get(self, url: str): req = urllib.request.Request( url, data=None, headers=self._headers ) with urllib.request.urlopen(req) as url: data = json.loads(url.read().decode()) return data
Methods
def get(self, url: str)
-
Expand source code
def get(self, url: str): req = urllib.request.Request( url, data=None, headers=self._headers ) with urllib.request.urlopen(req) as url: data = json.loads(url.read().decode()) return data