This document contains information regarding speculative implementations for a .NET wrapper for the Threads API. This collection of documents primarily answers the following question:
How would you handle getting responses from a web API in a strongly typed programming language where you can specify which fields you want to retrieve? And why?
1️⃣ Get all the possible fields & return it.
2️⃣ Make all the optional fields nullable in the model, and allow the library consumer to decide which fields they want to be non-null.
3️⃣ Return the JSON nodes directly (depends on the JSON lib)
4️⃣ Separate partial from full, both library & model side.
Documentation for proposals and explanation including pros and cons
This approach aims to provide the flexibility of querying specific fields while maintaining some level of type safety and readability.
- Query API with Specific Fields: When making an API request, specify the fields you need. For example:
{
"query": ["username", "email"]
}
-
Dynamic Model Generation: Rather than defining a static model for each possible API response, dynamically generate a model based on the requested fields.
-
Use Strong Typing with Optional Fields: Define a base model with common fields and extend it with optional fields dynamically. This can be achieved using a combination of dictionaries and generic methods to provide type safety without sacrificing flexibility.
- Flexibility: Easily adapt to changes in the API by querying only the required fields.
- Type Safety: Use generic methods to provide some level of type checking.
- Readability: More readable than raw dictionaries, as you can abstract field access logic.
Pro | Con | |
---|---|---|
vs. Dictionary Approach | Adds type safety and readability | Slightly more complex than a raw dictionary but more maintainable. |
vs. Nullable Fields Model Approach | More flexible and less verbose, especially with many optional fields. | Slightly less type-safe but balances flexibility and type safety better. |
@itsWindows11 here are a few proposals for response handlers. Enjoy! 😄