m0nhawk/grafana_api

update_dashboard() failing.

dbrennand opened this issue · 6 comments

Describe the bug
Using update_dashboard method fails.

To Reproduce
Steps to reproduce the behavior:

  1. Create GrafanaFace object.
  2. Call api.dashboard.update_dashboard()
  3. Pass in a JSON dashboard example from https://grafana.com/docs/grafana/latest/http_api/dashboard/#create-update-dashboard.

Expected behavior
The dashboard is created.

Versions

  • Grafana: [ 6.4.4]
  • grafana_api: [/dashboards/db ]
  • Authentication: [Token]

Additional context
My code. Works with normal requests library.

import grafana_api
import requests

with open("test.json") as json_data:
    # Using template from: https://grafana.com/docs/grafana/latest/http_api/dashboard/#create-update-dashboard
    test = json_data.read()

# Fails.
api = grafana_api.GrafanaFace(auth="my token", host="some grafana host")
# This method works so authentication is fine.
print(api.dashboard.get_home_dashboard())
print(api.dashboard.update_dashboard(test))

# Works all good with requests.
resp = requests.post("https://grafanahost.com/api/dashboards/db", headers={"Accept": "application/json", "Content-Type": "application/json", "Authorization": "Bearer token here"}, data=test)
print(resp.status_code)
print(resp.text)
print(resp.json())

Full traceback:

Traceback (most recent call last):
  File ".\test.py", line 12, in <module>
    print(api.dashboard.update_dashboard(str(test)))
  File "C:\Users\\.virtualenvs\lib\site-packages\grafana_api\api\dashboard.py", line 26, in update_dashboard
    r = self.api.POST(put_dashboard_path, json=dashboard)
  File "C:\Users\.virtualenvs\\lib\site-packages\grafana_api\grafana_api.py", line 111, in __request_runnner
    return r.json()
  File "C:\Users\\lib\site-packages\requests\models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "c:\python37\Lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "c:\python37\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "c:\python37\Lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

All the methods in the library is expecting JSON (dictionaries in Python) values as an input.

So, something like this will work:

with open("test.json") as json_data:
    test = json_data.read()
    test = json.loads(test)

print(api.dashboard.update_dashboard(test))

Feel free to re-open this issue or create a new one for other questions.

Hey, so I just started using the API and I'm running to the same issue now, except I am using a Python dict to represent the dashboard, but I'm still getting the same error.

I even tried with one of the test case dicts in test_dashboard.py to be sure it wasn't a malformed json, and that didn't work either.

@m0nhawk for context:

my_db = {'id': None, 'uid': None, 'title': 'hithere', 'tags': ['templated'], 'timezone': 'browser', 'schemaVersion': 16, 'version': 0}

grafana_api = GrafanaFace(auth='...', host='...')

# get operations work fine. I tested folders.get_all_folders(), for example, both with improper auth and proper auth to make sure it was working as expected.

grafana_api.dashboard.update_dashboard({'dashboard': my_db, 'folderId': 0, 'overwrite': True})

Output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../site-packages/grafana_api/api/dashboard.py", line 26, in update_dashboard
    r = self.api.POST(put_dashboard_path, json=dashboard)
  File ".../site-packages/grafana_api/grafana_api.py", line 136, in __request_runnner
    return r.json()
  File ".../site-packages/requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File ".../python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File ".../python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File ".../python3.7/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Grafana version v6.6.1

It looks like the exception is happening on the response being converted to json.

Never mind, after debugging I found out the url was automatically resolving to the wrong protocol. Setting it fixed the whole thing!