示例#1
0
文件: views.py 项目: ox-it/moxie
 def dispatch_request(self, *args, **kwargs):
     """Finds the best_match service_response from those registered
     If no good service_response can be found we generally want to
     return a NotAcceptable 406.
     """
     if request.method == 'OPTIONS':
         return self._handle_options()
     best_match = request.accept_mimetypes.best_match(
             self.service_responses.keys())
     if best_match:
         service_response = self.service_responses[best_match]
         try:
             with statsd.timer('{module}.{view}'.format(module=self.__module__, view=self.__class__.__name__)):
                 response = self.handle_request(*args, **kwargs)
                 response = make_response(service_response(self, response))
                 response = self._expires_headers(response)
         except ApplicationException as ae:
             response = exception_handler(ae)
         except:
             if current_app.debug:
                 raise
             response = exception_handler(ApplicationException())
         response = self._cors_headers(response)
         return response
     else:
         return self.default_response()
示例#2
0
文件: sbb.py 项目: FHNW/mofa-places
 def invoke(self, doc, rti_type):
     for ident in doc.identifiers:
         if ident.startswith('sbb'):
             _, sbb_code = ident.split(':')
             with statsd.timer('transport.providers.sbb.rti'):
                 if rti_type == 'rail-departures':
                     services, messages = self.get_departure_board(sbb_code)
                 elif rti_type == 'rail-arrivals':
                     services, messages = self.get_arrival_board(sbb_code)
             title = self.provides.get(rti_type)
             return services, messages, rti_type, title
示例#3
0
 def invoke(self, doc, rti_type):
     for ident in doc.identifiers:
         if ident.startswith('sbb'):
             _, sbb_code = ident.split(':')
             with statsd.timer('transport.providers.sbb.rti'):
                 if rti_type == 'rail-departures':
                     services, messages = self.get_departure_board(sbb_code)
                 elif rti_type == 'rail-arrivals':
                     services, messages = self.get_arrival_board(sbb_code)
             title = self.provides.get(rti_type)
             return services, messages, rti_type, title
示例#4
0
文件: ldb.py 项目: ox-it/moxie
 def invoke(self, doc, rti_type, place_ident=None):
     for ident in doc.identifiers:
         if ident.startswith("crs"):
             _, crs_code = ident.split(":")
             with statsd.timer("transport.providers.ldb.rti"):
                 if rti_type == "rail-departures":
                     services, messages = self.get_departure_board(crs_code)
                 elif rti_type == "rail-arrivals":
                     services, messages = self.get_arrival_board(crs_code)
             title = self.provides.get(rti_type)
             return services, messages, rti_type, title
示例#5
0
文件: cloudamber.py 项目: ox-it/moxie
 def get_rti(self, naptan_code):
     """
     Get a dict containing RTI
     @param naptan_code: SMS code to search for
     @return: dictionary of services
     """
     try:
         with statsd.timer('transport.providers.cloudamber.rti_request'):
             response = requests.get(self.get_url(naptan_code),
                                     timeout=self.timeout)
             response.raise_for_status()
     except RequestException:
         logger.warning('Error in request to Cloudamber', exc_info=True,
                      extra={
                          'data': {
                              'url': self.url,
                              'naptan': naptan_code}
                      })
         raise ServiceUnavailable()
     else:
         with statsd.timer('transport.providers.cloudamber.parse_html'):
             return self.parse_html(response.text)
示例#6
0
文件: solr.py 项目: ox-it/moxie
 def connection(self, method, params=None, data=None, headers=None, timeout=None):
     """Does a GET request if there is no data otherwise a POST
     :param params: URL parameters as a dict
     :param data: POST form
     :param headers: custom headers to pass to Solr as a dict
     :param timeout: custom timeout
     """
     headers = headers or dict()
     timeout = timeout or self.DEFAULT_TIMEOUT
     params = params or dict()
     params['wt'] = self.return_type
     url = '{0}{1}/{2}'.format(self.server_url, self.core, method)
     logger.debug(data)
     try:
         with statsd.timer('core.search.solr.request'):
             if data:
                 response = requests.post(url, data, headers=headers,
                                          params=params, timeout=timeout)
             else:
                 response = requests.get(url, headers=headers,
                                         params=params, timeout=timeout)
     except RequestException as re:
         logger.error('Error in request to Solr', exc_info=True,
                      extra={
                          'data': {'url': url,
                                   'params': params,
                                   'headers': headers}})
         # has to cast to str as sometimes the message is not a string..
         raise SearchServerException(str(re.message))
     else:
         if response.ok:
             return response
         else:
             try:
                 json = response.json()
             except:
                 json = None
             message = "Search server (Solr) exception"
             if json and 'error' in json and 'msg' in json['error']:
                 solr_message = json['error']['msg']
             else:
                 solr_message = message
             logger.error(message, extra={
                 'data': {
                     'url': url,
                     'solr_message': solr_message,
                     'solr_status_code': response.status_code,
                     'params': params,
                     'headers': headers}})
             raise SearchServerException(solr_message, status_code=response.status_code)