def get_package_status(package_uid): url = get_server_url('/packages/{}'.format(package_uid)) try: return http.get(url, json=True).json()['status'] except http.HTTPError as error: raise UpdateHubError(error) except (ValueError, KeyError): raise UpdateHubError('Could not get package info. Try again later.')
def finish_package(package_uid, callback=None): url = get_server_url('/packages/{}/finish'.format(package_uid)) try: http.put(url) except http.HTTPError as error: raise UpdateHubError( 'Could not finish package on server: {}'.format(error)) finally: call(callback, 'push_finish', package_uid)
def upload_metadata(metadata): try: validate_metadata(metadata) except ValidationError: raise UpdateHubError('You have an invalid package metadata.') url = get_server_url('/packages') signature = sign_dict(metadata, config.get_private_key_path()) payload = json.dumps(metadata, sort_keys=True) headers = {'UH-SIGNATURE': signature} try: response = http.post( url, payload=payload, json=True, headers=headers).json() return response['uid'] except http.HTTPError as error: raise UpdateHubError('Could not upload metadata: {}'.format(error)) except (ValueError, KeyError): raise UpdateHubError('Could not upload metadata: unknown error.')
def upload_object(obj, package_uid, callback=None): """Uploads a package object to UpdateHub server.""" # First, check if we should upload the object url = get_server_url('/packages/{}/objects/{}'.format( package_uid, obj['sha256sum'])) body = json.dumps({'etag': obj['md5']}) try: response = http.post(url, body, json=True) except http.HTTPError: return ObjectUploadResult.FAIL # Object already uploaded, return EXISTS. if response.status_code == 200: call(callback, 'object_read', obj['chunks']) return ObjectUploadResult.EXISTS # Object not uploaded, try to uploaded it. try: body = response.json() uploader = STORAGES[body['storage']] url = body['url'] except (ValueError, KeyError): return ObjectUploadResult.FAIL return uploader(obj['filename'], url, callback)
def test_get_server_url_strips_slashes(self): os.environ[utils.SERVER_URL_VAR] = 'http://ossystems.com.br/' observed = utils.get_server_url() self.assertEqual(observed, 'http://ossystems.com.br')
def test_can_get_url_with_path(self): os.environ[utils.SERVER_URL_VAR] = 'http://ossystems.com.br' observed = utils.get_server_url('/test') self.assertEqual(observed, 'http://ossystems.com.br/test')
def test_get_default_server_url(self): observed = utils.get_server_url() self.assertEqual(observed, utils.DEFAULT_SERVER_URL)
def test_get_server_url_by_environment_variable(self): os.environ[utils.SERVER_URL_VAR] = 'http://ossystems.com.br' observed = utils.get_server_url() self.assertEqual(observed, 'http://ossystems.com.br')