-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest.py
More file actions
95 lines (76 loc) · 3.08 KB
/
request.py
File metadata and controls
95 lines (76 loc) · 3.08 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# coding=utf-8
'''
Este módulo se encarga de gestionar las requests a la API de Shokesu
'''
import requests, json
from re import match, search, DOTALL
from logger import Logger
try:
from urllib.parse import urlparse, urlencode
except ImportError:
from urlparse import urlparse
from urllib import urlencode
# URI raíz de la API de Shokesu
# api_uri_root = 'https://api.shokesu.com'
api_uri_root = 'https://app.shokesu.com:8443/shokesu'
def request(method, path, params = {}, access_token = None, payload = None):
'''
Envia una request HTTP a la API de Shokesu.
:param method: Es el método a utilizar. Deberá ser uno de los siguientes
GET, PUT, POST, DELETE
:param path Es la ruta del recurso a obtener, relativa a https://api.shokesu.com
:param params Es un diccionario que indicará los parámetros a añadir a la URL.
:param access_token Es un token JWT que se usará para autenticar al cliente. (Si es None,
no se añadira a la cabecera)
:param payload Parámetros a añadir al cuerpo de la request. Por defecto es None. Si no es
None, deberá ser un objeto convertible a JSON. El objeto se codificará en dicho formato y
se incluirá en el payload.
:return:
'''
params = dict([(key, value) for key, value in params.items() if not value is None])
# Construimos la url
result = match('^\/?(.*)$', path)
path = result.group(1)
url = '{}/{}?{}'.format(api_uri_root, path, urlencode(params))
send_request = {
'GET' : requests.get,
'POST' : requests.post,
'PUT' : requests.put,
'DELETE' : requests.delete
}
logger = Logger()
if payload is None and method == 'POST':
payload = {}
payload = json.dumps(payload) if not payload is None else None
headers = {}
if not access_token is None:
headers['Authorization'] = 'Bearer {}'.format(access_token)
logger.debug('-------------\n')
logger.debug('Sending {} request to {}'.format(method, url))
logger.debug('Headers: ')
logger.debug('\n'.join(['{}: {}'.format(key, value) for key, value in headers.items()]))
logger.debug('Payload: ')
logger.debug(payload)
response = send_request[method](url = url, data = payload, headers = headers)
logger.debug('Response status code: {}'.format(response.status_code))
try:
if response.status_code == 404:
raise Exception('404 Error Not Found')
if response.status_code != 200:
try:
description = search('<b>description<\/b>[^<]*<u>([^<]+)<\/u>', response.text, DOTALL).group(1)
except:
description = 'Unknown server error'
raise Exception(description)
except Exception as e:
raise Exception('Error executing request to {}: {}'.format(response.url, str(e)))
logger.debug('Response body: ')
logger.debug(response.text)
logger.debug('-------------\n')
return response
'''
try:
return response.json()
except:
raise Exception('Error parsing request response from {} to JSON'.format(response.url))
'''