Podio:如何在Python 3中使用Item.update方法?

时间:2017-06-24 11:56:36

标签: python podio

Podio API for Python没有示例代码,但这是为Ruby编写的示例代码:

Podio::Item.update(210606, {
  :fields => {
    'title' => 'The API documentation is much more funny',
    'business_value' => { :value => 20000, :currency => 'EUR' },
    'due_date' => { :start => '2011-05-06 11:27:20', :end => 
5.days.from_now.to_s(:db) }
  }
})

我不能为我的生活弄清楚如何将其翻译成Python 3.我尝试过使用词典,在列表中使用词典,使用字段ID和名称来引用字段等。但它永远不会实际上更新了什么

这是我将上述内容转换为Python代码的失败尝试(由于我的'Bugs(API示例)应用程序中的字段与示例代码中的字段不同,因此使用了不同的字段):

newValues = {'fields':{'title': "This is my title",'description_of_problem': 
"the not work"}}

try:
    podio.Item.update(629783395, newValues['fields'])
    print('updating was successful')
except:
    print('updating was not successful')

podio为:

podio = api.OAuthClient(
    client_id,
    client_secret,
    username,
    password,    
)

我的代码中的'fields'部分确实没有任何意义,但我无法弄清楚还有什么与Ruby代码的那部分有关,我怀疑这是问题所在。该程序始终打印'更新成功',就像成功调用Item.update方法一样,但正如我所说它实际上并没有更新Podio中的任何内容。任何人都可以看到什么是错的吗?

1 个答案:

答案 0 :(得分:1)

我只需关注Item update API,然后传入符合请求部分的词典:

{
  "revision": The revision of the item that is being updated. This is optional,
  "external_id": The new external_id of the item,
  "fields": The values for each field, see the create item operation for details,
  "file_ids": The list of attachments,
  "tags": The list of tags,
  "reminder": Optional reminder on this task
  {
    "remind_delta": Minutes (integer) to remind before the due date
  },
  "recurrence": The recurrence for the task, if any,
  {
    "name": The name of the recurrence, "weekly", "monthly" or "yearly",
    "config": The configuration for the recurrence, depends on the type
    {
      "days": List of weekdays ("monday", "tuesday", etc) (for "weekly"),
      "repeat_on": When to repeat, "day_of_week" or "day_of_month" (for "monthly")
    },
    "step": The step size, 1 or more,
    "until": The latest date the recurrence should take place
  },
  "linked_account_id": The linked account to use for meetings,
  "ref" The reference of the item
  {
    "type": The type of reference,
    "id": The id of the reference
  }
}

文档进一步指向item creation API for further examples。请注意该对象在最外层映射中具有"fields"键的方式。

所有Ruby文档都构建了映射为Ruby hash(在Python中,dict),其中包含需要更新的条目; :field是一个不可变的字符串(称为符号),它定义该哈希中指向嵌套哈希的键。 Python implementation for the update method只是将该字典转换为JSON帖子正文。

将Ruby代码直接翻译为Python是:

from datetime import datetime, timedelta

podio.item.update(210606, {
  'fields': {
      'title': 'The API documentation is much more funny',
          'business_value': {'value': 20000, 'currency': 'EUR'},
      'due_date': {
          'start': '2011-05-06 11:27:20',
          'end': (datetime.now() + timedelta(days=5)).strftime('%Y-%m-%d %H:%M:%S')}
    }
})

在您的情况下,您做错了不包括最外层词典中的'fields'键;你解开了最外面的字典,只在'fields'下发布了嵌套字典。相反,包括外部词典:

newValues = {
    'fields': {
        'title': "This is my title",
        'description_of_problem': "the not work"
    }
}

podio.Item.update(629783395, newValues)