-
Notifications
You must be signed in to change notification settings - Fork 1
Request format of file contents using headers #80
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
Conversation
Co-authored-by: Joseph Ware <[email protected]>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #80 +/- ##
==========================================
+ Coverage 98.63% 99.23% +0.60%
==========================================
Files 5 6 +1
Lines 73 131 +58
==========================================
+ Hits 72 130 +58
Misses 1 1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
|
||
| return content | ||
|
|
||
| def get_file_contents( |
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.
Can anyone think of a neat way to do better typing here? I don't think we can avoid it having Any as a return type :(
| BlParamDType = str | int | float | bool | ||
|
|
||
|
|
||
| class RequestedResponseFormats(StrEnum): |
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.
I don't really like the names for this enum's values. I would like it to be obvious to the user what format the response would be in - open to suggestions here
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.
tried to find something, couldn't
| [tool.setuptools_scm] | ||
| version_file = "src/daq_config_server/_version.py" | ||
|
|
||
| [tool.pyright] |
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 came with the copier template, but I replaced it with pyrightconfig.json as it seems to allow more options, like ignoring certain rules in unit tests
DiamondJoseph
left a comment
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.
I need to read through the tests just to make sure everything is behaving how I would expect it to, but the code looks good. Some questions around typing that I'll look at with a fresh head tomorrow
stan-dot
left a comment
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.
lgtm, minor cosmetic questions
| responses={ | ||
| 200: { | ||
| "description": "Returns JSON, plain text, or binary file.", | ||
| "content": { | ||
| "application/json": { | ||
| "schema": { | ||
| "type": "object", | ||
| "additionalProperties": True, | ||
| "example": { | ||
| "key": "value", | ||
| "list": [1, 2, 3], | ||
| "nested": {"a": 1}, | ||
| }, | ||
| } | ||
| }, | ||
| "text/plain": { | ||
| "schema": { | ||
| "type": "string", | ||
| "example": "This is a plain text response", | ||
| } | ||
| }, | ||
| "application/octet-stream": { | ||
| "schema": {"type": "string", "format": "binary"}, | ||
| }, | ||
| }, | ||
| }, |
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.
do we need to hardcode this?
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.
I think so, yes. It's used by the SWAGGER UI to give you more info about accepted requests/responses. Things still work without it though
src/daq_config_server/client.py
Outdated
| self._url = url.rstrip("/") | ||
| self._log = log if log else getLogger("daq_config_server.client") | ||
| self._cache: TTLCache[tuple[str, str | None], str] = TTLCache( | ||
| self._cache: TTLCache[tuple[str, str, str], str] = TTLCache( |
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.
can we write a custom type for what is this generic thing?
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.
Isn't it just in/out of _cached_get? In which case shouldn't it be tuple[str, str, str], response?
| BlParamDType = str | int | float | bool | ||
|
|
||
|
|
||
| class RequestedResponseFormats(StrEnum): |
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.
tried to find something, couldn't
| class RequestedResponseFormats(StrEnum): | ||
| DICT = ValidAcceptHeaders.JSON # Convert to dict using Response.json() | ||
| DECODED_STRING = ValidAcceptHeaders.PLAIN_TEXT # Use utf-8 decoding in response | ||
| RAW_BYTE_STRING = ValidAcceptHeaders.RAW_BYTES # Use raw bytes in response |
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.
| class RequestedResponseFormats(StrEnum): | |
| DICT = ValidAcceptHeaders.JSON # Convert to dict using Response.json() | |
| DECODED_STRING = ValidAcceptHeaders.PLAIN_TEXT # Use utf-8 decoding in response | |
| RAW_BYTE_STRING = ValidAcceptHeaders.RAW_BYTES # Use raw bytes in response | |
| response_type: dict[type, ValidAcceptHeaders] = defaultdict(lambda: PLAIN_TEXT, {dict: JSON, str: PLAIN_TEXT, byte: RAW_BYTES}) | |
If you just want to be able to pass the desired return type into the request instead, this could be moved inside get_file_contents?
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.
Thanks, this works well. The downside is that it does mean we lose the behaviour where it will just give you the output in bytes if it fails to convert to whatever you asked, since the validation here will fail.
Having this validation instead of defaulting to bytes may be better anyway though, what do you think?
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.
No, because then you'd need -> T | bytes and to check every time you call it. I would have try -> T catch ValidationException try -> bytes as something the client can do, with the cached return from the server. I also would probably prefer to accept a str rather than bytes? How often are you getting something other than text?
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.
As discussed, so that we can have nice static typing on this method, we will leave it to the user to do any try's with different types
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.
For the sake of easier reviews, I'm going to push this change into a new PR, see #86
Fixes #67
Notes for reviewers:
daq-config-server .pytest .http://localhost:8556/docsand try out get_configuration with the test filepath/workspaces/daq-config-server/tests/test_data/beamline_parameters.txt, and try out the three different accepted "Media types" on the dropdown box