Coverage for appr/api/registry.py: 0%

# Coverage for appr/api/registry.py : 0%

## 117 statements 0 run 117 missing 0 excluded

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | from __future__ import absolute_import, division, print_function import json from base64 import b64decode from flask import Blueprint, current_app, jsonify, request import appr.api.impl.registry from appr.api.app import getvalues, repo_name from appr.exception import ( ApprException, ChannelNotFound, InvalidParams, InvalidRelease, InvalidUsage, PackageAlreadyExists, PackageNotFound, PackageReleaseNotFound, UnableToLockResource, UnauthorizedAccess, Unsupported) from appr.models import DEFAULT_MEDIA_TYPE, Blob, Channel, Package registry_app = Blueprint( 'registry', __name__, ) @registry_app.errorhandler(Unsupported) @registry_app.errorhandler(PackageAlreadyExists) @registry_app.errorhandler(InvalidRelease) @registry_app.errorhandler(UnableToLockResource) @registry_app.errorhandler(UnauthorizedAccess) @registry_app.errorhandler(PackageNotFound) @registry_app.errorhandler(PackageReleaseNotFound) @registry_app.errorhandler(ApprException) @registry_app.errorhandler(InvalidUsage) @registry_app.errorhandler(InvalidParams) @registry_app.errorhandler(ChannelNotFound) def render_error(error): response = jsonify(error.to_dict()) response.status_code = error.status_code return response @registry_app.before_request def pre_request_logging(): jsonbody = request.get_json(force=True, silent=True) values = request.values.to_dict() if jsonbody: values.update(jsonbody) current_app.logger.info("request", extra={ "remote_addr": request.remote_addr, "http_method": request.method, "original_url": request.url, "path": request.path, "data": values, "headers": dict(request.headers.to_list())}) @registry_app.route("/test_error") def test_error(): raise InvalidUsage("error message", {"path": request.path}) def _pull(data, json_format=True): if json_format: resp = jsonify(data) else: resp = current_app.make_response(b64decode(data['blob'])) resp.headers['Content-Disposition'] = data['filename'] resp.mimetype = 'application/x-gzip' return resp @registry_app.route( "/api/v1/packages/<string:namespace>/<string:package_name>/blobs/sha256/<string:digest>", methods=['GET'], strict_slashes=False) def blobs(namespace, package_name, digest): reponame = repo_name(namespace, package_name) data = appr.api.impl.registry.pull_blob(reponame, digest, blob_class=Blob) json_format = request.args.get('format', None) == 'json' return _pull(data, json_format=json_format) @registry_app.route( "/api/v1/packages/<string:namespace>/<string:package_name>/blobs/sha256/<string:digest>/json", methods=['GET'], strict_slashes=False) def blobs_json(namespace, package_name, digest): reponame = repo_name(namespace, package_name) data = appr.api.impl.registry.pull_blob(reponame, digest, blob_class=Blob) return _pull(data, json_format=True) @registry_app.route( "/api/v1/packages/<string:namespace>/<string:package_name>/<string:release>/<string:media_type>/pull", methods=['GET'], strict_slashes=False) def pull(namespace, package_name, release, media_type): reponame = repo_name(namespace, package_name) data = appr.api.impl.registry.pull(reponame, release, media_type, Package, blob_class=Blob) json_format = request.args.get('format', None) == 'json' return _pull(data, json_format=json_format) @registry_app.route( "/api/v1/packages/<string:namespace>/<string:package_name>/<string:release>/<string:media_type>/pull/json", methods=['GET'], strict_slashes=False) def pull_json(namespace, package_name, release, media_type): reponame = repo_name(namespace, package_name) data = appr.api.impl.registry.pull(reponame, release, media_type, Package, blob_class=Blob) return _pull(data, json_format=True) @registry_app.route("/api/v1/packages/<string:namespace>/<string:package_name>", methods=['POST'], strict_slashes=False) def push(namespace, package_name): reponame = repo_name(namespace, package_name) values = getvalues() release = values['release'] media_type = values.get('media_type', DEFAULT_MEDIA_TYPE) force = (values.get('force', 'false') == 'true') metadata = values.get('metadata', None) blob = Blob(reponame, values['blob']) result = appr.api.impl.registry.push(reponame, release, media_type, blob, force, Package, metadata=metadata) return jsonify(result) @registry_app.route( "/api/v1/packages/<string:namespace>/<string:package_name>/<string:release>/<string:media_type>", methods=['DELETE'], strict_slashes=False) def delete_package(namespace, package_name, release, media_type): reponame = repo_name(namespace, package_name) result = appr.api.impl.registry.delete_package(reponame, release, media_type, package_class=Package) return jsonify(result) @registry_app.route("/api/v1/packages", methods=['GET'], strict_slashes=False) def list_packages(): values = getvalues() namespace = values.get('namespace', None) result = appr.api.impl.registry.list_packages(namespace, Package, search=values.get( 'query', None), media_type=values.get('media_type', None)) resp = current_app.make_response(json.dumps(result)) resp.mimetype = 'application/json' return resp @registry_app.route("/api/v1/packages/search", methods=['GET'], strict_slashes=False) def search_packages(): values = getvalues() query = values.get("q") result = appr.api.impl.registry.search(query, Package) return jsonify(result) @registry_app.route( "/api/v1/packages/<string:namespace>/<string:package_name>/<string:release>/<string:media_type>", methods=['GET'], strict_slashes=False) def show_package(namespace, package_name, release, media_type): reponame = repo_name(namespace, package_name) result = appr.api.impl.registry.show_package(reponame, release, media_type, channel_class=Channel, package_class=Package) return jsonify(result) @registry_app.route("/api/v1/packages/<string:namespace>/<string:package_name>", methods=['GET'], strict_slashes=False) def show_package_releases(namespace, package_name): reponame = repo_name(namespace, package_name) media_type = getvalues().get('media_type', None) result = appr.api.impl.registry.show_package_releases(reponame, media_type=media_type, package_class=Package) return jsonify(result) @registry_app.route("/api/v1/packages/<string:namespace>/<string:package_name>/<string:release>", methods=['GET'], strict_slashes=False) def show_package_release_manifests(namespace, package_name, release): reponame = repo_name(namespace, package_name) result = appr.api.impl.registry.show_package_manifests(reponame, release, package_class=Package) return jsonify(result) # CHANNELS @registry_app.route("/api/v1/packages/<string:namespace>/<string:package_name>/channels", methods=[ 'GET'], strict_slashes=False) def list_channels(namespace, package_name): reponame = repo_name(namespace, package_name) result = appr.api.impl.registry.list_channels(reponame, Channel) resp = current_app.make_response(json.dumps(result)) resp.mimetype = 'application/json' return resp @registry_app.route( "/api/v1/packages/<string:namespace>/<string:package_name>/channels/<string:channel_name>", methods=['GET'], strict_slashes=False) def show_channel(namespace, package_name, channel_name): reponame = repo_name(namespace, package_name) result = appr.api.impl.registry.show_channel(reponame, channel_name, Channel) return jsonify(result) @registry_app.route( "/api/v1/packages/<string:namespace>/<string:package_name>/channels/<string:channel_name>/<string:release>", methods=['POST'], strict_slashes=False) def add_channel_release(namespace, package_name, channel_name, release): reponame = repo_name(namespace, package_name) result = appr.api.impl.registry.add_channel_release( reponame, channel_name, release, channel_class=Channel, package_class=Package) return jsonify(result) @registry_app.route( "/api/v1/packages/<string:namespace>/<string:package_name>/channels/<string:channel_name>/<string:release>", methods=['DELETE'], strict_slashes=False) def delete_channel_release(namespace, package_name, channel_name, release): reponame = repo_name(namespace, package_name) result = appr.api.impl.registry.delete_channel_release( reponame, channel_name, release, channel_class=Channel, package_class=Package) return jsonify(result) @registry_app.route( "/api/v1/packages/<string:namespace>/<string:package_name>/channels/<string:channel_name>", methods=['DELETE'], strict_slashes=False) def delete_channel(namespace, package_name, channel_name): reponame = repo_name(namespace, package_name) result = appr.api.impl.registry.delete_channel(reponame, channel_name, channel_class=Channel) return jsonify(result)