理智有可能吗?计划的对Sanity的外部API调用

问题描述

这是我想完成的事情?

1-每晚每晚对新闻API进行提取调用 2-拉最新的头条新闻 3-导入理智...

理智让您做到这一点吗?它有cron工作吗?

谢谢

解决方法

Sanity不提供任何本机方式来执行此操作,但是您可以使用外部无服务器提供程序来创建cron作业,该提供程序将提供您要查找的功能

Firebase scheduled functions是提供者的一个示例,但是还有更多选择。我建议您看看Serverless framework examples

弄清楚将要使用的内容后,然后使用Sanity's HTTP APIone of their API clients(对于Javascript为@sanity/client)向其中添加数据。您需要一个令牌来完成此操作,因为POST请求受身份验证墙保护(请查看其Authentication docs)。

以下是Javascript中的示例:

// For Javascript - using the @sanity/client npm package
const allHeadlines = await pullHeadlines(/* ... */);

// Create a single transaction that will handle
// the creation of all headline documents
const transaction = sanityClient.transaction();
for (const headline of allHeadlines) {
  transaction = transaction.create({ title: headline.title,...headline });
}

transaction
  .commit()
  .then((res) => {
    console.log("All headlines created");
    // Finish the cron job here
  })
  .catch((error) => {
    console.error("Couldn't create the headline documents",error);
    // Maybe retry it here?
  });

希望有帮助,帖木儿ur

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...