Skip to content

Convenience functions for HAL _embedded and _links parsing#54

Open
gernot-h wants to merge 2 commits into
masterfrom
links-and-embedded-parsing
Open

Convenience functions for HAL _embedded and _links parsing#54
gernot-h wants to merge 2 commits into
masterfrom
links-and-embedded-parsing

Conversation

@gernot-h

@gernot-h gernot-h commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

One of the main reasons for starting my OO API in #14+ was the constant, repetitive effort for parsing the ["_embedded"] and ["_links"] parts of the JSON answers. Last years have however shown that I don't find the time to finish the OO API, let alone the ongoing effort to maintain it.

With the help of qwen and Claude, I pulled out the ["_links"] and ["_embedded"] handling from my OO branch #14 into a thin extension of the sw360python API.

First of all, there are two new helper functions:

>>> rel = client.get_release("064123")
>>> client.get_linked_id(rel, "self")
064123
>>> client.get_linked_id(rel, "sw360:component")
184123
>>> client.get_embedded(rel, "sw360:attachments")
[{'filename': 'CLIXML_libgcrypt-2.0.xml',
  'attachmentType': 'COMPONENT_LICENSE_INFO_XML',
  '_links': {'self': {'href': 'https://sw360.siemens.com/resource/api/attachments/18f123'}}},
 {'filename': 'libgcrypt-2.0.tar.bz2',
  'attachmentType': 'SOURCE_SELF',
  '_links': {'self': {'href': 'https://sw360.siemens.com/resource/api/attachments/7a7123'}}},
  ...

The more intersting part is that it adds a new class SW360Response which is now always returned instead of the raw JSON objects. It's derived from dict and thus it should be 100% backward compatible. You can access everything in the JSON answer as before, but you can also use it in a much friendlier way.

The current way to get a component to a release rel still works:

client.get_component(client.get_id_from_href(rel["_links"]["sw360:component"]["href"])

But with the new code, you can also just do:

client.get_component(rel.linked_id("component"))

Or to get from a component to a release ID, the old way still works:

client.get_id_from_href(comp["_embedded"]["sw360:releases"][0]["_links"]["self"]["href"])

But you can now also use the new methods:

comp.embedded_list("releases")[0].linked_id()

@tngraf, let me know what you think!

@gernot-h gernot-h mentioned this pull request Jun 11, 2026
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.

1 participant