def _register_name_invalid(self, snap_name): # Emulates the current Store behaviour and never combines errors. # It's either using invalid chars or too long, never both. if len(snap_name) > 40: msg = ('The name {} should not be longer than 40 characters.'. format(snap_name)) else: msg = ('The name {!r} is not valid. It can only contain dashes, ' 'numbers and lowercase ascii letters.'.format(snap_name)) payload = json.dumps({ 'error_list': [ { 'code': 'invalid', 'message': msg }, ] }).encode() response_code = 400 content_type = 'application/json' return response.Response(payload, response_code, [('Content-Type', content_type)])
def snap_developers(self, request): logger.debug("Handling snap developers request") snap_id = request.matchdict["snap_id"] if snap_id == "good": payload = json.dumps({"snap_developer": {}}).encode() response_code = 200 elif snap_id in ("test-snap-id-with-dev", "revoked", "no-revoked"): payload = json.dumps({ "snap_developer": { "type": "snap-developer", "authority-id": "dummy", "publisher-id": "dummy", "snap-id": snap_id, "developers": [{ "developer-id": "test-dev-id", "since": "2017-02-10T08:35:00.390258Z", "until": "2018-02-10T08:35:00.390258Z", }], } }).encode() response_code = 200 elif snap_id == "no-dev": payload = json.dumps({ "error_list": [{ "message": "error", "code": "snap-developer-not-found" }] }).encode() response_code = 403 elif snap_id == "badrequest": payload = json.dumps({"snap_developer": {}}).encode() response_code = 200 content_type = "application/json" return response.Response(payload, response_code, [("Content-Type", content_type)])
def _account_key_successful(self, account_key_request): # Extremely basic assertion parsing, just enough to make tests work. # Don't copy this. key_name = re.search( '^name: (.*)$', account_key_request, flags=re.MULTILINE).group(1) key_id = re.search( '^public-key-sha3-384: (.*)$', account_key_request, flags=re.MULTILINE).group(1) self.account_keys.append( {'name': key_name, 'public-key-sha3-384': key_id}) account_key = { 'account_key': { 'account-id': 'abcd', 'name': key_name, 'public-key-sha3-384': key_id, }, } payload = json.dumps(account_key).encode() response_code = 200 content_type = 'application/json' return response.Response( payload, response_code, [('Content-Type', content_type)])
def snap_developers(self, request): logger.debug('Handling snap developers request') snap_id = request.matchdict['snap_id'] if snap_id == 'good': payload = json.dumps({'snap_developer': {}}).encode() response_code = 200 elif snap_id in ('test-snap-id-with-dev', 'revoked', 'no-revoked'): payload = json.dumps({ 'snap_developer': { 'type': 'snap-developer', 'authority-id': 'dummy', 'publisher-id': 'dummy', 'snap-id': snap_id, 'developers': [{ 'developer-id': 'test-dev-id', 'since': '2017-02-10T08:35:00.390258Z', 'until': '2018-02-10T08:35:00.390258Z' }] } }).encode() response_code = 200 elif snap_id == 'no-dev': payload = json.dumps({ 'error_list': [{ 'message': 'error', 'code': 'snap-developer-not-found' }] }).encode() response_code = 403 elif snap_id == 'badrequest': payload = json.dumps({'snap_developer': {}}).encode() response_code = 200 content_type = 'application/json' return response.Response(payload, response_code, [('Content-Type', content_type)])
def snap_history(self, request): if self.fake_store.needs_refresh: return self._refresh_error() logger.debug("Handling account request") revisions = [ { "series": ["16"], "channels": [], "version": "2.0.1", "timestamp": "2016-09-27T19:23:40Z", "current_channels": ["beta", "edge"], "arch": "i386", "revision": 2, }, { "series": ["16"], "channels": ["stable", "edge"], "version": "2.0.2", "timestamp": "2016-09-27T18:38:43Z", "current_channels": ["stable", "candidate", "beta"], "arch": "amd64", "revision": 1, }, ] parsed_qs = urllib.parse.parse_qs( urllib.parse.urlparse(request.url).query) if "arch" in parsed_qs: output = [ rev for rev in revisions if rev["arch"] in parsed_qs["arch"] ] else: output = revisions response_code = 200 content_type = "application/json" return response.Response( json.dumps(output).encode(), response_code, [("Content-Type", content_type)])
def _account_key_successful(self, account_key_request): # Extremely basic assertion parsing, just enough to make tests work. # Don't copy this. key_name = re.search( "^name: (.*)$", account_key_request, flags=re.MULTILINE ).group(1) key_id = re.search( "^public-key-sha3-384: (.*)$", account_key_request, flags=re.MULTILINE ).group(1) self.account_keys.append({"name": key_name, "public-key-sha3-384": key_id}) account_key = { "account_key": { "account-id": "abcd", "name": key_name, "public-key-sha3-384": key_id, } } payload = json.dumps(account_key).encode() response_code = 200 content_type = "application/json" return response.Response( payload, response_code, [("Content-Type", content_type)] )
def snap_builds(self, request): if self.fake_store.needs_refresh: return self._refresh_error() logger.debug('Handling sign-build request') snap_build = request.json_body['assertion'] if snap_build == 'test-not-implemented': payload = json.dumps({ 'error_list': [ {'code': 'feature-disabled', 'message': ('The snap-build assertions are currently ' 'disabled.')}, ], }).encode() response_code = 501 content_type = 'application/json' elif snap_build == 'test-invalid-data': payload = json.dumps({ 'error_list': [ {'code': 'invalid-field', 'message': 'The snap-build assertion is not valid.'}, ], }).encode() response_code = 400 content_type = 'application/json' elif snap_build == 'test-unexpected-data': payload = b'unexpected chunk of data' response_code = 500 content_type = 'text/plain' else: payload = json.dumps({ 'type': 'snap-build', 'foo': 'bar', }).encode() response_code = 200 content_type = 'application/json' return response.Response( payload, response_code, [('Content-Type', content_type)])
def agreement(self, request): if "STORE_DOWN" in os.environ: response_code = 500 content_type = "text/plain" payload = b"Broken" else: if request.json_body["latest_tos_accepted"] is not True: response_code = 400 content_type = "application/json" payload = json.dumps( { "error_list": [ { "message": "`latest_tos_accepted` must be `true`", "code": "bad-request", "extra": {"latest_tos_accepted": "true"}, } ] } ).encode() else: response_code = 200 content_type = "application/json" payload = json.dumps( { "content": { "latest_tos_accepted": True, "tos_url": "http://fake-url.com", "latest_tos_date": "2000-01-01", "accepted_tos_date": "2010-10-10", } } ).encode() return response.Response( payload, response_code, [("Content-Type", content_type)] )
def snap_builds(self, request): if self.fake_store.needs_refresh: return self._refresh_error() logger.debug("Handling sign-build request") snap_build = request.json_body["assertion"] if snap_build == "test-not-implemented": payload = json.dumps({ "error_list": [{ "code": "feature-disabled", "message": ("The snap-build assertions are currently disabled."), }] }).encode() response_code = 501 content_type = "application/json" elif snap_build == "test-invalid-data": payload = json.dumps({ "error_list": [{ "code": "invalid-field", "message": "The snap-build assertion is not valid.", }] }).encode() response_code = 400 content_type = "application/json" elif snap_build == "test-unexpected-data": payload = b"unexpected chunk of data" response_code = 500 content_type = "text/plain" else: payload = json.dumps({"type": "snap-build", "foo": "bar"}).encode() response_code = 200 content_type = "application/json" return response.Response(payload, response_code, [("Content-Type", content_type)])
def render(self): id_site = get_site_id(self.domain, self.db_session) site = self.db_session.query(models.Site).get(id_site) try: payment = self.db_session.query(models.Payments).filter( models.Payments.id_site == id_site).one() except sqlalchemy.orm.exc.NoResultFound: payment = False if site.published is False: return preview.showBuilding() else: if payment is False: return preview.showUnavalaible() else: site_builder = preview.SiteBuilder(self.request, id_site, self.db_session, domain=self.domain, is_domain=True) layout = site_builder.get_page_layout( site_builder.get_home_id() ) return response.Response(layout)
def snap_push(self, request): if self.fake_store.needs_refresh: return self._refresh_error() logger.debug( "Handling upload request with content {}".format(request.json_body) ) name = request.json_body["name"] if name == "test-snap-unregistered": payload = b"" response_code = 404 content_type = "text/plain" else: response_code = 202 content_type = "application/json" if name == "test-review-snap": details_path = "details/upload-id/review-snap" elif name == "test-duplicate-snap": details_path = "details/upload-id/duplicate-snap" elif name == "test-scan-error-with-braces": details_path = "details/upload-id/scan-error-with-braces" else: details_path = "details/upload-id/good-snap" if not request.json_body.get("dry_run", False): snap_id = self.registered_names[name]["snap_id"] self.pushed_snaps.add(snap_id) payload = json.dumps( { "status_details_url": urllib.parse.urljoin( "http://localhost:{}/".format(self.server_port), details_path ) } ).encode() return response.Response( payload, response_code, [("Content-Type", content_type)] )
def snap_release(self, request): # noqa: C901 if self.fake_store.needs_refresh: return self._refresh_error() logger.debug( "Handling release request with content {}".format(request.json_body) ) response_code = 200 content_type = "application/json" name = request.json_body["name"] channels = request.json_body["channels"] revision = request.json_body["revision"] if name == "test-snap-unregistered": response_code = 404 content_type = "text/plain" payload = b"" elif "alpha" in channels: response_code = 400 payload = json.dumps({"errors": "Not a valid channel: alpha"}).encode() elif "edge/{curly}" in channels: response_code = 400 payload = json.dumps( { "error_list": [ { "message": ( "Invalid branch name: {curly}. Enter a value consisting of letters, numbers or hyphens. " "Hyphens cannot occur at the start or end of the chosen value." ), "code": "invalid-field", } ] } ).encode() elif "no-permission" in channels: response_code = 403 payload = json.dumps( { "error_list": [ { "code": "macaroon-permission-required", "message": "Permission is required: channel", } ], "permission": "channel", "channels": ["no-permission"], } ).encode() elif "bad-channel" in channels: response_code = 500 payload = json.dumps({}).encode() elif name == "test-snap" or name.startswith("test-snapcraft"): payload = json.dumps( { "opened_channels": channels, "channel_map": [ {"channel": "stable", "info": "none"}, {"channel": "candidate", "info": "none"}, { "revision": int(revision), "channel": "beta", "version": "0", "info": "specific", }, {"channel": "edge", "info": "tracking"}, ], } ).encode() elif name.startswith("arm-"): payload = json.dumps( { "opened_channels": channels, "channel_map_tree": { "0.1": { "16": { "armhf": [ {"channel": "stable", "info": "none"}, {"channel": "candidate", "info": "none"}, { "revision": int(revision), "channel": "beta", "version": "0", "info": "specific", }, {"channel": "edge", "info": "tracking"}, ] } } }, } ).encode() elif name.startswith("multiarch-"): payload = json.dumps( { "opened_channels": channels, "channel_map_tree": { "0.1": { "16": { "amd64": [ {"channel": "stable", "info": "none"}, {"channel": "candidate", "info": "none"}, { "revision": int(revision), "channel": "beta", "version": "0", "info": "specific", }, {"channel": "edge", "info": "tracking"}, ], "armhf": [ {"channel": "stable", "info": "none"}, {"channel": "candidate", "info": "none"}, { "revision": int(revision), "channel": "beta", "version": "0", "info": "specific", }, {"channel": "edge", "info": "tracking"}, ], } } }, } ).encode() elif "notanumber" in revision: response_code = 400 payload = json.dumps( { "success": False, "error_list": [ { "code": "invalid-field", "message": "The 'revision' field must be an integer", } ], "errors": {"revision": ["This field must be an integer."]}, } ).encode() else: raise NotImplementedError( "Cannot handle release request for {!r}".format(name) ) return response.Response( payload, response_code, [("Content-Type", content_type)] )
def hello_world(request): return response.Response('Hello World!')
def snap_state(self, request): if self.fake_store.needs_refresh: return self._refresh_error() logger.debug('Handling snap state request') channel_map = { 'channel_map_tree': { 'latest': { '16': { 'i386': [ { 'info': 'none', 'channel': 'stable' }, { 'info': 'none', 'channel': 'beta' }, { 'info': 'specific', 'version': '1.0-i386', 'channel': 'edge', 'revision': 3 }, ], 'amd64': [ { 'info': 'specific', 'version': '1.0-amd64', 'channel': 'stable', 'revision': 2 }, { 'info': 'specific', 'version': '1.1-amd64', 'channel': 'beta', 'revision': 4 }, { 'info': 'tracking', 'channel': 'edge' }, ], } } } } parsed_qs = urllib.parse.parse_qs( urllib.parse.urlparse(request.url).query) if 'architecture' in parsed_qs: arch = parsed_qs['architecture'][0] series = channel_map['channel_map_tree']['latest']['16'] if arch in series: output = { 'channel_map_tree': { 'latest': { '16': { arch: series[arch] } } } } else: output = {} else: output = channel_map response_code = 200 content_type = 'application/json' return response.Response( json.dumps(output).encode(), response_code, [('Content-Type', content_type)])
def snap_validations(self, request): logger.debug("Handling validation request") snap_id = request.matchdict["snap_id"] if snap_id == "good": validation = [ { "approved-snap-id": "snap-id-1", "approved-snap-revision": "3", "approved-snap-name": "snap-1", "authority-id": "dev-1", "series": "16", "sign-key-sha3-384": "1234567890", "snap-id": "snap-id-gating", "timestamp": "2016-09-19T21:07:27.756001Z", "type": "validation", "revoked": "false", "required": True, }, { "approved-snap-id": "snap-id-2", "approved-snap-revision": "5", "approved-snap-name": "snap-2", "authority-id": "dev-1", "series": "16", "sign-key-sha3-384": "1234567890", "snap-id": "snap-id-gating", "timestamp": "2016-09-19T21:07:27.756001Z", "type": "validation", "revoked": "false", "required": False, }, { "approved-snap-id": "snap-id-3", "approved-snap-revision": "-", "approved-snap-name": "snap-3", "authority-id": "dev-1", "series": "16", "sign-key-sha3-384": "1234567890", "snap-id": "snap-id-gating", "timestamp": "2016-09-19T21:07:27.756001Z", "type": "validation", "revoked": "false", "required": True, }, ] payload = json.dumps(validation).encode() response_code = 200 elif snap_id == "bad": payload = "foo".encode() response_code = 200 elif snap_id == "test-snap-id-with-no-validations": payload = json.dumps([]).encode() response_code = 200 elif snap_id == "err": payload = json.dumps( {"error_list": [{"code": "test-code", "message": "test-error"}]} ).encode() response_code = 503 content_type = "application/json" return response.Response( payload, response_code, [("Content-Type", content_type)] )
def snap_close(self, request): if self.fake_store.needs_refresh: return self._refresh_error() logger.debug("Handling close request") channels = request.json_body["channels"] if channels == ["invalid"]: payload = json.dumps({ "error_list": [{ "code": "invalid-field", "message": ("The 'channels' field content is not valid."), }] }).encode() response_code = 400 content_type = "application/json" elif channels == ["unexpected"]: payload = b"unexpected chunk of data" response_code = 500 content_type = "text/plain" elif channels == ["broken-plain"]: payload = b"plain data" response_code = 200 content_type = "text/plain" elif channels == ["broken-json"]: payload = json.dumps({"closed_channels": channels}).encode() response_code = 200 content_type = "application/json" else: payload = json.dumps({ "closed_channels": channels, "channel_map_tree": { "latest": { "16": { "amd64": [ { "channel": "stable", "info": "none" }, { "channel": "candidate", "info": "none" }, { "channel": "beta", "info": "specific", "version": "1.1", "revision": 42, }, { "channel": "edge", "info": "tracking" }, ] } } }, }).encode() response_code = 200 content_type = "application/json" return response.Response(payload, response_code, [("Content-Type", content_type)])
def account(self, request): if self.fake_store.needs_refresh: return self._refresh_error() logger.debug('Handling account request') snaps = { 'basic': { 'snap-id': 'snap-id', 'status': 'Approved', 'private': False, 'price': None, 'since': '2016-12-12T01:01:01Z' }, 'test-snap-with-no-validations': { 'snap-id': 'test-snap-id-with-no-validations', 'status': 'Approved', 'private': False, 'price': None, 'since': '2016-12-12T01:01:01Z' }, 'test-snap-with-dev': { 'snap-id': 'test-snap-id-with-dev', 'status': 'Approved', 'private': False, 'price': None, 'since': '2016-12-12T01:01:01Z' }, 'core': { 'snap-id': 'good', 'status': 'Approved', 'private': False, 'price': None, 'since': '2016-12-12T01:01:01Z' }, 'core-no-dev': { 'snap-id': 'no-dev', 'status': 'Approved', 'private': False, 'price': None, 'since': '2016-12-12T01:01:01Z' }, 'badrequest': { 'snap-id': 'badrequest', 'status': 'Approved', 'private': False, 'price': None, 'since': '2016-12-12T01:01:01Z' }, 'revoked': { 'snap-id': 'revoked', 'status': 'Approved', 'private': False, 'price': None, 'since': '2016-12-12T01:01:01Z' }, 'no-revoked': { 'snap-id': 'no-revoked', 'status': 'Approved', 'private': False, 'price': None, 'since': '2016-12-12T01:01:01Z' }, } snaps.update({ name: { 'snap-id': snap_data['snap_id'], 'status': 'Approved', 'private': snap_data['private'], 'price': None, 'since': '2016-12-12T01:01:01Z' } for name, snap_data in self.registered_names.items() }) payload = json.dumps({ 'account_id': 'abcd', 'account_keys': self.account_keys, 'snaps': { '16': snaps }, }).encode() response_code = 200 content_type = 'application/json' return response.Response(payload, response_code, [('Content-Type', content_type)])
def snap_release(self, request): if self.fake_store.needs_refresh: return self._refresh_error() logger.debug('Handling release request with content {}'.format( request.json_body)) response_code = 200 content_type = 'application/json' name = request.json_body['name'] channels = request.json_body['channels'] revision = request.json_body['revision'] if name == 'test-snap-unregistered': response_code = 404 content_type = 'text/plain' payload = b'' elif 'alpha' in channels: response_code = 400 payload = json.dumps({ 'errors': 'Not a valid channel: alpha', }).encode() elif (name == 'test-snap' or name.startswith('snapcrafttest')): payload = json.dumps({ 'opened_channels': channels, 'channel_map': [{ 'channel': 'stable', 'info': 'none' }, { 'channel': 'candidate', 'info': 'none' }, { 'revision': int(revision), 'channel': 'beta', 'version': '0', 'info': 'specific' }, { 'channel': 'edge', 'info': 'tracking' }] }).encode() elif name.startswith('arm-'): payload = json.dumps({ 'opened_channels': channels, 'channel_map_tree': { '0.1': { '16': { 'armhf': [{ 'channel': 'stable', 'info': 'none' }, { 'channel': 'candidate', 'info': 'none' }, { 'revision': int(revision), 'channel': 'beta', 'version': '0', 'info': 'specific' }, { 'channel': 'edge', 'info': 'tracking' }] } } } }).encode() elif name.startswith('multiarch-'): payload = json.dumps({ 'opened_channels': channels, 'channel_map_tree': { '0.1': { '16': { 'amd64': [{ 'channel': 'stable', 'info': 'none' }, { 'channel': 'candidate', 'info': 'none' }, { 'revision': int(revision), 'channel': 'beta', 'version': '0', 'info': 'specific' }, { 'channel': 'edge', 'info': 'tracking' }], 'armhf': [{ 'channel': 'stable', 'info': 'none' }, { 'channel': 'candidate', 'info': 'none' }, { 'revision': int(revision), 'channel': 'beta', 'version': '0', 'info': 'specific' }, { 'channel': 'edge', 'info': 'tracking' }] } } } }).encode() elif 'notanumber' in revision: response_code = 400 payload = json.dumps({ 'success': False, 'error_list': [{ 'code': 'invalid-field', 'message': "The 'revision' field must be an integer" }], 'errors': { 'revision': ['This field must be an integer.'] } }).encode() else: raise NotImplementedError( 'Cannot handle release request for {!r}'.format(name)) return response.Response(payload, response_code, [('Content-Type', content_type)])
def snap_state(self, request): if self.fake_store.needs_refresh: return self._refresh_error() logger.debug("Handling snap state request") channel_map = { "channel_map_tree": { "latest": { "16": { "i386": [ { "info": "none", "channel": "stable" }, { "info": "none", "channel": "beta" }, { "info": "specific", "version": "1.0-i386", "channel": "edge", "revision": 3, }, ], "amd64": [ { "info": "specific", "version": "1.0-amd64", "channel": "stable", "revision": 2, }, { "info": "specific", "version": "1.1-amd64", "channel": "beta", "revision": 4, }, { "info": "tracking", "channel": "edge" }, ], } } } } parsed_qs = urllib.parse.parse_qs( urllib.parse.urlparse(request.url).query) if "architecture" in parsed_qs: arch = parsed_qs["architecture"][0] series = channel_map["channel_map_tree"]["latest"]["16"] if arch in series: output = { "channel_map_tree": { "latest": { "16": { arch: series[arch] } } } } else: output = {} else: output = channel_map response_code = 200 content_type = "application/json" return response.Response( json.dumps(output).encode(), response_code, [("Content-Type", content_type)])
def posts(request): engine = request.registry.settings['engine'] posts = [(content, 'anonymous') for content, in storage.get_posts(engine)] body = json.dumps(posts).encode('utf-8') return response.Response(content_type='application/json', body=body)
def snap_close(self, request): if self.fake_store.needs_refresh: return self._refresh_error() logger.debug('Handling close request') channels = request.json_body['channels'] if channels == ['invalid']: payload = json.dumps({ 'error_list': [ { 'code': 'invalid-field', 'message': ('The \'channels\' field content is not ' 'valid.') }, ], }).encode() response_code = 400 content_type = 'application/json' elif channels == ['unexpected']: payload = b'unexpected chunk of data' response_code = 500 content_type = 'text/plain' elif channels == ['broken-plain']: payload = b'plain data' response_code = 200 content_type = 'text/plain' elif channels == ['broken-json']: payload = json.dumps({ 'closed_channels': channels, }).encode() response_code = 200 content_type = 'application/json' else: payload = json.dumps({ 'closed_channels': channels, 'channel_map_tree': { 'latest': { '16': { 'amd64': [{ 'channel': 'stable', 'info': 'none' }, { 'channel': 'candidate', 'info': 'none' }, { 'channel': 'beta', 'info': 'specific', 'version': '1.1', 'revision': 42 }, { 'channel': 'edge', 'info': 'tracking' }] } } }, }).encode() response_code = 200 content_type = 'application/json' return response.Response(payload, response_code, [('Content-Type', content_type)])
def account(self, request): if self.fake_store.needs_refresh: return self._refresh_error() logger.debug("Handling account request") snaps = { "basic": { "snap-id": "snap-id", "status": "Approved", "private": False, "price": None, "since": "2016-12-12T01:01:01Z", }, "test-snap-with-no-validations": { "snap-id": "test-snap-id-with-no-validations", "status": "Approved", "private": False, "price": None, "since": "2016-12-12T01:01:01Z", }, "test-snap-with-dev": { "snap-id": "test-snap-id-with-dev", "status": "Approved", "private": False, "price": None, "since": "2016-12-12T01:01:01Z", }, "core": { "snap-id": "good", "status": "Approved", "private": False, "price": None, "since": "2016-12-12T01:01:01Z", }, "core-no-dev": { "snap-id": "no-dev", "status": "Approved", "private": False, "price": None, "since": "2016-12-12T01:01:01Z", }, "badrequest": { "snap-id": "badrequest", "status": "Approved", "private": False, "price": None, "since": "2016-12-12T01:01:01Z", }, "revoked": { "snap-id": "revoked", "status": "Approved", "private": False, "price": None, "since": "2016-12-12T01:01:01Z", }, "no-revoked": { "snap-id": "no-revoked", "status": "Approved", "private": False, "price": None, "since": "2016-12-12T01:01:01Z", }, "no-id": { "snap-id": None, "status": "Approved", "private": False, "price": None, "since": "2016-12-12T01:01:01Z", }, } snaps.update( { name: { "snap-id": snap_data["snap_id"], "status": "Approved", "private": snap_data["private"], "price": None, "since": "2016-12-12T01:01:01Z", } for name, snap_data in self.registered_names.items() } ) payload = json.dumps( { "account_id": "abcd", "account_keys": self.account_keys, "snaps": {"16": snaps}, } ).encode() response_code = 200 content_type = "application/json" return response.Response( payload, response_code, [("Content-Type", content_type)] )
def add_post(request): POSTS.append(request.json_body['content']) return response.Response('ok')
def posts(request): body = json.dumps(POSTS).encode('utf-8') return response.Response( content_type='application/json', body=body)
def zuul_status(request): template, params = process_request(request) return response.Response(template.render(**params))
def snap_binary_metadata(self, request): logger.debug('Handling binary metadata request') if request.method == 'GET': current = [ { 'type': 'icon', 'hash': '1234567890', 'filename': 'icon.png' }, { 'type': 'screenshot', 'hash': '0987654321', 'filename': 'ss1.png' }, { 'type': 'screenshot', 'hash': '1122334455', 'filename': 'ss2.png' }, ] return response.Response( json.dumps(current).encode('utf-8'), 200, [('Content-Type', 'application/json')]) else: # POST/PUT info = json.loads(request.params['info']) invalid = any( [e.get('filename', '').endswith('invalid') for e in info]) conflict = any( [e.get('filename', '').endswith('conflict') for e in info]) if invalid: err = { 'error_list': [{ 'message': 'Invalid field: icon', 'code': 'invalid-request', }] } payload = json.dumps(err).encode('utf8') response_code = 400 elif conflict and request.method == 'POST': # POST, return error error_list = [{ 'message': 'original-icon', 'code': 'conflict', 'extra': { 'name': 'icon' }, }] payload = json.dumps({'error_list': error_list}).encode('utf8') response_code = 409 else: updated_info = [] for entry in info: entry.pop('key', None) updated_info.append(entry) payload = json.dumps(updated_info).encode('utf-8') response_code = 200 content_type = 'application/json' return response.Response(payload, response_code, [('Content-Type', content_type)])
def snap_binary_metadata(self, request): logger.debug("Handling binary metadata request") if request.method == "GET": current = [ {"type": "icon", "hash": "1234567890", "filename": "icon.png"}, {"type": "screenshot", "hash": "0987654321", "filename": "ss1.png"}, {"type": "screenshot", "hash": "1122334455", "filename": "ss2.png"}, ] return response.Response( json.dumps(current).encode("utf-8"), 200, [("Content-Type", "application/json")], ) else: # POST/PUT info = json.loads(request.params["info"]) invalid = any([e.get("filename", "").endswith("invalid") for e in info]) conflict = any([e.get("filename", "").endswith("conflict") for e in info]) conflict_with_braces = any( [e.get("filename", "").endswith("conflict-with-braces") for e in info] ) if invalid: err = { "error_list": [ {"message": "Invalid field: icon", "code": "invalid-request"} ] } payload = json.dumps(err).encode("utf8") response_code = 400 elif conflict and request.method == "POST": # POST, return error error_list = [ { "message": "original-icon", "code": "conflict", "extra": {"name": "icon"}, } ] payload = json.dumps({"error_list": error_list}).encode("utf8") response_code = 409 elif conflict_with_braces and request.method == "POST": # POST, return error error_list = [ { "message": "original icon with {braces}", "code": "conflict", "extra": {"name": "icon"}, } ] payload = json.dumps({"error_list": error_list}).encode("utf8") response_code = 409 else: updated_info = [] for entry in info: entry.pop("key", None) updated_info.append(entry) payload = json.dumps(updated_info).encode("utf-8") response_code = 200 content_type = "application/json" return response.Response( payload, response_code, [("Content-Type", content_type)] )
def _account_key_not_implemented(self): payload = b"Not Implemented" response_code = 501 content_type = "text/plain" return response.Response(payload, response_code, [("Content-Type", content_type)])
def get_status(_request): """Status endpoint.""" return response.Response(status_int=200, status="200 OK", content_type="text/plain")
def _account_key_not_implemented(self): payload = b'Not Implemented' response_code = 501 content_type = 'text/plain' return response.Response(payload, response_code, [('Content-Type', content_type)])