Coverage for appr/client.py: 22%

# Coverage for appr/client.py : 22%

## 176 statements 38 run 138 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 227 228 229 230 231 232 233 234 235 236 | from __future__ import absolute_import, division, print_function import os import json import logging import re import requests from requests.utils import urlparse import appr from appr.auth import ApprAuth from appr.config import ApprConfig from appr.discovery import discover_sources, ishosted logger = logging.getLogger(__name__) DEFAULT_REGISTRY = 'http://localhost:5000' DEFAULT_PREFIX = "/cnr" class ApprClient(object): def __init__(self, endpoint=DEFAULT_REGISTRY, auth=None, config=None, requests_verify=True): if not auth: auth = ApprAuth() if not config: config = ApprConfig() self.auth = auth self.config = config self.package = None self.endpoint = self._configure_endpoint(endpoint) self.host = self.endpoint.geturl() self._headers = { 'Content-Type': 'application/json', 'User-Agent': "apprpy-cli/%s" % appr.__version__ } if 'APPR_CA_BUNDLES' in os.environ: requests_verify = os.environ['APPR_CA_BUNDLES'] if requests_verify is None: requests_verify = True self.verify = requests_verify def _url(self, path): return self.endpoint.geturl() + path def auth_token(self): """ return the Authorization bearer """ return self.auth.token(self.host, scope=self.package) def _configure_endpoint(self, endpoint): if endpoint is None: endpoint = DEFAULT_REGISTRY alias = self.config.get_registry_alias(endpoint) if alias: endpoint = alias if not re.match("https?://", endpoint): if endpoint.startswith("localhost"): scheme = "http://" else: scheme = "https://" endpoint = scheme + endpoint return urlparse(endpoint + DEFAULT_PREFIX) @property def headers(self): token = self.auth_token() headers = {} headers.update(self._headers) if token is not None: headers['Authorization'] = token return headers def version(self): path = "/version" resp = requests.get(self._url(path), headers=self.headers, verify=self.verify) resp.raise_for_status() return resp.json() def show_package(self, package, version, media_type=None): self.package = package path = "/api/v1/packages/%s" % (package) if version and version != 'default': path = path + "/%s" % version params = {} if media_type: params["media_type"] = media_type resp = requests.get( self._url(path), params=params, headers=self.headers, verify=self.verify) resp.raise_for_status() return resp.json() def _get_pull_url(self, package, version_parts, media_type): if media_type is None: raise ValueError("media-type is not set") organization, name = package.split("/") if version_parts['key'] == "digest": digest = version_parts['value'] url = "/api/v1/packages/%s/%s/blobs/sha256/%s" % (organization, name, digest) elif version_parts['key'] == "channel": chan_name = version_parts['value'] channel = self.show_channels(package, chan_name) version = channel['current'] url = "/api/v1/packages/%s/%s/%s/%s/pull" % (organization, name, version, media_type) elif version_parts['key'] == "version": version = version_parts['value'] url = "/api/v1/packages/%s/%s/%s/%s/pull" % (organization, name, version, media_type) else: url = "/api/v1/packages/%s/%s/%s/%s/pull" % (organization, name, version_parts['value'], media_type) return url def _pull_path(self, name, version_parts, media_type): self.package = name if ishosted(name): sources = discover_sources(name, version_parts['value'], media_type) path = sources[0] else: path = self._url(self._get_pull_url(name, version_parts, media_type)) return path def pull(self, name, version_parts, media_type): path = self._pull_path(name, version_parts, media_type) resp = requests.get(path, headers=self.headers, verify=self.verify) resp.raise_for_status() return resp.content def pull_json(self, name, version_parts, media_type): path = self._pull_path(name, version_parts, media_type) resp = requests.get(path, params={'format': 'json'}, headers=self.headers, verify=self.verify) resp.raise_for_status() return resp.json() def list_packages(self, params): path = "/api/v1/packages" resp = requests.get( self._url(path), params=params, headers=self.headers, verify=self.verify) resp.raise_for_status() return resp.json() def push(self, name, body, force=False): self.package = name organization, pname = name.split("/") body['name'] = pname body['organization'] = organization body['package'] = name path = "/api/v1/packages/%s/%s" % (organization, pname) resp = requests.post( self._url(path), params={"force": str(force).lower()}, data=json.dumps(body), headers=self.headers, verify=self.verify) resp.raise_for_status() return resp.json() def delete_package(self, name, version, media_type): self.package = name organization, name = name.split("/") path = "/api/v1/packages/%s/%s/%s/%s" % (organization, name, version, media_type) resp = requests.delete(self._url(path), headers=self.headers, verify=self.verify) resp.raise_for_status() return resp.json() def _crud_channel(self, name, channel='', action='get'): self.package = name if channel is None: channel = '' path = "/api/v1/packages/%s/channels/%s" % (name, channel) resp = getattr(requests, action)(self._url(path), params={}, headers=self.headers, verify=self.verify) if channel == '' and resp.status_code == 404: return [] resp.raise_for_status() return resp.json() def show_channels(self, name, channel=None): return self._crud_channel(name, channel) def create_channel(self, name, channel): return self._crud_channel(name, channel, 'post') def delete_channel(self, name, channel): return self._crud_channel(name, channel, 'delete') def create_channel_release(self, name, channel, release): path = "%s/%s" % (channel, release) return self._crud_channel(name, path, 'post') def delete_channel_release(self, name, channel, release): path = "%s/%s" % (channel, release) return self._crud_channel(name, path, 'delete') def login(self, username, password, scope=None): path = "/api/v1/users/login" resp = requests.post( self._url(path), data=json.dumps({ "user": { "username": username, "password": password } }), headers=self.headers, verify=self.verify) resp.raise_for_status() result = resp.json() self.auth.add_token(self.host, result['token'], scope=scope) return result def signup(self, username, password, password_confirmation, email): path = "/api/v1/users" resp = requests.post( self._url(path), data=json.dumps({ "user": { "username": username, "password": password, "password_confirmation": password_confirmation, "email": email } }), headers=self.headers, verify=self.verify) resp.raise_for_status() result = resp.json() self.auth.add_token(self.host, result['token']) return result def generate(self, name, namespace=None, variables=None, version=None, shards=None): path = "/api/v1/packages/%s/generate" % name params = {} body = {} body['variables'] = variables or {} if namespace: params['namespace'] = namespace if shards: body['shards'] = shards if version: params['version'] = version r = requests.get( self._url(path), data=json.dumps(body), params=params, headers=self.headers) r.raise_for_status() return r.json()