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组织。openqa。硒。遥远的UnreachableBrowserException如何定义EXE路径?   java Camel AdviceWith不使用指定文件替换端点   基于字符串的java图像加载   Java中的启发式算法,计算8个谜题状态的线性冲突   java为什么不支持文件。probeContentType返回null   JPA@EntityListeners、@PrePersist和Spring@RepositoryEventHandler、@HandleBeforeSave之间的java差异   可能前缀的Java字符串到字符串[]   安装rJava | Makefile时发生java错误。全部:38:target’libjri的配方。所以他失败了   Java公共静态void main()   java如何覆盖txt文件中的某些单词   java如何获得循环中生成的字符值之和?   java Log4j创建另一个具有相同属性的appender   java如何在从Axis2 Web服务客户端通过代理服务器调用Web服务时设置代理设置?   在Windows上安装Elasticsearch时发生java错误   java如何向EditText组件添加TextChangedListener?