首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何实现Apify webhooks?

如何实现Apify webhooks?
EN

Stack Overflow用户
提问于 2019-09-10 20:53:08
回答 1查看 782关注 0票数 2

需要帮助来实现Apify webhook。完成一项任务需要一些时间。我想添加一个Apify webhook,它将运行另一个任务,但不确定如何做到这一点。

代码语言:javascript
复制
$.ajax({
  url : 'https://api.apify.com/v2/actor-tasks/XXXXXXX/runs?token=XXXXXXXX&waitForFinish=120',  
  method : 'POST',
  contentType: 'application/json',
  dataType: 'json',
  data : JSON.stringify ({
      "queries" : "Outreach link building"
  }),
  success:function(result) {
    console.log(result);
  } 
});

然后,webhook将调用以下任务:

代码语言:javascript
复制
$.ajax({
   url : `https://api.apify.com/v2/datasets/${datasetId}/items?format=json`,  
   method : 'GET',
   contentType: 'application/json; charset=utf-8',
   success:function(response) {
     console.log(response); // Items from dataset
   } 

 });

顺便说一句,如果我想实现我需要的方式是错误的,请让我知道你的建议。

EN

回答 1

Stack Overflow用户

发布于 2019-09-10 21:13:42

如果你根本不想在你的应用程序下面使用服务器,你想从客户端(前端)管理一切,并且同步运行的5分钟最大延迟对你来说太短了,你可以使用轮询。您只需调用run端点几次,直到它返回状态为succeeded。

但是,如果可以使用waitForFinish,只需执行此操作,然后发送第二个调用以获取

代码语言:javascript
复制
success:function(result) {
    console.log(result);
   // instead of this log, you need to put your second call here and use the `result.data.defaultDatasetId` as ID of the dataset
  } 

如果您必须等待超过300秒,则需要使用轮询。但我真的会避免这种情况,或者不使用带回调的ajax,而是使用更现代的fetch

代码语言:javascript
复制
$.ajax({
  url : 'https://api.apify.com/v2/actor-tasks/XXXXXXX/runs?token=XXXXXXXX&waitForFinish=120',  
  method : 'POST',
  contentType: 'application/json',
  dataType: 'json',
  data : JSON.stringify ({
      "queries" : "Outreach link building"
  }),
  success:function(result) {
    console.log(result);
    // Here instead of just logging, you get the run object back with its `status`. You also find an `id` there and you can periodically poll the run with this endpoint
https://apify.com/docs/api/v2#/reference/actors/run-object/get-run

  } 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57871260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档