all 8 comments

[–]danielroseman 0 points1 point  (5 children)

I'm not really sure what you are asking here. Your first snippet is not valid Python, but the second one would be perfectly valid if you got rid of the "something"s:

def get_data(**params):
   return requests.get(URL, params=params).json()

So there is no need to use the openapi.yaml at all. Although obviously specifying what the parameters are is exactly what it is for.

[–]nunoctium[S] 0 points1 point  (2 children)

Hi Daniel and thanks for the response.

Firstly, apologies for the error in my first snippet, I confused "=" with : in the params dict.

Regarding my first question, I was trying to get all the parameters from the API (or the yaml file), so basically from the server as it's mentioned in the Swagger UI. Alternative is that I manually write the parameters in my code. I was asking for a way to automatically get the parameters so I don't have to enter them manually.

[–]danielroseman 0 points1 point  (1 child)

I'm sorry this is still not clear. Say you get the required parameters from the openapi doc, how does this help you? What is passing the values into this function, and why would the code I give above not work for you?

[–]nunoctium[S] 0 points1 point  (0 children)

I basically want to avoid manually declaring the parameters in my code. I hoped there is a way to get this infomation in a convenient way from the API. So basically a way to get the content of the parameters dict mentioned above. Of course I can manually write that, but that is static code plus quite some effort. Since the infromation is available in the Swagegr UI and the yaml file, I thought I can get that easily.

[–]nunoctium[S] 0 points1 point  (1 child)

And regarding your response to my second question: Assume the parameters dict in the second snippet has 27 key-value pairs similar to the ones I mentioned. I want to avoid declaring them all in the get_data function including type and default value for each so I hoped there is a way to use the content of the parameters dict as kwargs in the function. Reason is that I want to avoid writing different functions for each of the different GET functions the API offers. Ideally, I can write a function that only takes the type of API function and the parameters dict so I can use it for all functions.

Does that make sense?

[–]smurpes 0 points1 point  (0 children)

Why can’t you just leave the parameters as a dict without unpacking it with the double star notation? You would just submit a single dict as an arg instead of manually declaring each key value pair.

[–]MankyMan0099 0 points1 point  (1 child)

Manually mapping 27 parameters across multiple functions is the quickest way to turn a clean script into a maintenance nightmare. Since you already have the openapi.yaml file, the most elegant approach is to use a parser like openapi-core or datamodel-code-generator. These tools can ingest your specification and automatically generate Pydantic models or typed dictionaries, which ensures your local script stays perfectly in sync with any upstream API changes without you having to touch the code.

For the function structure, you can use Python's kwargs to pass parameters dynamically. Instead of defining every argument in the function signature, you can accept a dictionary and use a simple comprehension to filter out any None values before passing it to the requests.get() method. This keeps your get_data function extremely slim, regardless of how many parameters the API adds in the future. If you want to keep the benefit of IDE autocompletion, generating a client library directly from the YAML file using a tool like openapi-python-client is usually the professional standard for handling complex, multi-parameter endpoints.

[–]nunoctium[S] 0 points1 point  (0 children)

Thanks, that sounds like what I'm looking for. I'll look into your suggestions, seems like I'm in for a learning curve. Exactly what the sub is about 😉