-1

There is no example code for the Podio API for Python but this is the example code written for 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) }
  }
})

I can't for the life of me figure out how to translate this to Python 3. I've tried using dictionaries, dicts inside lists, referencing the field with both their field-id and their names etc. But it never actually updates anything.

This is my failed attempt at translating the above to Python code (with different fields since the fields in my 'Bugs (API example) app' aren't the same as in the example code):

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')

With podio being:

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

The 'fields' part in my code doesn't make any sense really, but I couldn't figure out what else to do with that part of the Ruby code, I suspect that is the issue. The program always prints 'updating was successful' as if the Item.update method was successfully called, but as I said it doesn't actually update anything in Podio. Can anyone see what's wrong?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Chisq
  • 117
  • 1
  • 9

1 Answers1

1

I'd just follow the Item update API, and pass in a dictionary that matches the request section there:

{
  "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
  }
}

The documentation further points to the item creation API for further examples. Note how that object has a "fields" key in the outermost mapping.

All the Ruby documentation does is build that mapping as a Ruby hash (in Python, a dict) with the entries that need updating; :field is an immutable string (called a symbol) that defines a key in that hash pointing to a nested hash. The Python implementation for the update method just converts that dictionary to a JSON post body.

A direct translation of the Ruby code to Python is:

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

What you did wrong in your case is not include the 'fields' key in the outermost dictionary; you unwrapped the outermost dictionary and only posted the nested dictionary under 'fields'. Instead, include that outer dictionary:

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

podio.Item.update(629783395, newValues)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • It works! Wow I can't believe that was the issue this whole time, I thought I tried that before but got to the exception saying the update was unsuccessful and I thought it was because of removing ['fields'] (since it always printed 'successful' when I kept that part), but apparently the reason it got to the exception was because I didn't specify the fields using their correct names. As the other commentator said, the try/except code wasn't very helpful. Anyway, thanks a lot! – Chisq Jun 24 '17 at 12:41
  • @Nils: yes, don't play Pokemon, you do not want to catch them *all*. See [Why is "except: pass" a bad programming practice?](//stackoverflow.com/q/21553327) – Martijn Pieters Jun 24 '17 at 12:43
  • Thanks, I'll think about that in the future. Still new to programming. – Chisq Jun 24 '17 at 12:45