Expose ApiClient and allow Allow custom ApiClient for generativeModel
Problem
Currently, generativeModel restricts the injection of a custom ApiClient primarily to "package private test-only" methods (createModelWithClient).
This prevents critical production use cases, such as:
- Middleware: Injecting middleware for server-side function calls or request transformation.
- Network Config: Managing custom proxies, timeouts, or logging.
Additionally, this restriction forces code duplication between production logic (createGenerativeModel) and test logic (createModelWithClient), which are nearly identical except for the client parameter.
Proposal
Refactor GenerativeModel to unify initialization logic and officially support a custom client.
- Refactor Internal Constructor: Update GenerativeModel._ to accept an optional ApiClient? client. If provided, use it; otherwise, instantiate the default HttpApiClient.
- Update Public API: Add an optional ApiClient? client parameter to createGenerativeModel.
- Cleanup: Deprecate or remove createModelWithClient and _constructTestModel as they become redundant.
Reasoning
- Extensibility: Enables the creation of middleware that complements current features (e.g., orchestrating server-side function calls).
- Maintainability (DRY): Removes duplicate constructor chains, ensuring testing logic mirrors production logic.
- Clarity: Signals that using a custom client is a supported feature for advanced network configurations.
Reference
Draft implementation available here: main...villecm:flutterfire:main
Expose ApiClient and allow Allow custom ApiClient for generativeModel
Problem
Currently, generativeModel restricts the injection of a custom ApiClient primarily to "package private test-only" methods (createModelWithClient).
This prevents critical production use cases, such as:
Additionally, this restriction forces code duplication between production logic (createGenerativeModel) and test logic (createModelWithClient), which are nearly identical except for the client parameter.
Proposal
Refactor GenerativeModel to unify initialization logic and officially support a custom client.
Reasoning
Reference
Draft implementation available here: main...villecm:flutterfire:main