-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added processors for URL generation models
- Loading branch information
Showing
2 changed files
with
50 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from confhub.model_processors.generate_url import URLBuilder | ||
|
||
__all__ = [ | ||
'URLBuilder', | ||
] |
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,45 @@ | ||
import inspect | ||
from typing import Set | ||
|
||
import yarl | ||
|
||
from confhub.core.error import ConfhubError | ||
|
||
|
||
class URLBuilder: | ||
""" | ||
A class for generating a URL based on parameters passed to an object. | ||
The `get_url` method builds URLs using parameters matching the `yarl.URL.build` method signature. | ||
""" | ||
|
||
def get_url(self) -> str: | ||
""" | ||
Generates and returns a URL based on object parameters. | ||
Returns: | ||
str: Generated URL in human-readable format. | ||
Exceptions: | ||
ConfhubError: Occurs if there is an error creating the URL. | ||
""" | ||
# We get the signature of the yarl.URL.build method | ||
build_signature: inspect.Signature = inspect.signature(yarl.URL.build) | ||
valid_keys: Set[str] = set(build_signature.parameters.keys()) | ||
|
||
# Collecting parameters to create a URL | ||
params = { | ||
key: | ||
(f"/{value}" if "/" not in value and key == "path" else value) | ||
if isinstance(value, str) | ||
else value | ||
for key, value in self.__dict__.items() | ||
if not key.startswith('_') and key in valid_keys | ||
} | ||
|
||
try: | ||
url: yarl.URL = yarl.URL.build(**params) | ||
# Returning the URL in human-readable format; Example: `http://localhost:8000` | ||
return url.human_repr() | ||
except Exception as err: | ||
raise ConfhubError("Error creating service url", err=err, params=params) |