Forums

API from pythonanywhere.com not working as expected

Hola estoy haciendo tests con https://pythonanywhere.com y he intentado crear una función para cambiar las tareas programadas.

Aquí la documentación de la API: https://help.pythonanywhere.com/pages/API/

Primero obtengo el id de la tarea programada

username = os.environ.get('USER', 'XXX')
token = os.environ.get('API_TOKEN', 'XXX')

response = requests.get(
    'https://www.pythonanywhere.com/api/v0/user/{username}/schedule/'.format(
        username=username
    ),
    headers={'Authorization': 'Token {token}'.format(token=token)}
)


idcron = response.json()[0].get("id")

Y luego intento cambiar la hora

    params = {'command': response.json()[0].get("command"),
        'enabled': response.json()[0].get("enabled"),
        'interval': response.json()[0].get("interval"),
        'hour': 11,
        'minute': 25
    }

    response = requests.put(
        'https://www.pythonanywhere.com/api/v0/user/{username}/schedule/{idcron}'.format(
            username=username, idcron=idcron
        ),
        headers={'Authorization': 'Token {token}'.format(token=token)},
        params=params
    )

Y tengo el siguiente fallo

>>> response.text
'{"interval":["This field is required."],"command":["This field is required."],"minute":["This field is required."]}'

He probado de todo pero siempre tengo esta respuesta.

PD: Estoy ejecutando este scrip dentro de la consola de pythonanywhere en la misma cuenta que quiero cambiar la tarea programada.

Use the patch method, not put.

Looks like our docs are confusing and should be corrected. Thanks for the report.

I am attempting to use Patch instead and when sending

params={'enabled': False}

none of the tasks change their status even though I get a successful response back.

Could you give more context of how you do it?

Here is the full function. I get the task data directly from doing a GET on the scheduled tasks. That is working no problem.

def pause(task, username, token):                                                                                                      
    task['enabled'] = False                                                                                                            
    print(task)                                                                                                                        
    response = requests.patch(                                                                                                         
    'https://www.pythonanywhere.com/api/v0/user/{username}/schedule/{taskid}/'.format(                                                 
        username=username,                                                                                                             
        taskid=task['id']
    ),
    headers={'Authorization': 'Token {token}'.format(token=token)},
    params={'enabled': False},
    )
    print(response.content)

Try using:

json={'enabled': False}

Instead of

params={'enabled': False}

Thanks, that did the trick.

Great, thanks for confirming!