class AppRegisterAPI(Resource): def __init__(self, repo: AppRepository = None, validator: Validator = None): self.parser = reqparse.RequestParser() if repo: self.repo = repo else: self.repo = AppRepository() if validator: self.validator = validator else: self.validator = Validator() self.parser.add_argument('source_url', type=str, location='json') self.parser.add_argument('system', type=str, location='json') self.parser.add_argument('name', type=str, location='json') self.parser.add_argument('is_package', type=bool, location='json') super(AppRegisterAPI, self).__init__() def post(self): data = self.parser.parse_args() app_id = 0 if data: is_package = data['is_package'] source_url = data['source_url'] if not is_package: self.validator.validate_url(source_url) system = data['system'] self.validator.validate_sys(system) name = data['name'] new_app = Application(name, source_url, system, is_package=is_package) app_id = self.repo.store_app(new_app) return jsonify({'resource_uri': "/app/{}".format(app_id)}) def delete(self, app_id): app: Application = self.repo.load_app(app_id) if not app: abort(404) if app.is_installed(): abort(409) self.repo.remove_app(app_id) return None, 204
class AppListAPI(Resource): def __init__(self, repo: AppRepository = None): if not repo: self.repo = AppRepository() else: self.repo = repo def get(self) -> List[Application]: apps = self.repo.load_apps() return jsonify(apps) def delete(self) -> Tuple[None, int]: self.repo.remove_apps() return None, 204
class AppAPI(Resource): def __init__(self, repo: AppRepository = None, validator: Validator = None): self.parser = reqparse.RequestParser() if repo: self.repo = repo else: self.repo = AppRepository() if validator: self.validator = validator else: self.validator = Validator() self.parser.add_argument('source_url', type=str, location='json') self.parser.add_argument('system', type=str, location='json') self.parser.add_argument('name', type=str, location='json') super(AppAPI, self).__init__() def get(self, app_id): app = self.repo.load_app(app_id) if app: return jsonify(app) abort(404) def put(self, app_id): data = self.parser.parse_args() source_url = data['source_url'] self.validator.validate_url(source_url) system = data['system'] self.validator.validate_sys(system) name = data['name'] app = self.repo.load_app(app_id) if app: app.set_source_url(source_url) app.set_system(system) app.set_name(name) return jsonify(app) abort(404)
def __init__(self, repo: AppRepository = None, validator: Validator = None): self.parser = reqparse.RequestParser() if repo: self.repo = repo else: self.repo = AppRepository() if validator: self.validator = validator else: self.validator = Validator() self.parser.add_argument('source_url', type=str, location='json') self.parser.add_argument('system', type=str, location='json') self.parser.add_argument('name', type=str, location='json') super(AppAPI, self).__init__()
def __init__(self, repo: AppRepository = None): if not repo: self.repo = AppRepository() else: self.repo = repo
# internal from src.installer.app_installer import ApplicationDownloader, ApplicationInstaller from src.installer.installer_api import AppInstallAPI from src.manager.app_api import AppRegisterAPI, AppAPI from src.manager.app_initializer import ApplicationInitializer from src.manager.app_list_api import AppListAPI from src.model.application import ApplicationEncoder, ApplicationDecoder from src.repository.app_repo import AppRepository from src.repository.repo_initializer import AppRepositoryInitializer # Global dependency injection ENCODER = ApplicationEncoder() DECODER = ApplicationDecoder() REPO = AppRepository(ENCODER, DECODER) DOWNLOADER = ApplicationDownloader() REPO_INITIALIZER = AppRepositoryInitializer(ENCODER, DECODER) APP_INITIALIZER = ApplicationInitializer(REPO_INITIALIZER) INSTALLER = ApplicationInstaller(REPO, DOWNLOADER, APP_INITIALIZER) def main(): try: port = os.environ['PORT'] except KeyError: port = 8080 app = Flask(__name__) api = Api(app) initialize_app(app)
def setUp(self, mock_encoder, mock_decoder): # ApplicationInstaller code to do setup self.repo = AppRepository(encoder=mock_encoder, decoder=mock_decoder, repo_name=':memory:')
class TestAppRepository(unittest.TestCase): @patch('src.model.application.ApplicationDecoder') @patch('src.model.application.ApplicationEncoder') def setUp(self, mock_encoder, mock_decoder): # ApplicationInstaller code to do setup self.repo = AppRepository(encoder=mock_encoder, decoder=mock_decoder, repo_name=':memory:') def test_load_app(self): with patch('src.repository.app_repo.sqlite3') as sqlite3: # given conn = sqlite3.Connection(':memory:') conn.row_factory = None sqlite3.connect = MagicMock(return_value=conn) conn.execute.return_value = '' # when self.repo.load_app('1') # then sqlite3.connect.assert_called_once_with(':memory:') conn.execute.assert_called_once_with( 'SELECT ID, NAME, SOURCE_URL, SYSTEM, INSTALLED, PACKAGE FROM APPS WHERE ID = ?', ('1', )) conn.close.assert_called_once() # cleanup conn.close() def test_load_apps(self): with patch('src.repository.app_repo.sqlite3') as sqlite3: # given conn = sqlite3.Connection(':memory:') conn.row_factory = None sqlite3.connect = MagicMock(return_value=conn) conn.execute.return_value = '' # when self.repo.load_apps() # then sqlite3.connect.assert_called_once_with(':memory:') conn.execute.assert_called_once_with( 'SELECT ID, NAME, SOURCE_URL, SYSTEM, INSTALLED FROM APPS') conn.close.assert_called_once() # cleanup conn.close() def test_remove_app(self): with patch('src.repository.app_repo.sqlite3') as sqlite3: # given conn = sqlite3.Connection(':memory:') conn.row_factory = None sqlite3.connect = MagicMock(return_value=conn) conn.execute.return_value = '' # when self.repo.remove_app('1') # then sqlite3.connect.assert_called_once_with(':memory:') conn.execute.assert_called_once_with( 'DELETE FROM APPS WHERE ID = ?', ('1', )) conn.close.assert_called_once() # cleanup conn.close() def test_remove_apps(self): with patch('src.repository.app_repo.sqlite3') as sqlite3: # given conn = sqlite3.Connection(':memory:') conn.row_factory = None sqlite3.connect = MagicMock(return_value=conn) conn.execute.return_value = '' # when self.repo.remove_apps() # then sqlite3.connect.assert_called_once_with(':memory:') conn.execute.assert_called_once_with('DELETE FROM APPS') conn.close.assert_called_once() # cleanup conn.close()