-
Notifications
You must be signed in to change notification settings - Fork 25
add pagination with search_after, other refactoring #85
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
"pystac[validation]", | ||
"uvicorn", | ||
"overrides", | ||
"starlette", | ||
] | ||
|
||
extra_reqs = { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/models/links.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
"""link helpers.""" | ||
|
||
from typing import Any, Dict, List, Optional | ||
from urllib.parse import ParseResult, parse_qs, unquote, urlencode, urljoin, urlparse | ||
|
||
import attr | ||
from stac_pydantic.links import Relations | ||
from stac_pydantic.shared import MimeTypes | ||
from starlette.requests import Request | ||
|
||
# Copied from pgstac links | ||
|
||
# These can be inferred from the item/collection, so they aren't included in the database | ||
# Instead they are dynamically generated when querying the database using the classes defined below | ||
INFERRED_LINK_RELS = ["self", "item", "parent", "collection", "root"] | ||
|
||
|
||
def merge_params(url: str, newparams: Dict) -> str: | ||
"""Merge url parameters.""" | ||
u = urlparse(url) | ||
params = parse_qs(u.query) | ||
params.update(newparams) | ||
param_string = unquote(urlencode(params, True)) | ||
|
||
href = ParseResult( | ||
scheme=u.scheme, | ||
netloc=u.netloc, | ||
path=u.path, | ||
params=u.params, | ||
query=param_string, | ||
fragment=u.fragment, | ||
).geturl() | ||
return href | ||
|
||
|
||
@attr.s | ||
class BaseLinks: | ||
"""Create inferred links common to collections and items.""" | ||
|
||
request: Request = attr.ib() | ||
|
||
@property | ||
def base_url(self): | ||
"""Get the base url.""" | ||
return str(self.request.base_url) | ||
|
||
@property | ||
def url(self): | ||
"""Get the current request url.""" | ||
return str(self.request.url) | ||
|
||
def resolve(self, url): | ||
"""Resolve url to the current request url.""" | ||
return urljoin(str(self.base_url), str(url)) | ||
|
||
def link_self(self) -> Dict: | ||
"""Return the self link.""" | ||
return dict(rel=Relations.self.value, type=MimeTypes.json.value, href=self.url) | ||
|
||
def link_root(self) -> Dict: | ||
"""Return the catalog root.""" | ||
return dict( | ||
rel=Relations.root.value, type=MimeTypes.json.value, href=self.base_url | ||
) | ||
|
||
def create_links(self) -> List[Dict[str, Any]]: | ||
"""Return all inferred links.""" | ||
links = [] | ||
for name in dir(self): | ||
if name.startswith("link_") and callable(getattr(self, name)): | ||
link = getattr(self, name)() | ||
if link is not None: | ||
links.append(link) | ||
return links | ||
|
||
async def get_links( | ||
self, extra_links: Optional[List[Dict[str, Any]]] = None | ||
) -> List[Dict[str, Any]]: | ||
""" | ||
Generate all the links. | ||
|
||
Get the links object for a stac resource by iterating through | ||
available methods on this class that start with link_. | ||
""" | ||
# TODO: Pass request.json() into function so this doesn't need to be coroutine | ||
if self.request.method == "POST": | ||
self.request.postbody = await self.request.json() | ||
# join passed in links with generated links | ||
# and update relative paths | ||
links = self.create_links() | ||
|
||
if extra_links: | ||
# For extra links passed in, | ||
# add links modified with a resolved href. | ||
# Drop any links that are dynamically | ||
# determined by the server (e.g. self, parent, etc.) | ||
# Resolving the href allows for relative paths | ||
# to be stored in pgstac and for the hrefs in the | ||
# links of response STAC objects to be resolved | ||
# to the request url. | ||
links += [ | ||
{**link, "href": self.resolve(link["href"])} | ||
for link in extra_links | ||
if link["rel"] not in INFERRED_LINK_RELS | ||
] | ||
|
||
return links | ||
|
||
|
||
@attr.s | ||
class PagingLinks(BaseLinks): | ||
"""Create links for paging.""" | ||
|
||
next: Optional[str] = attr.ib(kw_only=True, default=None) | ||
|
||
def link_next(self) -> Optional[Dict[str, Any]]: | ||
"""Create link for next page.""" | ||
if self.next is not None: | ||
method = self.request.method | ||
if method == "GET": | ||
href = merge_params(self.url, {"token": self.next}) | ||
link = dict( | ||
rel=Relations.next.value, | ||
type=MimeTypes.json.value, | ||
method=method, | ||
href=href, | ||
) | ||
return link | ||
if method == "POST": | ||
return { | ||
"rel": Relations.next, | ||
"type": MimeTypes.json, | ||
"method": method, | ||
"href": f"{self.request.url}", | ||
"body": {**self.request.postbody, "token": self.next}, | ||
} | ||
|
||
return None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now we need the token, and pass sort as a separate value instead of part of search