Skip to content
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

Linting fixes #1004

Merged
merged 6 commits into from
Dec 10, 2024
Merged

Conversation

jjaakola-aiven
Copy link
Contributor

About this change - What it does

References: #xxxxx

Why this way

@jjaakola-aiven jjaakola-aiven requested review from a team as code owners December 9, 2024 17:08
*,
request: Request,
primary_url: str,
) -> bytes:
LOG.info("Forwarding %s request to remote url: %r since we're not the master", request.method, request.url)
timeout = 60.0
headers = request.headers.mutablecopy()
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a piece of code for the authorization header, do we want to bring this back?
i.e.

# auth_header = request.headers.get("Authorization")
# if auth_header is not None:
#    headers["Authorization"] = auth_header

Copy link
Contributor

@nosahama nosahama Dec 9, 2024

Choose a reason for hiding this comment

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

There's also this piece of code: func = getattr(self._get_forward_client(), request.method.lower()), this would mean that we generate a new client session every time we want to forward a request. Maybe we move the session initialization to the __init__() function, assign directly to self._forward_client and that will get reused for the whole app runtime and we can even add a close() function that cleans up the client session in the application lifespan.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, probably not in this PR though.

Copy link

github-actions bot commented Dec 9, 2024

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  src/karapace
  auth.py
  config.py 125
  forward_client.py 61-62, 78, 88, 104
  rapu.py
  statsd.py
  typing.py
  src/schema_registry
  factory.py 20, 48, 59
  schema_registry_apis.py 79, 87, 120, 189, 266, 304, 357, 403, 411, 419, 428, 459, 467, 475-476, 490, 498, 506, 517, 527, 538-539, 550-552, 560, 580, 588, 596-597, 611, 695, 720, 729, 750, 779, 812-815, 838, 846, 854, 868, 907, 922, 939
  src/schema_registry/http_handlers
  __init__.py 17-21
  src/schema_registry/middlewares
  __init__.py 6-10, 15, 30
  src/schema_registry/routers
  config.py 56-58, 96-98, 120-122
  master_availability.py 7, 57
  mode.py 12
  schemas.py 7
  subjects.py 86-88, 167-169
Project Total  

This report was generated by python-coverage-comment-action

headers=response.headers,
)
if self._acceptable_response_content_type(content_type=response.headers.get("Content-Type")):
return await response.text()
Copy link
Contributor

Choose a reason for hiding this comment

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

Would there be anytime when we expect text/plain?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Error cases, I can't think of anything else.


async def forward_request_remote(self, *, request: Request, primary_url: str, response_type: type[T] | type[P]) -> T | P:
body = await self._forward_request_remote(request=request, primary_url=primary_url)
if response_type == int:
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not using isinstance()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The response_type is a type not an int object.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yh makes sense, hence the type vars 👍

if response_type == list[int]:
return json_decode(body, assume_type=list[int]) # type: ignore[return-value]
if issubclass(response_type, BaseModel):
return response_type.model_validate_json(body) # type: ignore[return-value]
Copy link
Contributor

Choose a reason for hiding this comment

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

If it's a BaseModel, it should already have been validated from the source response, unless there's no other way to load the text into the object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Uh, Pydantic 1.x has parse_raw which is deprecated in 2.x. I think we got to support 1.x so I'll change to parse_raw.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

status_code=response.status,
headers=response.headers,
)
if self._acceptable_response_content_type(content_type=response.headers.get("Content-Type")):
Copy link
Contributor

Choose a reason for hiding this comment

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

What if the server cannot be reached, i.e. there's even no response, etc, or 5xx status.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Handling improvement to do later.

@jjaakola-aiven jjaakola-aiven force-pushed the jjaakola-aiven-fastapi-lint-fixes branch from d7e9684 to 37a17fa Compare December 10, 2024 07:19
@jjaakola-aiven jjaakola-aiven force-pushed the jjaakola-aiven-fastapi-lint-fixes branch from 76f2f7e to 5f77f09 Compare December 10, 2024 08:16
) -> BaseModelResponse | SimpleTypeResponse:
body = await self._forward_request_remote(request=request, primary_url=primary_url)
if response_type == int:
return int(body) # type: ignore[return-value]
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe a cast will work to avoid the # type: ignore[return-value], we can cast to the SimpleTypeResponse, but not sure if this works, will test in another PR.

@@ -14,16 +16,30 @@
LOG = logging.getLogger(__name__)


BaseModelResponse = TypeVar("BaseModelResponse", bound=BaseModel)
SimpleTypeResponse = TypeVar("SimpleTypeResponse", bound=Union[int, list[int]])
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need the Union, maybe | works, will test it later.

],
ids=str,
)
async def test_forward_request_with_basemodel_response(testcase: ContentTypeTestCase) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you.

@nosahama nosahama merged commit 8024753 into jjaakola-aiven-fastapi Dec 10, 2024
8 of 9 checks passed
@nosahama nosahama deleted the jjaakola-aiven-fastapi-lint-fixes branch December 10, 2024 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants