def _redirect_to_full_url(self): id = self._validate_int('id') is_lang_set = 'lang' in self.request.matchdict lang = self._validate_lang() if is_lang_set \ else self._get_interface_lang() url = '%s/%d/%s/info' % (self._API_ROUTE, id, lang) if log.isEnabledFor(logging.DEBUG): log.debug('API: %s %s', 'GET', url) resp, data = call_api(self.settings, url) if resp.status_code == 404: raise HTTPNotFound() elif resp.status_code == 400: raise HTTPBadRequest("Incorrect document id or lang") elif resp.status_code != 200: raise HTTPInternalServerError( "An error occurred while loading the document") if 'redirects_to' in data: if lang not in data['available_langs']: lang = self._get_best_lang(data['available_langs']) self._redirect(data['redirects_to'], lang, None, is_lang_set) else: locale = data['locales'][0] slug = get_slug(locale, is_route=self._API_ROUTE == 'routes') self._redirect(data['document_id'], locale['lang'], slug, is_lang_set)
def data(self): id, lang = self._validate_id_lang() headers = None if 'Authorization' in self.request.headers: headers = { 'Authorization': self.request.headers.get('Authorization') } url = '%s/%d?l=%s' % (self._API_ROUTE, id, lang) resp, data = call_api(self.settings, url, headers) if resp.status_code != 200: raise HTTPInternalServerError( "An error occurred while loading the document") if data.get('not_authorized', False): self.template_input.update({'not_authorized': True}) else: locales = data['locales'] self.template_input.update({ 'lang': locales[0]['lang'], 'profile': data, 'locale': locales[0], 'geometry': self._get_geometry(data['geometry']['geom']) if data['geometry'] else None }) return self.template_input
def data(self): id, lang = self._validate_id_lang() headers = None if 'Authorization' in self.request.headers: headers = { 'Authorization': self.request.headers.get('Authorization') } url = '%s/%d?l=%s' % (self._API_ROUTE, id, lang) resp, data = call_api(self.settings, url, headers) if resp.status_code != 200: raise HTTPInternalServerError( "An error occurred while loading the document") if data.get('not_authorized', False): self.template_input.update({ 'not_authorized': True }) else: locales = data['locales'] self.template_input.update({ 'lang': locales[0]['lang'], 'profile': data, 'locale': locales[0], 'geometry': self._get_geometry(data['geometry']['geom']) }) return self.template_input
def _test_api_call(self): resp, content = call_api(self.view.settings, self._prefix) self.assertEqual(resp.status_code, 200) self.assertTrue('total' in content) self.assertTrue('documents' in content) total = content['total'] documents = content['documents'] self.assertEqual(isinstance(total, int), True) self.assertEqual(isinstance(documents, list), True)
def _add_api_status(self, status): api_status = None success = False try: resp, api_status = call_api( self.request.registry.settings, 'health') if resp.status_code == 200: success = True except: log.exception('Getting api status failed') if not success: self.request.response.status_code = 500 status['api'] = 'ok' if success else 'error' status['api_status'] = api_status
def _add_api_status(self, status): api_status = None success = False try: resp, api_status = call_api(self.request.registry.settings, 'health') if resp.status_code == 200: success = True except: log.exception('Getting api status failed') if not success: self.request.response.status_code = 500 status['api'] = 'ok' if success else 'error' status['api_status'] = api_status
def _get_documents(self): params = [] lang = self._get_interface_lang() params.append(('pl', lang)) # Inject default list filters params: filters = dict(self._DEFAULT_FILTERS, **{k: v for k, v in params}) # query_string contains filter params using the standard URL format # (eg. ?offset=50&limit=20&elevation=>2000). query_string = '?' + urlencode(params) if params else '' url = '%s%s' % (self._API_ROUTE, query_string) resp, content = call_api(self.settings, url) if resp.status_code == 200: documents = content['documents'] total = content['total'] else: raise HTTPInternalServerError( "An error occurred while loading the results") return documents, total, filters, lang