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

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

问题描述

信息

我已经使用嘉尚API,使我们的CMS我们自己的任务概述开始。我发现 github上的API,它可以帮助我很多与此有关。
正如我在更早问题所提到的,我想让所有任务特定的用户。我已经成功地做​​到这一点使用下面的code。

 公共职能的用户($ ID)
{
    如果(使用isset($ _ SERVER ['HTTP_X_REQUESTED_WITH'])及&放大器;
    ($ _ SERVER ['HTTP_X_REQUESTED_WITH'] =='XMLHtt prequest')){
        $这个 - >布局='阿贾克斯';
    }    $体式=新的体位(阵列(
        apiKey'=> XXXXXXXXXXXXXXXXXXXX
    ));    $结果= json_de code($ asana-> getTasksByFilter(阵列(
        '受让人'=> $ ID,
        工作空间=> XXXXXXXXXX
    )));    如果($ asana->响应code ='200'|| is_null($结果)!){
        抛出新的\\异常('错误尝试连接到Asana,响应code:'。$ asana->响应code,1);
    }    $任务=阵列();
    的foreach($结果>数据$任务){
        $结果= json_de code($ asana-> getTaskTags($任务 - > ID));
        $任务 - >标签= $ result->数据;
        $任务[] = $任务;
    }    $ USER = json_de code($ asana-> getUserInfo($ ID));    如果($ asana->!响应code ='200'|| is_null($用户)){
        抛出新的\\异常('错误尝试连接到Asana,响应code:'。$ asana->响应code,1);
    }    $这个 - >渲染(任务,阵列(
        '任务'=> $任务,
        '标题'=> 为任务'$用户可方式>&数据 - GT;名
    ));
}

问题

以上工作得很好,除了一件事。它不仅仅是一个启动的Windows Vista的机器(很慢:))慢。如果我包括标签,可能需要长达60秒之前,我得到的所有结果。如果我不包括标记它大约需要5秒钟这仍然是太长了。现在,我希望我不是第一个有史以来使用过体位API和一些你可能已经经历了过去同样的问题。


解决方案

API本身可以肯定是更快,我们必须围绕如何提高响应一些长期的计划,​​但在近至中期的API很可能将保持基本相同的速度。

诀窍不花大量的时间访问API一般以减少您请求的数量,只要求你需要的数据。有时,API客户端不使这个容易,我不熟悉PHP客户端具体,但我可以给如何做到这一点的工作,一般只用简单的HTTP查询的例子。

所以现在你在做伪code以下内容:

<?pre> GET /任务受让人= ...&放大器;工作区= ...
的foreach任务
  GET /task/.../tags
GET /用户/ ...

因此​​,如果用户有20个任务(和真正的用户通常有一个许多超过20个任务 - 如果你只关心不完整和任务,在过去,说,一周后,您可以使用 completed_since =&LT; D​​ATE_ONE_WEEK_AGO&GT; ),您做了22的请求。而且因为它是同步的,您在开始下一个之前等待每一个几秒钟,这些请求中的每一个。

幸运的是,API有一个名为参数?opt_fields ,允许你指定的确切的你需要的数据。例如:让我们假设,对于教任务,你真正想要的是知道任务ID,任务名称,它有标记和他们的名字。然后,您可以要求:

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

(每个资源收录总是带来了 ID 字段)

这将让你得到,在一个单一的HTTP请求,所有你后的数据。 (当然,用户查找仍然是独立的,但至少这只是1个额外的要求,而不是N)。有关opt_fields的更多信息,请查看文档的输入/输出选项

希望帮助!

Information

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
    ));
}

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.

解决方案

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.

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/...

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.

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

(Each resource included always brings its id field)

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.

Hope that helps!

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

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