factorise http_cli
-import json
import functools
import requests
@@ -6,7 +5,6 @@ import requests
from .config import requests_get_params
-
def parse_response(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
@@ -28,24 +26,22 @@ def parse_response(f):
@parse_response
-def post(config, url, data, **kwargs):
+def _req(method, config, url, **kwargs):
all_kwargs, headers = requests_get_params(config)
all_kwargs.update(kwargs)
+ fn = getattr(requests, method)
+
try:
- r = requests.post(url, data=data, headers=headers, **all_kwargs)
+ r = fn(url, headers=headers, **all_kwargs)
except (requests.exceptions.Timeout, requests.exceptions.ConnectionError):
raise
return r
-@parse_response
-def get(config, url, **kwargs):
- all_kwargs, headers = requests_get_params(config)
- all_kwargs.update(kwargs)
+def post(config, url, data, **kwargs):
+ return _req('POST', config, url, data=data, **kwargs)
- try:
- r = requests.get(url, headers=headers, **all_kwargs)
- except (requests.exceptions.Timeout, requests.exceptions.ConnectionError):
- raise
- return r
+
+def get(config, url, **kwargs):
+ return _req('GET', config, url, **kwargs)
