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
ip_api_locator.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 
22  GeolocatorBase,
23  RappUtilities,
24  RappError
25  )
26 
27 
28 ## @class IpAPILocator
29 # @brief IP-API locator
31 
32  ## @brief Constructor
33  def __init__(self):
34  GeolocatorBase.__init__(self)
35  self._url = 'http://ip-api.com/json/'
36 
37  ## @brief Fetch the location
38  #
39  # @param req
40  # [rapp_platform_ros_communications::Geolocator::GeolocatorSrv]
41  # The service request
42  #
43  # @return [dict] The server results
44  def fetch_geolocation(self, req):
45 
46  if req.ip == '':
47  err = 'No IP provided'
48  raise RappError(err)
49 
50  try:
51  response = self._http_request.perform_request(self._url + req.ip)
52  except RappError as err:
53  RappUtilities.rapp_print(err, 'ERROR')
54  raise RappError(err)
55 
56  if response['status'] != 'success':
57  err = 'Http request failed: ' + response['message']
58  RappUtilities.rapp_print(err, 'ERROR')
59  raise RappError(err)
60 
61  # Process servers results. Extract titles etc and add to previous
62  # results
63  try:
64  return self._handle_server_response(response)
65  except RappError as err:
66  RappUtilities.rapp_print(err, 'ERROR')
67  raise RappError(err)
68 
69  ## @brief Handles the server's response
70  #
71  # @param response [] The server's response to the request module.
72  #
73  # @return values [dict] The final values
74  def _handle_server_response(self, response):
75 
76  # {'server_response_name':'ros_service_name'}
77  keys = {'city': 'city',
78  'country': 'country',
79  'countryCode': 'countryCode',
80  'lat': 'latitude',
81  'lon': 'longtitude',
82  'regionName': 'regionName',
83  'timezone': 'timezone',
84  'zip': 'zip'}
85  # }
86 
87  return self.rapp_http_json_parser.find_values(keys, response)
def _handle_server_response
Handles the server's response.