source: OpenRLabs-Git/web2py/applications/rlabs/modules/http_requests.py

main
Last change on this file was f03481a, checked in by David Fuertes <dfuertes@…>, 4 years ago

Terminado instalador fullstack

  • Property mode set to 100644
File size: 3.3 KB
Line 
1# -*- coding: utf-8 -*-
2#################################################################################
3# @file    http_requests.py
4# @brief   Module that implement strategy pattern to do http request   
5# @warning None
6# @note Use:
7# @license GNU GPLv3+
8# @author  David Fuertes, EUPT, University of Zaragoza.
9#          Opengnsys Api Rest support provided by Juan Carlos García, EUPT, University of Zaragoza.
10# @version 1.1.0 - First version
11# @date    2019-15-11
12#################################################################################
13import urllib3
14import json
15import requests
16
17requests.packages.urllib3.disable_warnings()
18
19urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
20
21import errors
22
23class UsingPoolManagerConnector:
24    def __init__(self, pool_manager):
25        self.pool_manager = pool_manager
26        self.TIMEOUT = 0.5
27        self.TIMEOUT_POST = 1
28       
29    def do_action(self, action):
30        params = action.get_params()
31       
32        try: 
33            respuesta_json = self.__do_request(params)               
34           
35            return json.loads(respuesta_json.data.decode('utf-8'))
36        except urllib3.exceptions.MaxRetryError:       
37            return {'error': errors.ERROR_CONEXION}       
38   
39    def __do_request(self, params):
40        param_request = [params['action'], params['url']]
41               
42        if params['action'] == 'GET':
43            timeout = self.TIMEOUT
44           
45        if params['action'] == 'POST' or params['action'] == 'DELETE' :
46            timeout = self.TIMEOUT_POST
47       
48         
49        if 'body' in params:           
50            return self.pool_manager.request(*param_request,
51                                             body = json.dumps(params['body']).encode('utf-8'),
52                                             headers = params['headers'],
53                                             timeout = timeout, retries=1, redirect=1)
54        else:
55            return self.pool_manager.request(*param_request, headers = params['headers'],
56                                             timeout = timeout, retries=1, redirect=1)
57       
58class NotUsingPoolManagerConnector:
59    def __init__(self):
60        self.TIMEOUT = 0.05
61        self.TIMEOUT_POST = 1
62       
63    def do_action(self, action):
64
65        params = action.get_params()
66
67        try: 
68            respuesta = self.__do_request(params)
69            return json.loads(respuesta.text)
70        except requests.exceptions.RequestException :       
71            return {'error': errors.ERROR_CONEXION}
72       
73    def __do_request(self, params):
74       
75        if params['action'] == 'GET':
76            timeout = self.TIMEOUT
77        else:
78            timeout = self.TIMEOUT_POST
79           
80        return requests.get(params['url'],
81                                        headers=params['headers'],
82                                        verify=False,
83                                        timeout=timeout,
84                                        allow_redirects=False) 
85           
86       
87
88class HttpRequest:
89    def __init__(self, connector = None):     
90       
91        self.connector = connector
92       
93    def set_connector(self, connector):
94        self.connector = connector
95       
96    def do_action(self, action):
97        return self.connector.do_action(action)
98       
Note: See TracBrowser for help on using the repository browser.