How reactiveui/refit works
Project type and purpose
• This is a .NET library that automatically generates type-safe REST API clients (inspired by Square’s Retrofit). Its purpose is to let developers define API interfaces (with HTTP attributes) and then have a “RestService” generate an implementation that makes HTTP calls.
• It includes support for multiple .NET targets (e.g., .NET Core, Xamarin, Blazor) with added integrations such as HttpClientFactory, JSON and XML serialization adapters, and even Roslyn-based source generation for interface stub generation.
File structure for major components
• Core Library – the “Refit” directory contains the core API classes (e.g., RestService, RequestBuilder, attributes for HTTP methods, exception handling, etc.).
• Interface Stub Generator – separate projects under InterfaceStubGenerator.Shared and the Roslyn-based versions (Roslyn38 and Roslyn41) indicate a code-generation component. This part is responsible for source generation that auto-implements the defined interfaces.
• Serialization Adapters – separate modules like Refit.Newtonsoft.Json and Refit.Xml show that the library supports different serialization strategies.
• HttpClientFactory Integration – the Refit.HttpClientFactory folder highlights integration with HttpClientFactory for dependency injection and middleware use.
• Testing and Benchmarks – projects like Refit.Tests, Refit.Benchmarks, and Refit.GeneratorTests contain test harnesses, benchmarks, and snapshot validations (for generated stubs). These help validate functionality and performance.
• Samples – under the “samples” folder there are sample projects demonstrating usage, which helps illustrate how consumers integrate with the library.
README for additional architectural insights
• The README emphasizes using interface attributes (like [Get], [Post], etc.) and how RestService turns these interfaces into live clients.
• The guidance on HttpClientFactory, DelegatingHandlers (for adding auth tokens), and exception handling points to integration components at runtime.
A. API Interface Definitions (Consumers write these)
• These are plain C# interfaces decorated with attributes (such as [Get], [Post], [Body], and [Headers]).
• They are located (and exemplified) in sample projects and referenced in tests.
B. Refit Core Library
• Contains the RestService class, RequestBuilder, and various attribute definitions.
• Provides common functionality to dynamically generate implementations based on the defined API interfaces.
• Implements the rules for querystring construction, body serialization, static/dynamic headers, etc.
C. Code Generation / Interface Stub Generation
• Represented by the InterfaceStubGenerator projects (including the Roslyn38 and Roslyn41 versions).
• Uses Roslyn-based source generators to produce the client code at compile time.
• This component feeds implementations into the Refit core so that interface calls are wired to HTTP calls.
D. Serialization Adapters
• Refit.Newtonsoft.Json for JSON serialization (with option to use Newtonsoft.Json’s settings).
• Refit.Xml for XML serialization.
• SystemTextJsonContentSerializer is built into the core by default.
E. HttpClientFactory and DelegatingHandlers Integration
• Refit.HttpClientFactory wraps RestService and integrates with ASP.NET Core’s dependency injection using HttpClientFactory.
• This shows additional middleware (like custom DelegatingHandlers) that modify requests (e.g., add authentication).
F. Test Harnesses and Benchmarking
• Refit.Tests and Refit.Benchmarks illustrate how the generated clients are validated and how performance is measured.
G. Samples and Integration Clients
• Sample client setups (like “samples/Meow.Common” and “samples/sampleUsngLocalApi”) that show how the library is used in real applications.
• Additional extension modules such as Refit.HttpClientFactory, Refit.Newtonsoft.Json, and Refit.Xml as plug-in components that augment the core client generation.
• Testing and benchmark components are shown as validating layers that help maintain code quality and performance.