RAPP Platform  v0.6.0
RAPP Platform is a collection of ROS nodes and back-end processes that aim to deliver ready-to-use generic services to robots
 All Classes Namespaces Files Functions Variables Macros
rapp_http_request_handler.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- encode: utf-8 -*-
3 
4 #Copyright 2015 RAPP
5 
6 #Licensed under the Apache License, Version 2.0 (the "License");
7 #you may not use this file except in compliance with the License.
8 #You may obtain a copy of the License at
9 
10  #http://www.apache.org/licenses/LICENSE-2.0
11 
12 #Unless required by applicable law or agreed to in writing, software
13 #distributed under the License is distributed on an "AS IS" BASIS,
14 #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #See the License for the specific language governing permissions and
16 #limitations under the License.
17 
18 # Authors: Aris Thallas
19 # contact: aris.thallas@{iti.gr, gmail.com}
20 
21 
22 import requests
23 
24 from rapp_utilities import RappUtilities
25 from rapp_exceptions import RappError
26 
27 
28 ## @class RappHttpRequestHandler
29 # @brief Performs http requests
30 class RappHttpRequestHandler(object):
31 
32  def __init__(self):
33  ## The server url
34  self._url = ''
35  ## The request parameters
36  self._params = {}
37  ## The request additional headers
38  self._headers = {}
39 
40  ## The required response format
41  self._response_format = 'json'
42 
43  ## Seconds to wait for any server response
44  self._server_timeout = 1.0
45  ## The value of valid response status code
46  self._accepted_status = 200
47 
48  ## @brief Execute the request and verify response
49  #
50  # @param url_add [string] The server url
51  # @param params_dict [dict] The request's parameters
52  # @param headers_dict [dict] The request's additional headers dictionary
53  #
54  # @return response [request module response] The servers response
55  #
56  # @exceptions RappError Request error
57  def perform_request(self,
58  url_add=None,
59  params_dict=None,
60  headers_dict=None):
61 
62  # Handle arguments
63  url = self._url
64  if url_add is not None:
65  url = url_add
66  param = self._params.copy()
67  if params_dict is not None:
68  param.update(params_dict)
69  head = self._headers.copy()
70  if headers_dict is not None:
71  head.update(headers_dict)
72 
73  try:
74  response = requests.get(url, params=param,
75  headers=head, timeout=self._server_timeout)
76  except requests.RequestException as err:
77  RappUtilities.rapp_print(err, 'ERROR')
78  raise RappError(err)
79  RappUtilities.rapp_print('URL: ' + response.url, 'DEBUG')
80 
81  if (isinstance(self._accepted_status, int) and
82  response.status_code == self._accepted_status) or \
83  (isinstance(self._accepted_status, list) and
84  response.status_code in self._accepted_status):
85  return self._modify_response(response)
86 
87  error = 'Request error. Status code: ' + str(response.status_code)
88  RappUtilities.rapp_print(error, 'ERROR')
89  raise RappError(error)
90 
91  ## @brief Return the proper response format
92  #
93  # @param response [request module response] The servers response
94  # @param form [string] The required response format
95  #
96  # @exceptions RappError Format transformation error
97  def _modify_response(self, response, form=None):
98  if form is None and self._response_format == 'json' or form == 'json':
99  try:
100  return response.json()
101  except ValueError as err:
102  RappUtilities.rapp_print(err, 'ERROR')
103  raise RappError(err)
104  else:
105  return response.text()
_server_timeout
Seconds to wait for any server response.
def perform_request
Execute the request and verify response.
def _modify_response
Return the proper response format.
_accepted_status
The value of valid response status code.