Skip to content

Commit

Permalink
style: fix lint errors, reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertRosca committed Sep 24, 2024
1 parent bc951be commit fa0e986
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
4 changes: 3 additions & 1 deletion src/zulip_write_only_proxy/_logging.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging
import sys
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -128,7 +130,7 @@ def logger(self):

return self._logger

async def dispatch(self, request: "Request", call_next) -> "Response":
async def dispatch(self, request: Request, call_next) -> Response:
"""Add a middleware to FastAPI that will log requests and responses,
this is used instead of the builtin Uvicorn access logging to better
integrate with structlog"""
Expand Down
14 changes: 7 additions & 7 deletions src/zulip_write_only_proxy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

import datetime
import enum
import logging
import secrets
from typing import IO, TYPE_CHECKING, Any

from pydantic import BaseModel, Field, PrivateAttr, SecretStr, field_validator

from zulip_write_only_proxy import logger

if TYPE_CHECKING:
import zulip

log = logging.getLogger(__name__)


class PropagateMode(str, enum.Enum):
change_one = "change_one"
Expand Down Expand Up @@ -46,9 +45,10 @@ def upload_file(self, file: IO[Any]):
def get_stream_topics(self):
stream = self._client.get_stream_id(self.stream)
if stream["result"] != "success":
log.error(
f"failed to get stream id for {self.stream}, "
f"zulip api response: {stream}"
logger.error(
"Failed to get stream id",
stream=self.stream,
response=stream,
)
return stream
stream_id = stream["stream_id"]
Expand Down Expand Up @@ -89,5 +89,5 @@ class ScopedClientWithKey(ScopedClient):
key: str # type: ignore[assignment]

@field_validator("key")
def _set_key(cls, v: str | SecretStr) -> str:
def _set_key(self, v: str | SecretStr) -> str:
return v.get_secret_value() if isinstance(v, SecretStr) else v
12 changes: 5 additions & 7 deletions src/zulip_write_only_proxy/mymdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async def acquire_token(self):
Token data stored under `_access_token` and `_expires_at`.
"""
expired = self._expires_at <= dt.datetime.now()
expired = self._expires_at <= dt.datetime.now(tz=dt.timezone.utc)
if self._access_token and not expired:
logger.debug("Reusing existing MyMdC token", expires_at=self._expires_at)
return self._access_token
Expand All @@ -59,19 +59,17 @@ async def acquire_token(self):

if any(k not in data for k in ["access_token", "expires_in"]):
logger.critical(
"Response from MyMdC missing required fields, check webservice `user-id`"
"and `user-secret`.",
"Response from MyMdC missing required fields, check webservice "
"`user-id` and `user-secret`.",
response=response.text,
status_code=response.status_code,
)
msg = "Invalid response from MyMdC"
raise ValueError(
msg
) # TODO: custom exception, frontend feedback
raise ValueError(msg) # TODO: custom exception, frontend feedback

expires_in = dt.timedelta(seconds=data["expires_in"])
self._access_token = data["access_token"]
self._expires_at = dt.datetime.now() + expires_in
self._expires_at = dt.datetime.now(tz=dt.timezone.utc) + expires_in

logger.info("Acquired new MyMdC token", expires_at=self._expires_at)
return self._access_token
Expand Down
5 changes: 2 additions & 3 deletions src/zulip_write_only_proxy/routers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,15 @@ def update_message(
content: Annotated[str | None, fastapi.Body(media_type="text/plain")] = None,
topic: Annotated[str | None, fastapi.Query()] = None,
):
if content or topic:
return client.update_message(topic, content, message_id, propagate_mode)
else:
if not (content or topic):
raise fastapi.HTTPException(
status_code=400,
detail=(
"Either content (update message text) or topic (rename message topic) "
"must be provided"
),
)
return client.update_message(topic, content, message_id, propagate_mode)


_docs_url = "https://zulip.com/api/upload-file#response"
Expand Down
2 changes: 1 addition & 1 deletion src/zulip_write_only_proxy/routers/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def check_auth(request: Request):
if "da" not in user.get("groups", []):
raise AuthException(
status_code=403,
detail=f"Forbidden - user `{user.get('preferred_username')}` not allowed access",
detail=f"Forbidden - `{user.get('preferred_username')}` not allowed access",
)


Expand Down

0 comments on commit fa0e986

Please sign in to comment.