Skip to content

fix(zammad): handle flat list response from search API#470

Open
skjnldsv wants to merge 1 commit into
psss:mainfrom
skjnldsv:fix/zammad-list-response
Open

fix(zammad): handle flat list response from search API#470
skjnldsv wants to merge 1 commit into
psss:mainfrom
skjnldsv:fix/zammad-list-response

Conversation

@skjnldsv

Copy link
Copy Markdown

Summary

Newer Zammad instances return a flat list of ticket objects from the
search endpoint instead of the {"assets": {"Ticket": {...}}} enveloppe.

Fix

Detect the response type:

  • list → build {str(id): ticket} dict directly
  • dict → existing assets.Ticket unwrap path

Both paths produce the same structure consumed by TicketsUpdated.fetch().

Traceback

Traceback (most recent call last):
  ...
  File ".../did/plugins/zammad.py", line 114, in fetch
    for _, ticket in self.parent.zammad.search(query).items():
                     ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
  File ".../did/plugins/zammad.py", line 63, in search
    result = self.perform_search(query)["assets"]
             ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
TypeError: list indices must be integers or slices, not str

Tested against a Zammad instance runing the newer API format, confirmed working.

Newer Zammad instances return a flat list of ticket objects from the
search endpoint instead of the `{"assets": {"Ticket": {...}}}` envelope.

Handle both formats: if the response is a list, build the expected
`{id: ticket}` dict directly; otherwise fall through to the existing
assets-unwrap path.

Fixes: TypeError: list indices must be integers or slices, not str
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
@skjnldsv

Copy link
Copy Markdown
Author

cc @come-nc

@come-nc come-nc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that works.
We actually do not need the keys so it would be cleaner to turn the map into list for older instances instead (and adapt the for to ignore keys in TicketsUpdated::fetch).

The diff I use locally is:

diff --git a/did/plugins/zammad.py b/did/plugins/zammad.py
index 78d331c..15f942e 100644
--- a/did/plugins/zammad.py
+++ b/did/plugins/zammad.py
@@ -59,12 +59,8 @@ class Zammad():
             raise ReportError(
                 f"Zammad search on {self.url} failed.") from error
 
-    def search(self, query: str) -> dict:
-        result = self.perform_search(query)["assets"]
-        try:
-            result = result["Ticket"]
-        except KeyError:
-            result = {}
+    def search(self, query: str):
+        result = self.perform_search(query)
         log.debug("Result: %s fetched", listed(len(result), "item"))
         log.data(pretty(result))
         return result
@@ -111,7 +107,7 @@ class TicketsUpdated(Stats):
         self.stats = []
         since = self.options.since.date
         until = self.options.until.date
-        for _, ticket in self.parent.zammad.search(query).items():
+        for ticket in self.parent.zammad.search(query):
             for article in self.parent.zammad.get_articles(ticket["id"]):
                 updated_at = datetime.fromisoformat(
                     article["updated_at"].replace('Z', '+00:00')).date()

I did not create a PR from it because I did not know how to support both old and new.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants