def _handle_request(self, req): return_url = req.values.get('return_url') errors = {'openid': [], 'mb': []} if 'login' in req.form: if req.form['login'] == 'mb': self._handle_musicbrainz_login(req, errors['mb']) from acoustid.api import serialize_response, errors as _errors, v2 as api_v2 if req.form.get('format') in api_v2.FORMATS: if self.session.get('id'): info = get_account_details(self.conn, self.session['id']) response = {'status': 'ok', 'api_key': info['apikey']} return serialize_response(response, req.form['format']) else: e = _errors.InvalidUserAPIKeyError() response = {'status': 'error', 'error': {'code': e.code, 'message': e.message}} return serialize_response(response, req.form['format'], status=400) elif req.form['login'] == 'openid': resp = self._handle_openid_login(req, errors['openid']) if resp is not None: return resp if 'openid.mode' in req.args: self._handle_openid_login_response(req, errors['openid']) if 'id' in self.session: if not return_url or not return_url.startswith(self.config.base_url): return_url = self.config.base_url + 'api-key' return redirect(return_url) return self.render_template('login.html', errors=errors, return_url=return_url)
def _error(self, code, message, format=FORMAT, status=400): assert format == FORMAT response_data = { '@status': 'error', 'error': message, } return serialize_response(response_data, format, status=status)
def test_serialize_xml(): data = {'status': 'ok', 'artists': [{'name': 'Jean Michel Jarre', 'year': 1948, 'cities': ['Paris', 'Lyon']}]} resp = serialize_response(data, 'xml') assert_equals('text/xml; charset=UTF-8', resp.content_type) expected = ( '''<?xml version='1.0' encoding='UTF-8'?>\n<response><status>ok</status><artists><artist><cities><city>Paris</city><city>Lyon''' '''</city></cities><name>Jean Michel Jarre</name><year>1948</year></artist></artists></response>''' ) assert_equals(expected, resp.data)
def _error(self, code, message, format=DEFAULT_FORMAT, status=400): response_data = { 'status': 'error', 'error': { 'code': code, 'message': message } } return serialize_response(response_data, format, status=status)
def _ok(self, data, format=DEFAULT_FORMAT): # type: (Dict[str, Any], str) -> Response response_data = {'status': 'ok'} response_data.update(data) t0 = time.time() try: return serialize_response(response_data, format) finally: t1 = time.time() if self.ctx.statsd is not None: request_type = self.__class__.__name__ self.ctx.statsd.timing( 'serialize_response,format={},request={}'.format( format, request_type), 1000 * (t1 - t0))
def test_serialize_json(): data = { 'status': 'ok', 'artists': [{ 'name': 'Jean Michel Jarre', 'year': 1948, 'cities': ['Paris', 'Lyon'] }] } resp = serialize_response(data, 'json') assert_equals('application/json; charset=UTF-8', resp.content_type) expected = b'''{"artists": [{"cities": ["Paris", "Lyon"], "name": "Jean Michel Jarre", "year": 1948}], "status": "ok"}''' assert_equals(expected, resp.data)
def _ok(self, data, format=DEFAULT_FORMAT): # type: (Dict[str, Any], str) -> Response response_data = {'status': 'ok'} response_data.update(data) return serialize_response(response_data, format)
def test_serialize_xml_attribute(): data = {'@status': 'ok'} resp = serialize_response(data, 'xml') assert_equals('text/xml; charset=UTF-8', resp.content_type) expected = b'''<?xml version='1.0' encoding='UTF-8'?>\n<response status="ok" />''' assert_equals(expected, resp.data)
def _ok(self, data, format=FORMAT): assert format == FORMAT response_data = {'@status': 'ok'} response_data.update(data) return serialize_response(response_data, format)
def _ok(self, data, format=DEFAULT_FORMAT): response_data = {"status": "ok"} response_data.update(data) return serialize_response(response_data, format)
def test_serialize_json(): data = {'status': 'ok', 'artists': [{'name': 'Jean Michel Jarre', 'year': 1948, 'cities': ['Paris', 'Lyon']}]} resp = serialize_response(data, 'json') assert_equals('application/json; charset=UTF-8', resp.content_type) expected = '''{"status": "ok", "artists": [{"cities": ["Paris", "Lyon"], "name": "Jean Michel Jarre", "year": 1948}]}''' assert_equals(expected, resp.data)
def test_serialize_xml_attribute(): data = {'@status': 'ok'} resp = serialize_response(data, 'xml') assert_equals('text/xml; charset=UTF-8', resp.content_type) expected = '''<?xml version='1.0' encoding='UTF-8'?>\n<response status="ok" />''' assert_equals(expected, resp.data)
def test_serialize_xml(): data = {'status': 'ok', 'artists': [{'name': 'Jean Michel Jarre', 'year': 1948, 'cities': ['Paris', 'Lyon']}]} resp = serialize_response(data, 'xml') assert_equals('text/xml; charset=UTF-8', resp.content_type) expected = '''<?xml version='1.0' encoding='UTF-8'?>\n<response><status>ok</status><artists><artist><cities><city>Paris</city><city>Lyon</city></cities><name>Jean Michel Jarre</name><year>1948</year></artist></artists></response>''' assert_equals(expected, resp.data)
def _ok(self, data, format=DEFAULT_FORMAT): response_data = {'status': 'ok'} response_data.update(data) return serialize_response(response_data, format)
def _error(self, code, message, format=DEFAULT_FORMAT, status=400): response_data = {"status": "error", "error": {"code": code, "message": message}} return serialize_response(response_data, format, status=status)