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)