def count_unread_statuses(owner, inbox_type="default"): params = { "inboxType": inbox_type, "owner": utils.encode(owner), } content = client.get("/subscribe/statuses/count", params).json() return StatusesCount(total=content["total"], unread=content["unread"])
def count_unread_statuses(owner, inbox_type='default'): params = { 'inboxType': inbox_type, 'owner': utils.encode(owner), } content = client.get('/subscribe/statuses/count', params).json() return StatusesCount(total=content['total'], unread=content['unread'])
def fetch(self): response = client.get('/files/{0}'.format(self.id)) content = response.json() self._name = content.get('name') self.id = content.get('objectId') self._url = content.get('url') self._type = content.get('mime_type') self._metadata = content.get('metaData')
def _find(cls, query_params): # type: (dict) -> Generator[Message, None, None] content = client.get('/rtm/messages/history', params=query_params).json() for data in content: msg = cls() msg._update_data(data) yield msg
def fetch(self): response = client.get('/files/{0}'.format(self.id)) content = utils.response_to_json(response) self._name = content.get('name') self.id = content.get('objectId') self._url = content.get('url') self._type = content.get('mime_type') self._metadata = content.get('metaData')
def fetch(self): """ 从服务器获取当前对象所有的值,如果与本地值不同,将会覆盖本地的值。 :return: 当前对象 """ response = client.get('/classes/{0}/{1}'.format(self._class_name, self.id), {}) self._update_data(response.json())
def fetch(self): """ 从服务器获取当前对象所有的值,如果与本地值不同,将会覆盖本地的值。 :return: 当前对象 """ response = client.get('/classes/{0}/{1}'.format(self._class_name, self.id), {}) result = self.parse(utils.response_to_json(response), response.status_code) self._finish_fetch(result, True)
def become(cls, session_token): response = client.get("/users/me", params={"session_token": session_token}) content = utils.response_to_json(response) user = cls() server_data = user.parse(content, response.status_code) user._finish_fetch(server_data, True) user._handle_save_result(True) if "smsCode" not in server_data: user.attributes.pop("smsCode", None) return user
def count(self): """ 返回满足查询条件的对象的数量。 :rtype: int """ params = self.dump() params["limit"] = 0 params["count"] = 1 response = client.get("/classes/{0}".format(self._query_class._class_name), params) return utils.response_to_json(response)["count"]
def become(cls, session_token): response = client.get('/users/me', params={'session_token': session_token}) content = response.json() user = cls() server_data = user.parse(content, response.status_code) user._finish_fetch(server_data, True) user._handle_save_result(True) if 'smsCode' not in server_data: user._attributes.pop('smsCode', None) return user
def count(self): """ 返回满足查询条件的对象的数量。 :rtype: int """ params = self.dump() params['limit'] = 0 params['count'] = 1 response = client.get('/classes/{0}'.format(self._query_class._class_name), params) return response.json()['count']
def count(self): """ 返回满足查询条件的对象的数量。 :rtype: int """ params = self.dump() params['limit'] = 0 params['count'] = 1 response = client.get( '/classes/{0}'.format(self._query_class._class_name), params) return utils.response_to_json(response)['count']
def __iter__(self): while True: content = client.get( '/scan/classes/{}'.format(self._query_class._class_name), self._params).json() for result in content['results']: obj = self._query_class() obj._update_data(result) yield obj if not content.get('cursor'): break self._params['cursor'] = content['cursor']
def test_redirect_region(): if client.REGION == 'US': # US region server doesn't support app router now return # setup old_app_router = client.app_router client.app_router = AppRouter('test_app_id') requests_intercept.install() def fake_app_router(environ, start_response): assert environ['PATH_INFO'] == '/1/route' start_response('200 OK', [('Content-Type', 'application/json')]) return [ json.dumps({ 'api_server': 'fake-redirect-server', 'ttl': 3600, }).encode('utf-8') ] host, port = 'app-router.leancloud.cn', 443 add_wsgi_intercept(host, port, lambda: fake_app_router) def fake_redirect_server(environ, start_response): start_response('307', [('Content-Type', 'application/json')]) return [ json.dumps({ 'api_server': 'fake-api-server', 'ttl': 3600, }).encode('utf-8') ] host, port = 'fake-redirect-server', 443 add_wsgi_intercept(host, port, lambda: fake_redirect_server) def fake_api_server(environ, start_response): start_response('200', [('Content-Type', 'application/json')]) return [json.dumps({ 'result': 42, }).encode('utf-8')] host, port = 'fake-api-server', 443 add_wsgi_intercept(host, port, lambda: fake_api_server) # test assert client.get('/redirectme').json()['result'] == 42 # teardown client.app_router = old_app_router requests_intercept.uninstall()
def become(cls, session_token): """ 通过 session token 获取用户对象 :param session_token: 用户的 session token :return: leancloud.User """ response = client.get('/users/me', params={'session_token': session_token}) content = response.json() user = cls() user._update_data(content) user._handle_save_result(True) if 'smsCode' not in content: user._attributes.pop('smsCode', None) return user
def find(self): """ 根据查询条件,获取包含所有满足条件的对象。 :rtype: list """ content = utils.response_to_json(client.get("/classes/{0}".format(self._query_class._class_name), self.dump())) objs = [] for result in content["results"]: obj = self._new_object() obj._finish_fetch(self._process_result(result), True) objs.append(obj) return objs
def find(self): """ 根据查询条件,获取包含所有满足条件的对象。 :rtype: list """ content = client.get('/classes/{0}'.format(self._query_class._class_name), self.dump()).json() objs = [] for result in content['results']: obj = self._new_object() obj._finish_fetch(self._process_result(result), True) objs.append(obj) return objs
def login(self, username=None, password=None): """ 登陆用户。如果用户名和密码正确,服务器会返回用户的 sessionToken 。 """ if username: self.set('username', username) if password: self.set('password', password) response = client.get('/login', params=self.dump()) content = response.json() server_data = self.parse(content, response.status_code) self._finish_fetch(server_data, False) self._handle_save_result(True) if 'smsCode' not in server_data: self.attributes.pop('smsCode', None)
def login(self, username=None, password=None): """ 登陆用户。如果用户名和密码正确,服务器会返回用户的 sessionToken 。 """ if username: self.set('username', username) if password: self.set('password', password) response = client.get('/login', params=self.dump()) content = utils.response_to_json(response) server_data = self.parse(content, response.status_code) self._finish_fetch(server_data, False) self._handle_save_result(True) if 'smsCode' not in server_data: self.attributes.pop('smsCode', None)
def is_authenticated(self): ''' 判断当前用户对象是否已登录。 会先检查此用户对象上是否有 `session_token`,如果有的话,会继续请求服务器验证 `session_token` 是否合法。 ''' session_token = self.get_session_token() if not session_token: return False try: response = client.get('/users/me', params={'session_token': session_token}) except LeanCloudError as e: if e.code == 211: return False else: raise return response.status_code == 200
def first(self): """ 根据查询获取最多一个对象。 :return: 查询结果 :rtype: Object :raise: LeanCloudError """ params = self.dump() params["limit"] = 1 content = utils.response_to_json(client.get("/classes/{0}".format(self._query_class._class_name), params)) results = content["results"] if not results: raise LeanCloudError(101, "Object not found") obj = self._new_object() obj._finish_fetch(self._process_result(results[0]), True) return obj
def first(self): """ 根据查询获取最多一个对象。 :return: 查询结果 :rtype: Object :raise: LeanCloudError """ params = self.dump() params['limit'] = 1 content = client.get('/classes/{0}'.format(self._query_class._class_name), params).json() results = content['results'] if not results: raise LeanCloudError(101, 'Object not found') obj = self._new_object() obj._finish_fetch(self._process_result(results[0]), True) return obj
def test_redirect_region(): if client.REGION == 'US': # US region server doesn't support app router now return # setup old_app_router = client.app_router client.app_router = AppRouter('test_app_id') requests_intercept.install() def fake_app_router(environ, start_response): assert environ['PATH_INFO'] == '/1/route' start_response('200 OK', [('Content-Type', 'application/json')]) return [json.dumps({ 'api_server': 'fake-redirect-server', 'ttl': 3600, }).encode('utf-8')] host, port = 'app-router.leancloud.cn', 443 add_wsgi_intercept(host, port, lambda: fake_app_router) def fake_redirect_server(environ, start_response): start_response('307', [('Content-Type', 'application/json')]) return [json.dumps({ 'api_server': 'fake-api-server', 'ttl': 3600, }).encode('utf-8')] host, port = 'fake-redirect-server', 443 add_wsgi_intercept(host, port, lambda: fake_redirect_server) def fake_api_server(environ, start_response): start_response('200', [('Content-Type', 'application/json')]) return [json.dumps({ 'result': 42, }).encode('utf-8')] host, port = 'fake-api-server', 443 add_wsgi_intercept(host, port, lambda: fake_api_server) # test assert client.get('/redirectme').json()['result'] == 42 # teardown client.app_router = old_app_router requests_intercept.uninstall()
def fetch(self, select=None, include=None): """ 从服务器获取当前对象所有的值,如果与本地值不同,将会覆盖本地的值。 :return: 当前对象 """ data = {} if select: if not isinstance(select, (list, tuple)): raise TypeError('select parameter must be a list or a tuple') data['keys'] = ','.join(select) if include: if not isinstance(include, (list, tuple)): raise TypeError('include parameter must be a list or a tuple') data['include'] = ','.join(include) response = client.get('/classes/{0}/{1}'.format(self._class_name, self.id), data) self._update_data(response.json())
def find(self): """ 根据查询条件,获取包含所有满足条件的对象。 :rtype: list """ content = client.get( '/classes/{0}'.format(self._query_class._class_name), self.dump()).json() objs = [] for result in content['results']: obj = self._new_object() obj._update_data(self._process_result(result)) objs.append(obj) return objs
def find(self): """ 根据查询条件,获取包含所有满足条件的对象。 :rtype: list """ content = utils.response_to_json( client.get('/classes/{0}'.format(self._query_class._class_name), self.dump())) objs = [] for result in content['results']: obj = self._new_object() obj._finish_fetch(self._process_result(result), True) objs.append(obj) return objs
def first(self): """ 根据查询获取最多一个对象。 :return: 查询结果 :rtype: Object :raise: LeanCloudError """ params = self.dump() params['limit'] = 1 content = utils.response_to_json( client.get('/classes/{0}'.format(self._query_class._class_name), params)) results = content['results'] if not results: raise LeanCloudError(101, 'Object not found') obj = self._new_object() obj._finish_fetch(self._process_result(results[0]), True) return obj
def first(self): """ 根据查询获取最多一个对象。 :return: 查询结果 :rtype: Object :raise: LeanCloudError """ params = self.dump() params['limit'] = 1 content = client.get( '/classes/{0}'.format(self._query_class._class_name), params).json() results = content['results'] if not results: raise LeanCloudError(101, 'Object not found') obj = self._new_object() obj._update_data(self._process_result(results[0])) return obj
def do_cloud_query(cls, cql, *pvalues): """ 使用 CQL 来构造查询。CQL 语法参考 `这里 <https://cn.avoscloud.com/docs/cql_guide.html>`_。 :param cql: CQL 语句 :param pvalues: 查询参数 :rtype: CQLResult """ params = {'cql': cql} if len(pvalues) == 1 and isinstance(pvalues[0], (tuple, list)): pvalues = json.dumps(pvalues[0]) if len(pvalues) > 0: params['pvalues'] = json.dumps(pvalues) content = client.get('/cloudQuery', params).json() objs = [] query = cls(content['className']) for result in content['results']: obj = query._new_object() obj._finish_fetch(query._process_result(result), True) objs.append(obj) return CQLResult(objs, content.get('count'), content.get('className'))
def do_cloud_query(cls, cql, *pvalues): """ 使用 CQL 来构造查询。CQL 语法参考 `这里 <https://cn.avoscloud.com/docs/cql_guide.html>`_。 :param cql: CQL 语句 :param pvalues: 查询参数 :rtype: CQLResult """ params = {'cql': cql} if len(pvalues) == 1 and isinstance(pvalues[0], (tuple, list)): pvalues = json.dumps(pvalues[0]) if len(pvalues) > 0: params['pvalues'] = json.dumps(pvalues) content = utils.response_to_json(client.get('/cloudQuery', params)) objs = [] query = Query(content['className']) for result in content['results']: obj = query._new_object() obj._finish_fetch(query._process_result(result), True) objs.append(obj) return CQLResult(objs, content.get('count'), content.get('className'))
def do_cloud_query(cls, cql, *pvalues): """ 使用 CQL 来构造查询。CQL 语法参考 `这里 <https://cn.avoscloud.com/docs/cql_guide.html>`_。 :param cql: CQL 语句 :param pvalues: 查询参数 :rtype: CQLResult """ params = {"cql": cql} if len(pvalues) == 1 and isinstance(pvalues[0], (tuple, list)): pvalues = json.dumps(pvalues[0]) if len(pvalues) > 0: params["pvalues"] = json.dumps(pvalues) content = utils.response_to_json(client.get("/cloudQuery", params)) objs = [] query = Query(content["className"]) for result in content["results"]: obj = query._new_object() obj._finish_fetch(query._process_result(result), True) objs.append(obj) return CQLResult(objs, content.get("count"), content.get("className"))
def do_cloud_query(cls, cql, *pvalues): """ 使用 CQL 来构造查询。CQL 语法参考 `这里 <https://cn.avoscloud.com/docs/cql_guide.html>`_。 :param cql: CQL 语句 :param pvalues: 查询参数 :rtype: CQLResult """ params = {"cql": cql} if len(pvalues) == 1 and isinstance(pvalues[0], (tuple, list)): pvalues = json.dumps(pvalues[0]) if len(pvalues) > 0: params["pvalues"] = json.dumps(pvalues) content = client.get("/cloudQuery", params).json() objs = [] query = cls(content["className"]) for result in content["results"]: obj = query._new_object() obj._update_data(query._process_result(result)) objs.append(obj) return CQLResult(objs, content.get("count"), content.get("className"))
def _do_request(self, params): return client.get('/subscribe/statuses', params).json()
def fetch(self, *args, **kwargs): """同步服务器的 Notification 数据 """ response = client.get('/tables/Notifications/{0}'.format(self.id)) self._update_data(response.json())
def fetch(self): response = client.get('/files/{0}'.format(self.id)) content = response.json() self._update_data(content)
def _do_request(self, params): return client.get('/classes/{0}'.format(self._query_class._class_name), params).json()