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

49 lines
1.2 KiB
Python
Raw Normal View History

2018-02-11 01:38:38 +00:00
#!/usr/bin/env python3
2018-02-08 00:15:58 +00:00
# -*- coding: utf-8 -*-
2018-02-11 01:38:38 +00:00
"""Base HTTP requests."""
2018-02-08 00:15:58 +00:00
from collections import OrderedDict
2018-02-11 01:38:38 +00:00
import requests
2018-02-08 00:15:58 +00:00
class TimeoutException(Exception):
2018-02-11 01:38:38 +00:00
"""Ignore timeouts."""
2018-02-08 00:15:58 +00:00
pass
2018-02-11 01:38:38 +00:00
2018-02-08 00:15:58 +00:00
class HttpRequest:
"""Provides all generic features for making HTTP GET requests"""
2018-02-11 01:38:38 +00:00
2018-02-08 00:15:58 +00:00
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)
2018-02-11 01:38:38 +00:00
except requests.exceptions.Timeout as exc:
raise TimeoutException(exc)
2018-02-08 00:15:58 +00:00
return req
2018-02-11 01:38:38 +00:00
2018-02-08 00:15:58 +00:00
class HttpPostRequest(HttpRequest):
"""Provides all generic features for making HTTP POST requests"""
2018-02-11 01:38:38 +00:00
2018-02-08 00:15:58 +00:00
def run(self):
"""Run query."""
try:
req = self.sess.post(self.url, data=self.params, timeout=self.timeout)
2018-02-11 01:38:38 +00:00
except requests.exceptions.Timeout as exc:
raise TimeoutException(exc)
2018-02-08 00:15:58 +00:00
return req