Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High level api #14

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft

High level api #14

wants to merge 7 commits into from

Conversation

gernot-h
Copy link
Collaborator

@gernot-h gernot-h commented Apr 5, 2022

@tngraf, we discussed about this proposal already Siemens-internal a while ago. I still use and love it in a number of places, especially for interactive API usage, so here's the public PR. :)

Several times, I found myself writing project-specific JSON parsing code (especially to parse ['_embedded']['sw360:releases'] and friends). So I started with first basic classes and made some important attributes and especially relations available in an OO fashion.

My code tries to be a thin and maintenable extension to the mostly direct API access we have now, only adding OO abstractions where they provide real benefit. To avoid the need to closely follow each SW360 api change, I only make important attributes available, the rest is available as the usual JSON data in the details attribute.

Example session:

>>> debian10 = sw360.get_project("123")
>>> debian10 = sw360.Project(debian10)
>>> debian10
Project(name='Debian_Buster', version='10', project_id='365275b1c92aae35d49f4a9c012dd7e9')
>>> debian10.releases                                                                                                                                  
{'334': Release(name='strace', version='4.26-0.2.debian', release_id='334'),
 'cd4': Release(name='usbutils', version='010-3.debian', release_id='cd4'),
 '7f5': Release(name='Linux Kernel', version='4.19.98-1.debian', release_id='7f5'),
[...]
>>> for release_id, release in debian10.releases.items(): 
...     print(release.name, release.version)
strace 4.26-0.2.debian
usbutils 010-3.debian
[...]
>>> release = debian10.releases['334']
>>> release.attachments
{}
>>> release.get(sw360_api)  # you will only get basic data within SW360 project, get full release data now
>>> release.attachments                                                                                                                                    
{'ed4': Attachment(attachment_type='CLEARING_REPORT', filename='strace_4.26-0.2_report.docx', attachment_id='ed4'),
 '659': Attachment(attachment_type='SOURCE_SELF', filename='strace_4.26-0.2.tar.bz2', attachment_id='659')}

Main open points:

  • For now, this only allows read access to SW360, code to push changes to the instances back to SW360 needs to be added, e.g. to_json() methods.
  • How to create high-level objects without breaking / duplicating low-level API? Currently, there are two ways: rel = Release(sw360.get_release("1234")), rel = Release().get(sw360, "1234")
  • Use SW360 names for attributes or Python-style ones? (attachment_type vs. attachmentType)

@gernot-h
Copy link
Collaborator Author

gernot-h commented Apr 5, 2022

@heliocastro, as you already contributed some valuable refactoring code, perhaps you're interested in reviewing my proposal for an OO API extension here? I would love to hear your opinion...

Start a high level API with classes for Release and Component.
Refactor the from_json() methods to one common version.
This allows to directly get data from SW360 using the high-level API -
and especially to retrieve complete data from embedded objects like
releases in a component or downloading an attachment.
We can only set component_id if releases are embedded in components, but
not for releases in projects.

As this was a somehow tricky bug, let's add first testcases for it.
This allows for easy copy'n'pasting of an object in interactive
sessions plus nicer print output.
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.

None yet

1 participant