-
Notifications
You must be signed in to change notification settings - Fork 0
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
Added safe mode option and url builder #23
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d9dd3a2
Added safe mode option and url builder
te25son 30b2c39
Update src/jokes/pipelines/get_pipeline.py
te25son 16dda8f
Use custom params to build urls
te25son 60a14ea
merge
te25son 4ebab7c
nit
te25son 52f9c21
Add primitive type check
te25son f53833c
naming and formatting
te25son 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 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 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 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 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
Empty file.
This file contains 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,55 @@ | ||
from pydantic import BaseModel, Field | ||
from click import get_current_context | ||
|
||
|
||
def filter_items(items: list[str | None]) -> set[str]: | ||
"""Filters none values from given list and returns a set.""" | ||
|
||
return set(filter(None, items)) | ||
|
||
|
||
class GetEndpointParams(BaseModel): | ||
""" | ||
Class representing the query parameters to be included in the get endpoint url. | ||
|
||
Fields containing an "" string will be converted to a query parameter with an empty value, i.e. `?param=`. | ||
Fields of type `None` will be converted to a valueless query parameter, i.e.`?param`. | ||
""" | ||
|
||
type: str | ||
category: str | ||
blacklist_flags: str | None = Field(default="", alias="blacklistFlags") | ||
safe_mode: str | None = Field(default=None, alias="safe-mode") | ||
|
||
|
||
def dict(self, **kwargs) -> dict[str, str]: | ||
"""Converts fields to a dict that can be converted to a valid url.""" | ||
|
||
safe_mode = context.obj.get("SAFE_MODE") if (context := get_current_context(silent=True)) else False | ||
included_fields = filter_items([ | ||
"type", | ||
"blacklist_flags" if self.blacklist_flags else None, | ||
"safe_mode" if safe_mode else None | ||
]) | ||
|
||
return super().dict(by_alias=True, include=included_fields, **kwargs) | ||
|
||
|
||
class SubmitEndpointParams(BaseModel): | ||
""" | ||
Class representing the query parameters to be included in the submit endpoint url. | ||
|
||
Fields containing an "" string will be converted to a query parameter with an empty value, i.e. `?param=`. | ||
Fields of type `None` will be converted to a valueless query parameter, i.e. `?param`. | ||
""" | ||
|
||
dry_run: str | None = Field(default=None, alias="dry-run") | ||
|
||
|
||
def dict(self, **kwargs) -> dict[str, str]: | ||
"""Converts fields to a dict that can be converted to a valid url.""" | ||
|
||
dry_run = context.obj.get("TEST") if (context := get_current_context(silent=True)) else False | ||
included_fields = filter_items(["dry_run" if dry_run else None]) | ||
|
||
return super().dict(by_alias=True, include=included_fields, **kwargs) |
This file contains 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,29 @@ | ||
from typing import Mapping | ||
from enum import Enum | ||
from jokes.utils.params import GetEndpointParams, SubmitEndpointParams | ||
|
||
|
||
BASE_URL = "https://v2.jokeapi.dev" | ||
|
||
Primitive = str | int | float | bool | None | ||
TParam = GetEndpointParams | SubmitEndpointParams | ||
|
||
|
||
class Endpoints(Enum): | ||
GET = "joke" | ||
SUBMIT = "submit" | ||
|
||
|
||
def build_query_string(dict: Mapping[str, Primitive]) -> str: | ||
"""Formats the params into a string usable by the joke api.""" | ||
|
||
return "&".join([k if v is None else f"{k}={v}" for k, v in dict.items()]) | ||
|
||
|
||
def build_endpoint_url(endpoint: Endpoints, params: TParam) -> str: | ||
"""Builds a valid joke api endpoint url.""" | ||
|
||
category = params.category if isinstance(params, GetEndpointParams) else None | ||
url = "/".join(filter(None, [BASE_URL, endpoint.value, category])) | ||
|
||
return f"{url}?{query_string}" if (query_string := build_query_string(params.dict())) else url |
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.
This pattern of adding an object to the context and fetching it this way is repeated (and will be repeated again if the submit dry-run is ever fully implemented).
Maybe add a new util?
JokesContext
?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.
Context can already be fetched from anywhere in the code via
get_current_context
.We could add a method like this though: