-
Notifications
You must be signed in to change notification settings - Fork 30
♻️ servicelib.fastapi
tools and rabbitmq.rpc
errors interface
#5157
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
17 commits
Select commit
Hold shift + click to select a range
437b9b2
add title to distinguish envs
pcrespov 815eaa8
splits tooling
pcrespov 781b9ae
enhance rpc expose interface
pcrespov 02e7565
error imports
pcrespov 86e7ca4
rename
pcrespov d6c1a04
webserver uses api errors for rpc
pcrespov 26416d0
logs and fixes bug in fake
pcrespov b897c11
@sanderegg review: base api exception private
pcrespov 7b659ff
adds service unavailable
pcrespov 173fc36
rename
pcrespov 8be9654
rename
pcrespov 8502d7c
should receive error
pcrespov e44c1fe
fixes test error and cleanup timeouts
pcrespov c9e76a9
extra test in rabbit
pcrespov acd73d2
mypy and doc
pcrespov 35f22ea
Merge branch 'master' into enh/servicelib
pcrespov 1ef7214
Merge branch 'master' into enh/servicelib
pcrespov 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
65 changes: 65 additions & 0 deletions
65
packages/models-library/src/models_library/api_schemas_payments/errors.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,65 @@ | ||
from pydantic.errors import PydanticErrorMixin | ||
|
||
|
||
class _BaseRpcApiError(PydanticErrorMixin, ValueError): | ||
@classmethod | ||
def get_full_class_name(cls) -> str: | ||
# Can be used as unique code identifier | ||
return f"{cls.__module__}.{cls.__name__}" | ||
|
||
|
||
# | ||
# service-wide errors | ||
# | ||
|
||
|
||
class PaymentServiceUnavailableError(_BaseRpcApiError): | ||
msg_template = "Payments are currently unavailable: {human_readable_detail}" | ||
|
||
|
||
# | ||
# payment transactions errors | ||
# | ||
|
||
|
||
class PaymentsError(_BaseRpcApiError): | ||
msg_template = "Error in payment transaction '{payment_id}'" | ||
|
||
|
||
class PaymentNotFoundError(PaymentsError): | ||
msg_template = "Payment transaction '{payment_id}' was not found" | ||
|
||
|
||
class PaymentAlreadyExistsError(PaymentsError): | ||
msg_template = "Payment transaction '{payment_id}' was already initialized" | ||
|
||
|
||
class PaymentAlreadyAckedError(PaymentsError): | ||
msg_template = "Payment transaction '{payment_id}' cannot be changes since it was already closed." | ||
|
||
|
||
# | ||
# payment-methods errors | ||
# | ||
|
||
|
||
class PaymentsMethodsError(_BaseRpcApiError): | ||
... | ||
|
||
|
||
class PaymentMethodNotFoundError(PaymentsMethodsError): | ||
msg_template = "The specified payment method '{payment_method_id}' does not exist" | ||
|
||
|
||
class PaymentMethodAlreadyAckedError(PaymentsMethodsError): | ||
msg_template = ( | ||
"Cannot create payment-method '{payment_method_id}' since it was already closed" | ||
) | ||
|
||
|
||
class PaymentMethodUniqueViolationError(PaymentsMethodsError): | ||
msg_template = "Payment method '{payment_method_id}' aready exists" | ||
|
||
|
||
class InvalidPaymentMethodError(PaymentsMethodsError): | ||
msg_template = "Invalid payment method '{payment_method_id}'" |
36 changes: 36 additions & 0 deletions
36
packages/service-library/src/servicelib/fastapi/app_state.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,36 @@ | ||
import logging | ||
|
||
from fastapi import FastAPI | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
class SingletonInAppStateMixin: | ||
""" | ||
Mixin to get, set and delete an instance of 'self' from/to app.state | ||
""" | ||
|
||
app_state_name: str # Name used in app.state.$(app_state_name) | ||
frozen: bool = True # Will raise if set multiple times | ||
|
||
@classmethod | ||
def get_from_app_state(cls, app: FastAPI): | ||
return getattr(app.state, cls.app_state_name) | ||
|
||
def set_to_app_state(self, app: FastAPI): | ||
if (exists := getattr(app.state, self.app_state_name, None)) and self.frozen: | ||
msg = f"An instance of {type(self)} already in app.state.{self.app_state_name}={exists}" | ||
raise ValueError(msg) | ||
|
||
setattr(app.state, self.app_state_name, self) | ||
return self.get_from_app_state(app) | ||
|
||
@classmethod | ||
def pop_from_app_state(cls, app: FastAPI): | ||
""" | ||
Raises: | ||
AttributeError: if instance is not in app.state | ||
""" | ||
old = getattr(app.state, cls.app_state_name) | ||
delattr(app.state, cls.app_state_name) | ||
return old |
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
84 changes: 84 additions & 0 deletions
84
packages/service-library/src/servicelib/fastapi/httpx_utils.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,84 @@ | ||
import httpx | ||
|
||
|
||
def _is_secret(k: str) -> bool: | ||
return "secret" in k.lower() or "pass" in k.lower() | ||
|
||
|
||
def _get_headers_safely(request: httpx.Request) -> dict[str, str]: | ||
return {k: "*" * 5 if _is_secret(k) else v for k, v in request.headers.items()} | ||
|
||
|
||
def to_httpx_command( | ||
request: httpx.Request, *, use_short_options: bool = True, multiline: bool = False | ||
) -> str: | ||
"""Command with httpx CLI | ||
|
||
$ httpx --help | ||
|
||
NOTE: Particularly handy as an alternative to curl (e.g. when docker exec in osparc containers) | ||
SEE https://www.python-httpx.org/ | ||
""" | ||
cmd = [ | ||
"httpx", | ||
] | ||
|
||
# -m, --method METHOD | ||
cmd.append(f'{"-m" if use_short_options else "--method"} {request.method}') | ||
|
||
# -c, --content TEXT Byte content to include in the request body. | ||
if content := request.read().decode(): | ||
cmd.append(f'{"-c" if use_short_options else "--content"} \'{content}\'') | ||
|
||
# -h, --headers <NAME VALUE> ... Include additional HTTP headers in the request. | ||
if headers := _get_headers_safely(request): | ||
cmd.extend( | ||
[ | ||
f'{"-h" if use_short_options else "--headers"} "{name}" "{value}"' | ||
for name, value in headers.items() | ||
] | ||
) | ||
|
||
cmd.append(f"{request.url}") | ||
separator = " \\\n" if multiline else " " | ||
return separator.join(cmd) | ||
|
||
|
||
def to_curl_command( | ||
request: httpx.Request, *, use_short_options: bool = True, multiline: bool = False | ||
) -> str: | ||
"""Composes a curl command from a given request | ||
|
||
$ curl --help | ||
|
||
NOTE: Handy reproduce a request in a separate terminal (e.g. debugging) | ||
""" | ||
# Adapted from https://github.com/marcuxyz/curlify2/blob/master/curlify2/curlify.py | ||
cmd = [ | ||
"curl", | ||
] | ||
|
||
# https://curl.se/docs/manpage.html#-X | ||
# -X, --request {method} | ||
cmd.append(f'{"-X" if use_short_options else "--request"} {request.method}') | ||
|
||
# https://curl.se/docs/manpage.html#-H | ||
# H, --header <header/@file> Pass custom header(s) to server | ||
if headers := _get_headers_safely(request): | ||
cmd.extend( | ||
[ | ||
f'{"-H" if use_short_options else "--header"} "{k}: {v}"' | ||
for k, v in headers.items() | ||
] | ||
) | ||
|
||
# https://curl.se/docs/manpage.html#-d | ||
# -d, --data <data> HTTP POST data | ||
if body := request.read().decode(): | ||
_d = "-d" if use_short_options else "--data" | ||
cmd.append(f"{_d} '{body}'") | ||
|
||
cmd.append(f"{request.url}") | ||
|
||
separator = " \\\n" if multiline else " " | ||
return separator.join(cmd) |
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
3 changes: 3 additions & 0 deletions
3
packages/service-library/src/servicelib/rabbitmq/_constants.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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
from typing import Final | ||
|
||
from pydantic import PositiveInt | ||
|
||
BIND_TO_ALL_TOPICS: Final[str] = "#" | ||
RPC_REQUEST_DEFAULT_TIMEOUT_S: Final[PositiveInt] = PositiveInt(5) | ||
RPC_REMOTE_METHOD_TIMEOUT_S: Final[int] = 30 |
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
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.
Uh oh!
There was an error while loading. Please reload this page.