使用 Asana API 响应缓慢 [英] Slow responses using the Asana API

查看:22
本文介绍了使用 Asana API 响应缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用 Asana API 在我们的 CMS 中制作我们自己的任务概览.我在 github 上找到了一个 API,它对我有很大帮助.正如我在之前的问题中提到的,我想要获取某个用户的所有任务.我已经使用下面的代码设法做到了这一点.

I've started using the Asana API to make our own task overview in our CMS. I found an API on github which helps me a great deal with this. As I've mentioned in an earlier question, I wanted to get all tasks for a certain user. I've managed to do this using the code below.

public function user($id)
{
    if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
    ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')) {
        $this->layout = 'ajax';
    }

    $asana = new Asana(array(
        'apiKey' => 'xxxxxxxxxxxxxxxxxxxx'
    ));

    $results = json_decode($asana->getTasksByFilter(array(
        'assignee' => $id,
        'workspace' => 'xxxxxxxxxx'
    )));

    if ($asana->responseCode != '200' || is_null($results)) {
        throw new \Exception('Error while trying to connect to Asana, response code: ' . $asana->responseCode, 1);
    }

    $tasks = array();
    foreach ($results->data as $task) {
        $result = json_decode($asana->getTaskTags($task->id));
        $task->tags = $result->data;
        $tasks[] = $task;
    }

    $user = json_decode($asana->getUserInfo($id));

    if ($asana->responseCode != '200' || is_null($user)) {
        throw new \Exception('Error while trying to connect to Asana, response code: ' . $asana->responseCode, 1);
    }

    $this->render("tasks", array(
        'tasks' => $tasks,
        'title' => 'Tasks for '.$user->data->name
    ));
}

问题

以上工作正常,除了一件事.它比启动的 Windows Vista 机器慢(非常慢:)).如果我包含标签,可能需要 60 秒才能获得所有结果.如果我不包括标签,它需要大约 5 秒,这仍然太长了.现在,我希望我不是第一个使用 Asana API 的人,并且你们中的一些人过去可能遇到过同样的问题.

The problem

The above works fine, except for one thing. It is slower than a booting Windows Vista machine (very slow :) ). If I include the tags, it can take up to 60 seconds before I get all results. If I do not include the tags it takes about 5 seconds which is still way too long. Now, I hope I am not the first one ever to have used the Asana API and that some of you might have experienced the same problem in the past.

推荐答案

API 本身肯定可以更快,我们有一些关于如何提高响应能力的长期计划,但在近期到中期API 可能会保持相同的基本速度.

The API itself could definitely be faster, and we have some long-term plans around how to improve responsiveness, but in the near-to-mid-term the API is probably going to remain the same basic speed.

不花大量时间访问 API 的诀窍通常是减少您发出的请求数量,只请求您需要的数据.有时,API 客户端并不容易,而且我对 PHP 客户端并不特别熟悉,但我可以举一个例子,说明这在一般情况下如何仅适用于普通的 HTTP 查询.

The trick to not spending a lot of time accessing the API is generally to reduce the number of requests you make and only request the data you need. Sometimes, API clients don't make this easy, and I'm not familiar with the PHP client specifically, but I can give an example of how this would work in general with just the plain HTTP queries.

所以现在您正在用伪代码执行以下操作:

So right now you're doing the following in pseudocode:

GET /tasks?assignee=...&workspace=...
foreach task
  GET /task/.../tags
GET /users/...

因此,如果用户有 20 个任务(而真实用户通常有很多超过 20 个任务 - 如果您只关心未完成的任务和在最后一周完成的任务,您可以使用?completed_since=),您已经提出了 22 个请求.而且因为它是同步的,所以在开始下一个请求之前,您会为这些请求中的每一个等待几秒钟.

So if the user has 20 tasks (and real users typically have a lot more than 20 tasks - if you only care about incomplete and tasks completed in the last, say, week, you could use ?completed_since=<DATE_ONE_WEEK_AGO>), you've made 22 requests. And because it's synchronous, you wait a few seconds for each and every one of those requests before you start the next one.

幸运的是,该 API 有一个名为 ?opt_fields 的参数,允许您指定所需的确切数据.例如:假设对于教学任务,您真正想要的是知道任务 ID、任务名称、它具有的标签及其名称.然后您可以请求:

Fortunately, the API has a parameter called ?opt_fields that allows you to specify the exact data you need. For example: let's suppose that for teach task, all you really want is to know the task ID, the task name, the tags it has and their names. You could then request:

GET /tasks?assignee=...&workspace=...&opt_fields=name,tags.name

(包含的每个资源总是带有它的 id 字段)

(Each resource included always brings its id field)

这将允许您在单个 HTTP 请求中获取您想要的所有数据.(好吧,用户查找仍然是独立的,但至少这只是 1 个额外请求而不是 N).有关 opt_fields 的更多信息,请查看关于输入/输出选项的文档.

This would allow you to get, in a single HTTP request, all the data you're after. (Well, the user lookup is still separate, but at least that's just 1 extra request instead of N). For more information on opt_fields, check out the documentation on Input/Output Options.

希望有帮助!

这篇关于使用 Asana API 响应缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆