reactiveui / refit

The automatic type-safe REST library for .NET Core, Xamarin and .NET. Heavily inspired by Square's Retrofit library, Refit turns your REST API into a live interface.
https://reactiveui.github.io/refit/
MIT License
8.58k stars 747 forks source link

JSON Lines support #1576

Open dropsonic opened 1 year ago

dropsonic commented 1 year ago

Is your feature request related to a problem? Please describe.

Some APIs provide batching via sending multiple records in the JSON Lines format. It is a set of JSON documents with a line break as a separator.

An example of such API is the Typesense API for importing multiple documents:

curl "http://localhost:8108/collections/companies/documents/import?action=create" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -H "Content-Type: text/plain" \
        -X POST \
        -d '{"id": "124","company_name": "Stark Industries","num_employees": 5215,"country": "USA"}
            {"id": "125","company_name": "Acme Corp","num_employees": 2133,"country": "CA"}'

Having JSON Lines support for the HTTP request body in Refit would be nice.

Describe the solution you'd like Marking a collection parameter in the method will do:

[Post("/collections/companies/documents/import")]
public Task InsertDocuments(string collectionName, [JsonLines] IEnumerable<Company> documents);

Describe alternatives you've considered Completely replacing the JSON formatter. However, it is quite tedious.

Describe suggestions on how to achieve the feature

Additional context

anaisbetts commented 1 year ago

While Refit doesn't do this now, you could fake it by writing the "official" Refit method as just passing a string, and add an Extension Method to the interface that JSON formats the elements, joins it on "\n", then calls the string version