Skip to content

Redesign the rest API - #247

Merged
pombredanne merged 7 commits into
aboutcode-org:developfrom
sbs2001:rest_api
Sep 3, 2020
Merged

Redesign the rest API#247
pombredanne merged 7 commits into
aboutcode-org:developfrom
sbs2001:rest_api

Conversation

@sbs2001

@sbs2001 sbs2001 commented Aug 21, 2020

Copy link
Copy Markdown
Collaborator

TODOs

  1. Update documentation. Add tests specific to checking new API additions
    2. Deal with inputting packageurls as query param.

@sbs2001
sbs2001 requested a review from tdruez August 26, 2020 05:54

@tdruez tdruez 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.

The API code looks good.
See my comments for models and urls.

Comment thread vulnerabilities/models.py Outdated

@property
def vulnerable_to(self):
qs = PackageRelatedVulnerability.objects.filter(

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.

Use Many2Many QuerySet syntax instead.

Comment thread vulnerabilities/models.py Outdated

@property
def vulnerable_to(self):
qs = PackageRelatedVulnerability.objects.filter(

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.

Same as above, use Many2Many syntax.

Comment thread vulnerabilities/models.py
null=True,
)

def set_package_url(self, package_url):

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.

Why are we overriding the default method from PackageURLMixin?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This one uses JSONField the oldset_package_url converts qualifiers into normalized string. More on this package-url/packageurl-python#35 (comment)

Comment thread vulnerablecode/urls.py Outdated
path('admin/', admin.site.urls),
re_path(r'^api/', include(api_router.urls))
path("admin/", admin.site.urls),
re_path(r"^api/", include(api_router.urls)),

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.

What is the need for re_path instead of path here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I haven't touched that, that's black doing it's thing. Having said that we can use path their alright.

Comment thread vulnerablecode/urls.py Outdated

api_router = DefaultRouter()
api_router.register(r'packages', PackageViewSet)
api_router = DefaultRouter(trailing_slash=False)

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.

What's the reasoning behind overriding the default trailing_slash convention?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I had to discuss this with you guys.

For the ListView using trailing_slash the endpoints look odd. The endpoints with queries look like :
/api/vulnerabilities/?vulnerability_id=foo

After the override you get the :
/api/vulnerabilities?vulnerability_id=foo

Not sure this matter tho, so I might be missing something here.

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 don't see the benefit of removing the trailing slash.

From https://www.django-rest-framework.org/api-guide/routers/

Trailing slashes are conventional in Django, but are not used by default in some other frameworks such as Rails.

I think we should follow Django's conventions.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

👍

The REST API now has a detail views for package and vulnerability.
The search API allows packages and vulnerabilities to be searched for using various
parameters. The design now uses hyperlinks and provides easy navigation.

Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
Comment thread vulnerabilities/models.py Outdated
return self.packagerelatedvulnerability_set.filter(is_vulnerable=True)
return self.package_set.filter(
packagerelatedvulnerability__is_vulnerable=True,
packagerelatedvulnerability__vulnerability_id=self.id,

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 don't think this lookup is necessary since the related manager is starting on self.

@tdruez tdruez 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.

Looks good. Although I don't think those lookups are necessary:
packagerelatedvulnerability__vulnerability_id=self.id,
and
packagerelatedvulnerability__package_id=self.id,

Please confirm and update accordingly.

@sbs2001
sbs2001 requested a review from tdruez August 27, 2020 12:09
@sbs2001 sbs2001 changed the title [WIP] Redesign the rest API Redesign the rest API Aug 27, 2020

@tdruez tdruez 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.

LGTM!

Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>

@pombredanne pombredanne left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM 👍

@pombredanne
pombredanne merged commit 37b7668 into aboutcode-org:develop Sep 3, 2020
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