Skip to content

Conversation

@olliesilvester
Copy link
Collaborator

@olliesilvester olliesilvester commented May 21, 2025

Fixes #67

Notes for reviewers:

  • I think the Enum names are a bit confusing - not sure if a user would prefer to request their data using MIME names or what I currently have (dict, raw_byte_string, decoded_string)
  • Can check the unit + system tests pass by:
    • Open code in dev container
    • in a terminal in dev container run the config server with daq-config-server .
    • in a separate terminal in dev container run pytest .
  • With the config server running locally, go to http://localhost:8556/docs and 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

@codecov
Copy link

codecov bot commented May 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.23%. Comparing base (e8c3643) to head (9f36624).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@olliesilvester olliesilvester changed the base branch from main to 65_create_main_endpoint May 21, 2025 15:09

return content

def get_file_contents(
Copy link
Collaborator Author

@olliesilvester olliesilvester May 21, 2025

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):
Copy link
Collaborator Author

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

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

Base automatically changed from 65_create_main_endpoint to main May 22, 2025 10:36
@olliesilvester olliesilvester marked this pull request as ready for review May 22, 2025 15:37
@olliesilvester olliesilvester changed the title Specify format of file contents Request format of file contents using headers May 22, 2025
[tool.setuptools_scm]
version_file = "src/daq_config_server/_version.py"

[tool.pyright]
Copy link
Collaborator Author

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

Copy link
Contributor

@DiamondJoseph DiamondJoseph left a 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

Copy link

@stan-dot stan-dot left a 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

Comment on lines +38 to +63
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"},
},
},
},

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?

Copy link
Collaborator Author

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

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(

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?

Copy link
Contributor

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):

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

Comment on lines +15 to +18
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
Copy link
Contributor

@DiamondJoseph DiamondJoseph May 28, 2025

Choose a reason for hiding this comment

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

Suggested change
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?

Copy link
Collaborator Author

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?

Copy link
Contributor

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?

Copy link
Collaborator Author

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

Copy link
Collaborator Author

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

@olliesilvester olliesilvester merged commit fc4a119 into main May 29, 2025
21 checks passed
@olliesilvester olliesilvester deleted the 67_format_json_files branch May 29, 2025 12: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.

get_configuration should format output on commonly-used file types

4 participants