def __str__(self): result = '{} ({})'.format(self.message, self.code) if self.errors: result += '\n\nErrors:\n' result += utils.json_dumps(self.errors, indent=4, separators=(',', ': ')) if self.extra: result += '\n\nExtra:\n' result += utils.json_dumps(self.extra, indent=4, separators=(',', ': ')) return result
def es(self, url, query={}, body={}): http_client = AsyncHTTPClient() if not url.startswith('/'): url = '/' + url for arg in query: if not isinstance(query[arg], list): query[arg] = [query[arg]] try: response = yield http_client.fetch( 'http://{}{}?{}'.format( config['api']['elasticsearch'], url, utils.url_encode_tornado_arguments(query) \ if query else '', ), method='POST' if body else 'GET', body=utils.json_dumps(body) if body else None, ) return utils.json_loads(response.body) except HTTPError as e: try: extra = utils.json_loads(e.response.body) except: extra = {'error': e.response.body.decode('utf-8')} raise exceptions.Elasticsearch_exception( e.code, extra, )
def write_object(self, obj): if isinstance(obj, Pagination): self.write_pagination(obj) return self.write( utils.json_dumps(obj, indent=4, sort_keys=True), )
def to_elasticsearch(self): database.es.index( index='images', doc_type='image', id=self.id, body=utils.json_dumps(self.__dict__), )
async def es_get(self, url, query={}, body={}): http_client = AsyncHTTPClient() if not url.startswith('/'): url = '/' + url for arg in query: if not isinstance(query[arg], list): query[arg] = [query[arg]] try: response = await http_client.fetch( 'http://{}{}?{}'.format( config['api']['elasticsearch'], url, utils.url_encode_tornado_arguments(query) \ if query else '', connect_timeout=2.0, request_timeout=2.0, ), method='POST' if body else 'GET', headers={ 'Content-Type': 'application/json', }, body=utils.json_dumps(body) if body else None, ) return utils.json_loads(response.body) except HTTPError as e: try: extra = utils.json_loads(e.response.body) if e.code == 404: return extra except: extra = {'error': e.response.body.decode('utf-8')} raise exceptions.Elasticsearch_exception( e.code, extra, )
def es(self, url, query={}, body={}): http_client = AsyncHTTPClient() if not url.startswith('/'): url = '/'+url for arg in query: if not isinstance(query[arg], list): query[arg] = [query[arg]] try: response = yield http_client.fetch( 'http://{}{}?{}'.format( config['api']['elasticsearch'], url, utils.url_encode_tornado_arguments(query) \ if query else '', ), method='POST' if body else 'GET', body=utils.json_dumps(body) if body else None, ) return utils.json_loads(response.body) except HTTPError as e: try: extra = utils.json_loads(e.response.body) except: extra = {'error': e.response.body.decode('utf-8')} raise exceptions.Elasticsearch_exception( e.code, extra, )
def get(self): client = Async_client(config['client']['api_url'], version='1') set_default_headers(self) q = self.get_argument('q') shows = yield client.get('/shows', { 'q': q, 'fields': 'title,poster_image' }) self.write(utils.json_dumps(shows))
def to_elasticsearch(self, show_id): self.show_id = int(show_id) database.es.index( index='episodes', doc_type='episode', id='{}-{}'.format(show_id, self.number), body=utils.json_dumps(self), ) self.__dict__.pop('show_id')
def to_elasticsearch(self): '''Sends the episodes's info to ES. This method is automatically called after update or insert. ''' self.session.es_bulk.append({ '_index': 'episodes', '_id': '{}-{}'.format(self.show_id, self.number), '_source': utils.json_dumps(self.serialize()), })
def _fetch(self, url, method, data=None, headers=None): if data is not None: if isinstance(data, dict) or isinstance(data, list): data = utils.json_dumps(data) if self.access_token: if headers == None: headers = {} if 'Authorization' not in headers: headers['Authorization'] = 'Bearer {}'.format( self.access_token) return self.fetch(url, headers=headers, method=method, body=data)
def to_elasticsearch(self): ''' :returns: boolean ''' database.es.index( index='shows', doc_type='show', id=self.id, body=utils.json_dumps(self), )
def to_elasticsearch(self): '''Sends the episodes's info to ES. This method is automatically called after update or insert. ''' self.session.es_bulk.append({ '_index': 'episodes', '_type': 'episode', '_id': '{}-{}'.format(self.show_id, self.number), '_source': utils.json_dumps(self.serialize()), })
def to_elasticsearch(self): """Sends the show's info to ES. This method is automatically called after update or insert. """ if not self.id: raise Exception('can\'t add the show to ES without an ID.') self.session.es_bulk.append({ '_index': 'shows', '_id': self.id, '_source': utils.json_dumps(self.serialize()), })
def rebuild_shows(self, session, pipe): from seplis.api.base.show import Show to_es = [] for item in session.query(models.Show).yield_per(10000): a = Show._format_from_row(item) to_es.append({ '_index': 'shows', '_type': 'show', '_id': a.id, '_source': utils.json_dumps(a), }) helpers.bulk(database.es, to_es)
def rebuild_episodes(self, session): from seplis.api.base.episode import Episode to_es = [] for item in session.query(models.Episode).yield_per(10000): a = Episode._format_from_row(item) a.show_id = item.show_id to_es.append({ '_index': 'episodes', '_type': 'episode', '_id': '{}-{}'.format(item.show_id, a.number), '_source': utils.json_dumps(a), }) helpers.bulk(database.es, to_es)
def to_elasticsearch(self): '''Sends the image's info to ES. This method is automatically called after update and insert. ''' if not self.id: raise Exception('can\'t add the show to ES without an ID.') session = orm.Session.object_session(self) session.es_bulk.append({ '_index': 'images', '_id': self.id, '_source': utils.json_dumps(self.serialize()), })
def to_elasticsearch(self): """Sends the show's info to ES. This method is automatically called after update or insert. """ if not self.id: raise Exception('can\'t add the show to ES without an ID.') session = orm.Session.object_session(self) session.es_bulk.append({ '_index': 'shows', '_type': 'show', '_id': self.id, '_source': utils.json_dumps(self.serialize()), })
def _fetch(self, url, method, data=None, headers=None): full_url = url if 'http://' not in url: full_url = self.get_url(url) if data is not None: if isinstance(data, dict): data = json_dumps(data) if self.access_token: if headers == None: headers = {} if 'Authorization' not in headers: headers['Authorization'] = 'Bearer {}'.format(self.access_token) request = HTTPRequest(full_url, headers=headers, method=method, body=data) self.http_client.fetch(request, self.stop) return self.wait()
async def get(self): if hasattr(self.application, 'shutting_down') and self.application.shutting_down == True: self.set_header('Content-Type', 'text/plain') self.write('SHUTTING DOWN') self.set_status(503) return result = await asyncio.gather( self.db_check(), ) self.set_header('Content-Type', 'application/json') self.write(utils.json_dumps(result)) if any([r['error'] for r in result]): self.set_status(500) else: self.set_status(200) self.finish()
def _fetch(self, method, uri, body=None, headers=None, timeout=TIMEOUT): if not headers: headers = {} if 'Content-Type' not in headers: headers['Content-Type'] = 'application/json' if 'Accept' not in headers: headers['Accept'] = 'application/json' if ('Authorization' not in headers) and self.access_token: headers['Authorization'] = 'Bearer {}'.format(self.access_token) try: if uri.startswith('http'): url = uri else: if not uri.startswith('/'): uri = '/' + uri url = self.url + uri response = yield self._client.fetch( httpclient.HTTPRequest( url, method=method, body=utils.json_dumps(body) if body or {} == body else None, headers=headers, connect_timeout=timeout, request_timeout=timeout, validate_cert=config['client']['validate_cert'], )) except httpclient.HTTPError as e: response = e.response if not response and (e.code == 599): raise API_error( status_code=e.code, code=e.code, message='Timeout', ) data = None if response.code != 404: if 400 <= response.code <= 600: if response.headers.get('Content-Type') == 'application/json': data = utils.json_loads(response.body) raise API_error(status_code=response.code, **data) raise Exception(response.body) data = HTTPData(self, response, timeout=timeout) if data is None: data = HTTPData(self, None, timeout=timeout) return data
def prepare(self): access_token = self.get_secure_cookie('session') if access_token: self.client.access_token = access_token.decode('utf-8') try: self.current_user = self.get_secure_cookie('user') self.current_user = utils.json_loads(self.current_user) \ if self.current_user else None if not self.current_user: self.current_user = yield self.client.get('/users/current') self.set_secure_cookie( 'user', utils.json_dumps(self.current_user), expires_days=None, ) except API_error as e: if e.code != 1009: # not signed in raise
def _fetch(self, method, uri, body=None, headers=None, timeout=TIMEOUT): if not headers: headers = {} if 'Content-Type' not in headers: headers['Content-Type'] = 'application/json' if 'Accept' not in headers: headers['Accept'] = 'application/json' if ('Authorization' not in headers) and self.access_token: headers['Authorization'] = 'Bearer {}'.format(self.access_token) try: if uri.startswith('http'): url = uri else: if not uri.startswith('/'): uri = '/'+uri url = self.url+uri response = yield self._client.fetch(httpclient.HTTPRequest( url, method=method, body=utils.json_dumps(body) if body or {} == body else None, headers=headers, connect_timeout=timeout, request_timeout=timeout, validate_cert=config['client']['validate_cert'], )) except httpclient.HTTPError as e: response = e.response if not response and (e.code == 599): raise API_error( status_code=e.code, code=e.code, message='Timeout', ) data = None if response.code != 404: if 400 <= response.code <= 600: if response.headers.get('Content-Type') == 'application/json': data = utils.json_loads(response.body) raise API_error(status_code=response.code, **data) raise Exception(response.body) data = HTTPData(self, response, timeout=timeout) if data is None: data = HTTPData(self, None, timeout=timeout) return data
def get_play_ids(self, show_id, number, servers): results = [] for server in servers: results.append({ 'play_id': web.create_signed_value( secret=server.secret, name='play_id', value=utils.json_dumps({ 'show_id': int(show_id), 'number': int(number), }), version=2, ), 'play_server': server, }) server.__dict__.pop('secret') return results
def _get(self, show_id, number): with new_session() as session: p = session.query(models.Play_server).filter( models.Play_access.user_id == self.current_user.id, models.Play_server.id == models.Play_access.play_server_id, ).options(sa.orm.undefer_group('secret')).all() playids = [] for s in p: playids.append({ 'play_id': web.create_signed_value( secret=s.secret, name='play_id', value=utils.json_dumps({ 'show_id': int(show_id), 'number': int(number), }), version=2, ), 'play_url': s.url, }) return playids
def write_object(self, d): self.write(utils.json_dumps(d))
def test(self): show_id = self.new_show() response = self.patch('/1/shows/{}'.format(show_id), { 'episodes': [ { 'air_date': date(2003, 9, 23), 'title': 'Yankee White', 'season': 1, 'number': 1, 'episode': 1, 'runtime': 45, }, ] } ) self.assertEqual(response.code, 200) # mark the episode as watched response = response = self.put('/1/users/{}/watched/shows/{}/episodes/{}'.format( self.current_user.id, show_id, 1) ) self.assertEqual(response.code, 200, response.body) self._test_watched_count(1, 45) # check that duplicating a watched episode works. response = response = self.put('/1/users/{}/watched/shows/{}/episodes/{}'.format( self.current_user.id, show_id, 1 ), { 'times': 100, }) self.assertEqual(response.code, 200) self._test_watched_count(101, 45*101) # check that the list of latest watched shows works. show_id_2 = self.new_show() response = self.patch( '/1/shows/{}'.format(show_id_2), json_dumps({ 'episodes': [ { 'air_date': date(2003, 9, 24), 'title': 'Some test', 'season': 1, 'number': 1, 'episode': 1, 'runtime': 45, }, ] }), ) # mark a second episode as watched. # run it twitch to check for duplication error. # it should increment the watched by 2 in the end. for i in [1,2]: response = self.put('/1/users/{}/watched/shows/{}/episodes/{}'.format( self.current_user.id, show_id_2, 1 )) self.assertEqual(response.code, 200) self._test_watched_count(103, 45*103) # check that the watched episode can be deleted response = self.delete('/1/users/{}/watched/shows/{}/episodes/{}'.format( self.current_user.id, show_id, 1 )) self.assertEqual(response.code, 200) response = self.delete('/1/users/{}/watched/shows/{}/episodes/{}'.format( self.current_user.id, show_id, 1 )) self.assertEqual(response.code, 400) # After removing episode 1 as watched there should # only be stats left from episode 2. self._test_watched_count(2, 45*2)
def __str__(self): return utils.json_dumps(self.data)
def process_bind_param(self, value, dialect): if value is None: return None if isinstance(value, str): return value return utils.json_dumps(value)
def process_bind_param(self, value, dialect): if value is None: return None if isinstance(value, str): return value return json_dumps(value)