def set_path(login: str, password: str, path: str, network: Network = None): is_network_not_passed = network is None if is_network_not_passed: network = Network(APIServerIp, APIServerPort) response = None is_authorized, error_message = authorization(login, password, network) if is_authorized: body = "folder=" + path request = HTTPBuilder().set_method("POST"). \ set_header(RequestType.CONTROL_TYPE, ControlRequests.SET_PATH). \ set_header("Content-Length", len(body)). \ build(body.encode("CP1251")) request = HTTPBuilder.insert_size_header_to_http_message(request) network.send(request) response = Responses.OK_RESPONSE.value if HTTPParser( network.receive()).get_body().decode() == Responses.OK_RESPONSE.value else None if is_network_not_passed: network.close() return response
def authorization(login: str, password: str, network: Network = None): is_network_not_passed = network is None if is_network_not_passed: network = Network(APIServerIp, APIServerPort) body = "login={}&password={}".format(login, password) request = HTTPBuilder().set_method("POST"). \ set_header(RequestType.ACCOUNT_TYPE, AccountRequests.AUTHORIZATION). \ set_header("Content-Length", str(len(body))). \ build(body.encode("CP1251")) request = HTTPBuilder.insert_size_header_to_http_message(request) network.send(request) response = HTTPParser(network.receive()) if is_network_not_passed: network.close() return response.get_header("Error") == "0", response.get_body().decode("CP1251")