Skip to content

Links service refactor #9460

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Links service refactor #9460

wants to merge 1 commit into from

Conversation

marcospri
Copy link
Member

Remove function registry pattern and return all links explicitly

Remove function registry pattern and return all links explicitly
@@ -48,7 +49,7 @@ def incontext_link(self, request):
"""
if not self.annotations:
return None
return links.incontext_link(request, self.annotations[0])
return incontext_link(request, self.annotations[0])
Copy link
Member Author

@marcospri marcospri Apr 8, 2025

Choose a reason for hiding this comment

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

We often use these functions directly, keep it as functions but moved them to the service namespace.

Copy link
Contributor

Choose a reason for hiding this comment

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

We could maybe just change code paths like this to use the service methods: request.find_service(name="links").incontext_link(annotation).

from urllib.parse import unquote, urljoin, urlparse


def pretty_link(url):
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved these to the service.

@@ -20,7 +21,7 @@ def asdict(self):
return {
"@context": self.CONTEXT_URL,
"type": "Annotation",
"id": self._links_service.get(self.annotation, "jsonld_id"),
"id": self._links_service.jsonld_id(self.annotation),
Copy link
Member Author

Choose a reason for hiding this comment

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

Call the right service method directly here.

@@ -80,7 +80,11 @@ def present(self, annotation: Annotation, with_metadata: bool = False): # noqa:
},
"target": annotation.target,
"document": DocumentJSONPresenter(annotation.document).asdict(),
"links": self._links_service.get_all(annotation),
"links": {
Copy link
Member Author

Choose a reason for hiding this comment

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

List every type of link here.

from pyramid.request import Request

from h.security.request_methods import default_authority

LINK_GENERATORS_KEY = "h.links.link_generators"

def json_link(request, annotation) -> str:
Copy link
Member Author

Choose a reason for hiding this comment

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

Functions from h.links

return LinksService(base_url=base_url, registry=request.registry)


def add_annotation_link_generator(config, name, generator, hidden=False): # noqa: FBT002
Copy link
Member Author

Choose a reason for hiding this comment

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

Remove the registry

@@ -21,6 +20,21 @@
PAGE_SIZE = 200


def pretty_link(url):
Copy link
Member Author

Choose a reason for hiding this comment

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

This was in h.links but only used here, 🤷 I could a function in the service like the others but it's a bit different in the intent.

@@ -19,7 +59,6 @@ def __init__(self, base_url, registry):
:type registry: pyramid.registry.Registry
"""
self.base_url = base_url
self.registry = registry

# It would be absolutely fair if at this point you asked yourself any
Copy link
Member Author

Choose a reason for hiding this comment

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

This request object and the comment below is the only reason to keep this a service, not just functions.

I don't want to go on a huge refactoring path, IMO keeping the service and the functions in the same module is a bit clearer.

Copy link
Contributor

@seanh seanh left a comment

Choose a reason for hiding this comment

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

Yeah this seems like an improvement -- more similar to our usual services pattern, and the "link registry" thing seems unnecessary

@@ -48,7 +49,7 @@ def incontext_link(self, request):
"""
if not self.annotations:
return None
return links.incontext_link(request, self.annotations[0])
return incontext_link(request, self.annotations[0])
Copy link
Contributor

Choose a reason for hiding this comment

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

We could maybe just change code paths like this to use the service methods: request.find_service(name="links").incontext_link(annotation).

Copy link
Contributor

github-actions bot commented May 9, 2025

This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the stale Used by https://github.com/probot/stale to label stale issues and pull requests before closing them label May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Used by https://github.com/probot/stale to label stale issues and pull requests before closing them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants