def get_form(self, form_id: str): request = Request.blank('/api/form/v1/form/' + form_id) request.authorization = self.auth.get_session_id() response = request.get_response() data = json.loads(response.body.decode()) return OperationResult.success(Form(**data)) if response.status_code == HTTPStatus.OK \ else OperationResult.fail(FailResult(code=response.status_code, **data))
def _get_permission(request: Request, help: str = ''): response = request.get_response() data = json.loads(response.body.decode()) if response.status_code not in [HTTPStatus.OK.value, HTTPStatus.NOT_FOUND.value]: logging.warning('Fail to get permissions' + help + ': ' + data.get('error_message', '')) return OperationResult.fail(FailResult(code=response.status_code, **data)) return OperationResult.success(Permission(**data))
def delete_answer(self, id: str): request = Request.blank('/api/form/v1/answer/' + id) request.authorization = self.auth.get_session_id() request.method = HTTPMethod.DELETE.value response = request.get_response() return OperationResult.success(True) if response.status_code == HTTPStatus.OK \ else OperationResult.fail(FailResult(code=response.status_code, **json.loads(response.body.decode())))
def _get_permissions(request: Request, help: str = ''): response = request.get_response() data = json.loads(response.body.decode()) if response.status_code != HTTPStatus.OK and response.status_code != HTTPStatus.NOT_FOUND: logging.warning('Fail to load permissions' + help + ': ' + data.get('error_message', '')) return OperationResult.fail(FailResult(code=response.status_code, **data)) data['items'] = list(map(lambda o: Permission(**o), data.get('items', []))) return OperationResult.success(ItemsResult(**data))
def create_binding(self, form_id: str, place_id: str): request = Request.blank('/api/form/v1/form/%s/place/%s' % (form_id, place_id)) request.authorization = self.auth.get_session_id() request.method = HTTPMethod.PUT.value response = request.get_response() return OperationResult.success(True) if response.status_code == HTTPStatus.OK \ else OperationResult.fail(FailResult(code=response.status_code, **json.loads(response.body.decode())))
def set_form(self, id: str, title: str, description: str, content: str): request = Request.blank('/api/form/v1/form/' + id) request.authorization = self.auth.get_session_id() request.method = HTTPMethod.POST.value data = {'title': title, 'description': description, 'content': content} request.body = json.dumps(data).encode() response = request.get_response() return OperationResult.success(True) if response.status_code == HTTPStatus.OK \ else OperationResult.fail(FailResult(code=response.status_code, **json.loads(response.body.decode())))
def create(self, login: str, password: str, email: str): request = Request.blank( '/api/users/v1/user?login=%s&password=%s$email=%s' % (login, password, email)) request.method = HTTPMethod.PUT.value request.body = password.encode() request.authorization = self.auth.get_session_id() response = request.get_response() data = json.loads(response.body.decode()) return OperationResult.success(data.get('data')) if response.status_code == HTTPStatus.OK \ else OperationResult.fail(FailResult(code=response.status_code, **data))
def get_answers(self, user_id: str, skip: int = 0, take: int = 50000): request = Request.blank('/api/form/v1/user/%s/answer?skip=%d&take=%d' % (user_id, skip, take)) request.authorization = self.auth.get_session_id() response = request.get_response() data = json.loads(response.body.decode()) if response.status_code != HTTPStatus.OK and response.status_code != HTTPStatus.NOT_FOUND: logging.warning('Fail to load answers for user ' + user_id + ': ' + data.get('error_message', '')) return OperationResult.fail( FailResult(code=response.status_code, **data)) data['items'] = list(map(lambda o: Answer(**o), data.get('items', []))) return OperationResult.success(ItemsResult(**data))
def authenticate_by_pass(self, login: str, password: str, client_addr: str = None): client_addr = client_addr if client_addr else AuthClient._get_ip() request = Request.blank( '/api/auth/v1/authenticate_by_pass?login=%s&client_addr=%s' % (login, client_addr)) request.body = password.encode() request.method = HTTPMethod.POST.value response = request.get_response() data = json.loads(response.body.decode()) return OperationResult.success(data.get('sid')) if response.status_code == HTTPStatus.OK \ else OperationResult.fail(FailResult(code=response.status_code, **data))
def create_answer(self, respondent_id: str, form_id: str, place_id: str, answer: str = '{}', id: str = None): id = id if id else str(uuid.uuid4()) request = Request.blank('/api/form/v1/answer/' + id) request.authorization = self.auth.get_session_id() request.method = HTTPMethod.PUT.value data = Answer(id, respondent_id, form_id, place_id, answer) request.body = json.dumps(data.__dict__).encode() response = request.get_response() return OperationResult.success(True) if response.status_code == HTTPStatus.OK \ else OperationResult.fail(FailResult(code=response.status_code, **json.loads(response.body.decode())))
def create_form(self, creator: str, title: str = None, description: str = None, content: str = '{}', id: str = None): id = id if not None else str(uuid.uuid4()) request = Request.blank('/api/form/v1/form/' + id) request.authorization = self.auth.get_session_id() request.method = HTTPMethod.PUT.value form = Form(id, creator, title if title is not None else 'Форма ' + id, description if description is not None else '', content if content is not None else '{}') request.body = json.dumps(form.__dict__).encode() response = request.get_response() return OperationResult.success(True) if response.status_code == HTTPStatus.OK \ else OperationResult.fail(FailResult(code=response.status_code, **json.loads(response.body.decode())))
def get_session(self, auth_sid: str): request = Request.blank('/api/auth/v1/session/' + auth_sid) response = request.get_response() data = json.loads(response.body.decode()) return OperationResult.success(SessionState(**data)) if response.status_code == HTTPStatus.OK \ else OperationResult.fail(FailResult(code=response.status_code))