fortran-lang / http-client

http-client offers a user-friendly, high-level API to make HTTP requests in Fortran.
https://http-client.fortran-lang.org/
MIT License
59 stars 8 forks source link

Discussion about the storage of headers #11

Closed rajkumardongre closed 1 year ago

rajkumardongre commented 1 year ago

Hi, I am considering having two properties in our response_type for storing headers. The first property, header_string, would store headers as a plain string without any processing(as it is from the fortran-curl). The second property, header, would store headers in a key-value pair inside a hashmap using the fhash library. With these two properties, users could access headers in two ways:

response%header_string

and

response%header

I have added an example of this in my recent commit for further insight.

I would like to hear your thoughts and suggestions on this approach.

milancurcic commented 1 year ago

I suggest hiding header_string and header from the user by making them private.

Rationale:

  1. Having to parse header_string yourself is not the nicest UX; I think we can do better.
  2. Iterating over the hashmap is not the nicest UX either because the user would need to learn the hashmap API. I think hashmap should be an internal dependency and hidden from the user.

Instead, consider making a small API as part of the response_type to get all header keys, as well as to get the value of a specific header. For example:

response%header_keys()  ! returns an array of strings

There are options for how to return an array of strings. It could be an array of character variables that have the length of the longest header key, and the shorter ones are space padded. It could also by an array of string_type variables from stdlib.

To get the specific header value, we'd have a function:

response%header_value(key)  ! accepts a string and returns a string

Although some header values can be non-strings (typically integer numbers), here I suggest simply returning a character string as the result (even for numbers, e.g. "1234" instead of 1234) so that we don't force polymorphic type guarding on the user. Instead, at least to start, the user always gets the string and they can convert it to numerical values if they need that.

rajkumardongre commented 1 year ago

Thanks @milancurcic, I fully agree with the points mentioned earlier. Making the header_key() procedure return an array of strings would greatly enhance its usability for users.

milancurcic commented 1 year ago

To implement this feature, I suggest relying on stdlib's string_type (see https://stdlib.fortran-lang.org/page/specs/stdlib_string_type.html).

As of v0.9.0 (released today), fpm supports metapackages, so stdlib can be provided like this: https://fpm.fortran-lang.org/en/spec/metapackages.html#fortran-lang-standard-library-stdlib.

So header_keys will return an array of type(string_type), and header_value(key) can accept key as type(string) or character(:), and returns a character(:).