1
0
mirror of https://github.com/mbirth/tcl_ota_check.git synced 2024-11-14 00:06:46 +00:00
tcl_ota_check/tcllib/requests/http.py

39 lines
1.1 KiB
Python
Raw Normal View History

2018-02-08 00:15:58 +00:00
# -*- coding: utf-8 -*-
import requests
from collections import OrderedDict
class TimeoutException(Exception):
pass
class HttpRequest:
"""Provides all generic features for making HTTP GET requests"""
def __init__(self, url, timeout=10):
self.url = url
self.params = OrderedDict()
self.timeout = timeout
self.headers = {}
def reset_session(self):
"""Reset everything to default."""
self.sess = requests.Session()
self.sess.headers.update(self.headers)
def run(self):
"""Run query."""
try:
req = self.sess.get(self.url, params=self.params, timeout=self.timeout)
except requests.exceptions.Timeout as e:
raise TimeoutException(e)
return req
class HttpPostRequest(HttpRequest):
"""Provides all generic features for making HTTP POST requests"""
def run(self):
"""Run query."""
try:
req = self.sess.post(self.url, data=self.params, timeout=self.timeout)
except requests.exceptions.Timeout as e:
raise TimeoutException(e)
return req