如何从代码存储库访问Foundry API?

问题描述

例如使用Python转换从代码存储库中访问内部Foundry API的正确方法是什么?

解决方法

默认情况下,并不是每个代码存储库都提供此功能,因此您应要求平台管理员在存储库的基础上启用此功能。

一旦存储库被列入白名单,您就可以使用代码存储库中的python请求库来构建API调用,并从transform_context获取用户令牌。这是一个代码段作为示例:

from transforms.api import transform_df,Output
import requests

@transform_df(
Output("/your/output/table"),)
def my_compute_function(ctx):

  # Construct the URL for the GET request
  url = "https://fourndy_url.com/service_url/api/..."


  # Authentication
  bearer_token = ctx.auth_header
  header = {'Authorization': bearer_token,'Content-Type': 'application/json'}


  # Get the response
  response = requests.get(url,headers=header).json()
,

要扩展上面@icharbon的帖子-我建议在python timeout调用上设置requests属性。没有这个,挂起的HTTP请求可能导致您的pyspark作业挂起。

from transforms.api import transform_df,Output
import requests


@transform_df(
Output("/your/output/table"),headers=header,timeout=10.0).json()

您可以了解有关超时参数和其他可选参数here的更多信息。