Looker API 3.1版

looker-sdk的Python项目详细描述


lookersdkforpython提供了与 looker api在looker服务器上可用。图书馆需要Python3.7+ 并使用输入模块进行注释。

disclaimer:这是looker sdk的一个实验版本,使用 looker开发的一种新的代码生成器。你应该期待一些事情 只是不起作用,并预见到对sdk源代码的巨大更改,直到 正式测试开始。

示例项目设置

在这些说明中,我们将使用pyenv 安装python3.7和 pipenv 管理项目依赖关系。下面是如何在Mac上安装它们的方法

brew update && brew install pyenv && brew install pipenv

创建项目目录

mkdir looker-sdk-example

安装python3.7并将其用于此目录

cd looker-sdk-example/
pyenv install 3.7.4
pyenv local3.7.4

使用pipenv安装looker_sdk

$ pipenv install --pre looker_sdk

配置sdk

为了配置sdk客户端,创建一个“looker.ini”文件来引用 在client.setup()期间

示例文件:

[Looker]
# API version is required
api_version=3.1
# Base URL for API. Do not include /api/* in the url
base_url=https://self-signed.looker.com:19999
# API 3 client id
client_id=YourClientID
# API 3 client secret
client_secret=YourClientSecret
# Set to false if testing locally against self-signed certs. Otherwise leave True
verify_ssl=True

代码示例

将下面的代码块复制到example.py

fromlooker_sdkimportclient,models,error# client calls will now automatically authenticate using the# api3credentials specified in 'looker.ini'looker_client=client.setup("looker.ini")looker_api_user=looker_client.me()# models can be passed named parameters to the constructornew_user=models.WriteUser(first_name="John",last_name="Doe")# as well as have fields set on the instancenew_user.is_disabled=Truenew_user.locale="fr"# create the user with the clientcreated_user=looker_client.create_user(new_user)print(f"Created user({created_user.id}): "f"{created_user.display_name} "f"locale({created_user.locale})")# Updating the user: change first_name and explicitly nullify# locale so that it defaults to looker system localeupdate_user=models.WriteUser(first_name="Jane",locale=models.EXPLICIT_NULL# do not use None)# update the user with the clientuser_id=created_user.idupdated_user=looker_client.update_user(user_id,body=update_user)print(f"Updated user({user_id}): {updated_user.display_name} "f"locale({updated_user.locale})")# perform API calls on behalf of the user: "sudo"try:print(f"Sudo as {user_id}")looker_client.login_user(user_id)excepterror.SDKError:print(f"Oops, we need to enable user({user_id}) first")looker_client.update_user(user_id,body=models.WriteUser(is_disabled=False))looker_client.login_user(user_id)sudo_user=looker_client.me()assertsudo_user.id==user_idassertsudo_user.id!=looker_api_user.id# logout to switch back to authenticating per 'looker.ini'looker_client.logout()print(f"Ending sudo({user_id}) session")assertlooker_client.me().id==looker_api_user.id# "sudo" using a context managerwithlooker_client.login_user(user_id):assertlooker_client.me().id==user_id# exiting context manager is the same as# calling looker_client.logout()assertlooker_client.me().id==looker_api_user.id# cleanuplooker_client.delete_user(user_id)print(f"Removed user({user_id})")

您可以运行上面的示例代码,但是请注意,它实际上会创建 删除Looker实例中的用户。

pipenv run python example.py

如果您在运行 对于具有自签名证书的实例,这将清除输出:

PYTHONWARNINGS=ignore pipenv run python example.py

关于静态类型检查的说明

所有客户机调用都用基本类型和模型类型进行了注释。 许多客户机调用接受一个限制json响应的字段参数 从api到指定字段。因此,上的所有属性 模型都被输入为可选的[]。结果是静态代码分析 (例如mypy)会抱怨 如果试图在需要的位置使用模型实例中的字段 该值不是可选的。从上面的示例中

created_user=looker_client.create_user(new_user)user_id=created_user.id# mypy error: Argument "user_id" to "update_user" of "LookerSDK"# has incompatible type "Optional[int]"; expected "int"looker_client.update_user(user_id,...)

这是因为created\u user.id具有类型可选的[int]但我们需要使用 它位于update_user()调用中,该调用的注释如下:

defupdate_user(self,user_id:int,# note: not Optional[int]body:models.WriteUser,fields:Optional[str]=None,)->models.User:

we知道created\u user.id是一个in t(我们没有传入字段 从响应中排除id的参数。但是,我的 不是这样,我们必须用下列方法之一来指导它

# assert about the typeassertisinstance(user_id,int)# or castfromtypingimportcastuser_id=cast(created_user.id,int)

现在mypy对更新用户(用户id,…)很满意

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java对多个for循环使用一个整数有什么好处?   要求的Java枚举错误返回类型   使用AOP的Java Spring MVC CSRF令牌   PHP ZF2中的java 安卓登录按钮和远程web登录   java将双精度转换为整数转换为字符串,并在TxtField CodeNameOne中输出   java代码不断向后向数组中输入值   如何使用Java获取git信息?   当使用DateUtils类的parseDateStructive()方法时,java异常无法解析日期   java执行示例Flink kafka consummer   java触发REST调用时无法解释某些汉字   java错误:不兼容的类型:<null>如果使用安卓 room(!\u cursor.isNull(null))则无法转换为int   java Spring引导构建正常,但Heroku部署失败   我无法使用ORMLite库在java(安卓)中执行“WITH RECURSIVE”子句   java JavaFX图像加载错误