I'm trying to get some data from the api using python requests. My code is as follows:
response = settings.get_request_with_token(settings.api_url, settings.token)
dashboard = json.loads(response.decode("utf-8"))
And get_request_with_token
is as follows:
def post_request_with_token(url, api_key, json_payload={}):
r = requests.post(url, headers={'Authorization': 'Bearer ' + api_key}, data=json_payload)
return r.content
In my response
I get the following result:
'[{"id":1,"title":"Name","uri":"db/name","type":"dash-db","tags":[],"isStarred":false}]'
I use then json.loads
to get the following result:
[{u'tags': [], u'type': u'dash-db', u'title': u'Name', u'uri': u'db/name', u'isStarred': False, u'id': 1}]
But I cannot rid of 'u' .
I tried as you can see with .decode("utf-8")
but I get the same.
Any idea how to solve it?
UPDATE
I'm trying to update dashboard on Grafana and I'm getting the following error:
[{u'message': u"invalid character 'm' looking for beginning of value", u'classification': u'DeserializationError'}, {u'message': u'Required', u'classification': u'RequiredError', u'fieldNames': [u'Dashboard']}]
UDPDATE2
The whole script looks as follows:
try:
dashboard_name = sys.argv[1] # Get the argument
# Get dashboard from QA organisation (we give default api token. The default token is from QA organisation from grafana)
response = settings.get_request_with_token(settings.api_url + "search?query=" + dashboard_name, settings.token)
dashboard = json.loads(response.decode("utf-8"))
if len(dashboard) < 1:
print("There is no dashboard data.")
else:
dashboard_data = dashboard[0]
dashboard_uri = str(dashboard_data["uri"])
dashboard_details = dashboard_api.get_dashboard(dashboard_uri, settings.token)
# Get all organisations
all_organisations = organization_api.get_all_organizations() # Get all organisations
for org in all_organisations:
if org['id'] == 28:
org_data_list = filter(lambda organisation: organisation['grafana_id'] == org['id'], settings.organisations)
try:
current_org_api_key = org_data_list[0]['api_key']
result = dashboard_api.add_dashboard(current_org_api_key, dashboard_details["dashboard"])
print(result)
pdb.set_trace()
except:
pass
except IndexError:
print("Please provide dashboard name!")
Function get_request_with_token
looks as follows:
def get_request_with_token(url, api_key):
r = requests.get(url, headers={'Authorization': 'Bearer ' + api_key})
return r.content
Function get_dashboard
looks as follows:
def get_dashboard(board_uri, token):
response = settings.get_request_with_token(settings.api_url + "dashboards/{0}".format(board_uri), token)
return json.loads(response.encode("utf-8"))
Function add_dashboard
looks as follows:
def add_dashboard(token, json_payload):
response = settings.post_request_with_token(settings.api_url + "dashboards/db", token, json_payload)
return json.loads(response)
And function post_request_with_token
looks as follows:
def post_request_with_token(url, api_key, json_payload={}):
r = requests.post(url, headers={'Authorization': 'Bearer ' + api_key, 'Content-Type': 'application/json'}, data=json_payload)
return r.content