m0nhawk/grafana_api

Creating annotation with multiple tags results in unmarshal error

megamorf opened this issue · 1 comments

Describe the bug

I'd like to create annotations that contain 1..n tags but passing an array or tuple to the add_annotation method fails with an unmarshal error:

Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
  File "/Users/megamorf/.local/share/virtualenvs/webhooks-29Gd7Fk5/lib/python3.7/site-packages/grafana_api/api/annotations.py", line 88, in add_annotation
    r = self.api.POST(annotations_path, json=payload)
  File "/Users/megamorf/.local/share/virtualenvs/webhooks-29Gd7Fk5/lib/python3.7/site-packages/grafana_api/grafana_api.py", line 127, in __request_runnner
    raise GrafanaBadInputError(response)
grafana_api.grafana_api.GrafanaBadInputError: Bad Input: `[{'classification': 'DeserializationError', 'message': 'json: cannot unmarshal array into Go struct field PostAnnotationsCmd.tags of type string'}]`

To Reproduce

Steps to reproduce the behavior:

from grafana_api.grafana_face import GrafanaFace
import time

tags = ['one','two','flex']

grafana_api = GrafanaFace(auth=api_token, host='localhost', port=3000)
grafana_api.annotations.add_annotation(
    time_from=int(time.time()),
    is_region=False,
    tags=tags,
    text="Job #345"
)

Expected behavior
Creating an annotation with multiple tags works when I do it without your code:

import requests
import json
import time
from datetime import datetime

example_annotation = {
    "time": int(time.time()),
    "tags": ["one", "two", "flex"],
    "text": str(datetime.now()),
}

def add_annotation_raw(annotation):
    resp = requests.post('http://localhost:3000/api/annotations',
        json=annotation,
        headers = {'Authorization': 'Bearer <api_key>'}
    )
    
    resp.raise_for_status()
    print('Created annotation. ID: {}'.format(resp.json()["id"]))
>>> add_annotation_raw(example_annotation)
Created annotation. ID: 9


{'alertId': 0,
  'alertName': '',
  'avatarUrl': '',
  'created': 1577398963875,
  'dashboardId': 0,
  'data': {},
  'email': '',
  'id': 9,
  'login': '',
  'newState': '',
  'panelId': 0,
  'prevState': '',
  'tags': ['one', 'two', 'flex'],
  'text': '2019-12-26 23:11:30.178468',
  'time': 1577398290,
  'timeEnd': 1577398290,
  'updated': 1577398963875,
  'userId': 0},

Versions

  • Grafana: version=6.5.1 commit=1763a0f
  • grafana_api: 0.9.2
  • Authentication: Token

Merged #55. Thank you for fix!