def do_GET(self): log = Logger() # log the received command and the client ip address and port number. log.log_info(f"{self.command} received from {self.client_address}") # parse the url url = urlparse.urlparse(self.path) # check if the client call is correct. if url.path == '/geocode': # send 200 : Ok status. self.send_response(200) self.send_header('Content-type', 'json') self.end_headers() # check if address query string is passed if urlparse.parse_qs(url.query).get('address'): # get the geocodes off the passed address address = urlparse.parse_qs(url.query)['address'][0].replace(" ", "+") self._get_geocode(address) else: # send 400 : Bad Request status and log the error. self.send_response(400) log.log_error("address parameter not passed") else: # send 404 : Not Found status and log the error. self.send_response(404) log.log_error("Unknown service requested.")
def get_geocodes(self, address_to_find): # log the usage of google service. log = Logger() log.log_info(f"Calling {self.geocoding_service_used} service") # create a request object req = request.Request(self.geocoding_api_url + address_to_find) try: # open the URL. with request.urlopen(req) as api_response: # get the HTTP status code from the service self.status = api_response.getcode() if api_response.status == 200: self.status_desc = "Ok" # get the response and load in json format. json_response = json.loads(api_response.read()) # assign the variables with values returned from the api call. self.latitude = json_response["results"][0]["geometry"][ "location"]["lat"] self.longitude = json_response["results"][0]["geometry"][ "location"]["lng"] self.full_address = json_response["results"][0][ "formatted_address"] # log the result. log.log_info( f"Status= {api_response.status}, lat={self.latitude}, lng={self.longitude}" ) # close the request. api_response.close() except Exception as ex: # set the status to 500 - internal server error. self.status_desc = ex self.status = 500 # log the critical error. log.log_critical(str(ex))
def run(host, port): log = Logger() try: # create http server. server = http.server.HTTPServer((host, port), RequestHandler) print(f"Geocoding Proxy Service - v{config.service_version}") print(f"Server Started on port :{port}") # log the start of the server. log.log_info(f"Server Started on port :{str(port)}") # listen for ever. server.serve_forever() except Exception as ex: # in case of exception, log the incident as critical. msg = f"Server Start error - {str(ex)}" log.log_critical(msg) exit(1)
def _get_geocode(self, param): log = Logger() # log the address received log.log_info(f"search for {param}") # get the geocodes using here service. service = Here() service.get_geocodes(param) # if the returned status is nnot 200: ok, call google service. if service.status != 200: service = Google() service.get_geocodes(param) # build meta data to attach to the result. meta = {'status': service.status, 'status_desc': str(service.status_desc), 'service_used': service.geocoding_service_used, 'requested_address': param, 'timestamp': service.timestamp} # compose the final results. result = {'lat': service.latitude, 'lng': service.longitude, 'meta': meta} # write the results is json format and in the encoding specified in the config file self.wfile.write(json.dumps(result).encode(config.reponse_encoding))