diff --git a/Consumers.sln b/Consumers.sln index 3d9d8e8055..47f7a79f3b 100644 --- a/Consumers.sln +++ b/Consumers.sln @@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsumerTests", "tests\Cons EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplication", "samples\WebApplication\WebApplication.csproj", "{DDCC7F24-6C4B-4F6D-A216-CEB01E634BD1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplicationConsumer", "samples\WebApplicationConsumer\WebApplicationConsumer.csproj", "{CCFC28B7-3E56-4364-AAE9-83EC01FBE820}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -37,6 +39,10 @@ Global {DDCC7F24-6C4B-4F6D-A216-CEB01E634BD1}.Debug|Any CPU.Build.0 = Debug|Any CPU {DDCC7F24-6C4B-4F6D-A216-CEB01E634BD1}.Release|Any CPU.ActiveCfg = Release|Any CPU {DDCC7F24-6C4B-4F6D-A216-CEB01E634BD1}.Release|Any CPU.Build.0 = Release|Any CPU + {CCFC28B7-3E56-4364-AAE9-83EC01FBE820}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CCFC28B7-3E56-4364-AAE9-83EC01FBE820}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CCFC28B7-3E56-4364-AAE9-83EC01FBE820}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CCFC28B7-3E56-4364-AAE9-83EC01FBE820}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/samples/WebApplication/Program.cs b/samples/WebApplication/Program.cs index 9fa1e8b62a..ff6206fbf5 100644 --- a/samples/WebApplication/Program.cs +++ b/samples/WebApplication/Program.cs @@ -1,3 +1,4 @@ +using Refit; using Vogen; var builder = WebApplication.CreateBuilder(args); @@ -26,13 +27,17 @@ app.MapGet("/weatherforecast", () => { var forecast = Enumerable.Range(1, 5).Select(index => - new WeatherForecast + { + Centigrade temperatureC = Centigrade.From(Random.Shared.Next(-20, 55)); + return new WeatherForecast ( DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - Random.Shared.Next(-20, 55), + temperatureC, + Farenheit.FromCentigrade(temperatureC), summaries[Random.Shared.Next(summaries.Length)], City.From("London") - )) + ); + }) .ToArray(); return forecast; }) @@ -42,13 +47,17 @@ app.MapGet("/weatherforecast/{city}", (City city) => { var forecast = Enumerable.Range(1, 5).Select(index => - new WeatherForecast + { + Centigrade temperatureC = Centigrade.From(Random.Shared.Next(-20, 55)); + return new WeatherForecast ( DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - Random.Shared.Next(-20, 55), + temperatureC, + Farenheit.FromCentigrade(temperatureC), summaries[Random.Shared.Next(summaries.Length)], city - )) + ); + }) .ToArray(); return forecast; }) @@ -57,12 +66,22 @@ app.Run(); -record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary, City City) +record WeatherForecast(DateOnly Date, Centigrade TemperatureC, Farenheit temperatureF, string? Summary, City City) { - public int TemperatureF => 32 + (int) (TemperatureC / 0.5556); } [ValueObject(parsableForStrings: ParsableForStrings.GenerateMethods)] public partial class City { -} \ No newline at end of file +} + +[ValueObject] +public partial struct Farenheit +{ + public static Farenheit FromCentigrade(Centigrade c) => From(32 + (int)(c.Value / 0.5556)); +} + +[ValueObject] +public partial struct Centigrade +{ +} diff --git a/samples/WebApplication/WebApplication.csproj b/samples/WebApplication/WebApplication.csproj index c3d5953565..30396a68e4 100644 --- a/samples/WebApplication/WebApplication.csproj +++ b/samples/WebApplication/WebApplication.csproj @@ -9,6 +9,7 @@ + diff --git a/samples/WebApplicationConsumer/Program.cs b/samples/WebApplicationConsumer/Program.cs new file mode 100644 index 0000000000..928f0b49d6 --- /dev/null +++ b/samples/WebApplicationConsumer/Program.cs @@ -0,0 +1,7 @@ +// See https://aka.ms/new-console-template for more information + +using RefitExample; + +await new RefitRunner().Run(); + + diff --git a/samples/WebApplicationConsumer/RefitExample/RefitRunner.cs b/samples/WebApplicationConsumer/RefitExample/RefitRunner.cs new file mode 100644 index 0000000000..cb0700b278 --- /dev/null +++ b/samples/WebApplicationConsumer/RefitExample/RefitRunner.cs @@ -0,0 +1,70 @@ +using Refit; +using Vogen; + +namespace RefitExample; + +public class RefitRunner +{ + public async Task Run() + { + Console.WriteLine("Refit example"); + Console.WriteLine("============="); + var api = RestService.For("https://localhost:7033"); + + await GetWeatherForecast(City.From("London")); + Console.WriteLine("============="); + + await GetWeatherForecast(City.From("Paris")); + Console.WriteLine("============="); + + await GetWeatherForecast(City.From("Peckham")); + Console.WriteLine("============="); + + return; + + async Task GetWeatherForecast(City city) + { + try + { + + var forecasts = await api.GetWeatherForecastByCity(city); + foreach (var f in forecasts) + { + Console.WriteLine($"City: {f.City}, TempC: {f.TemperatureC} ({f.TemperatureF.Value}F) - {f.Summary}"); + } + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } + } +} + +public interface IJsonPlaceholderApi +{ + [Get("/weatherforecast")] + Task> GetWeatherForecast(); + + [Get("/weatherforecast/{city}")] + Task> GetWeatherForecastByCity(City city); +} + +public record WeatherForecast(DateOnly Date, Centigrade TemperatureC, Farenheit TemperatureF, string? Summary, City City) +{ +} + +[ValueObject] +public partial class City +{ +} + +[ValueObject] +public partial struct Farenheit +{ +} + +[ValueObject] +public partial struct Centigrade +{ +} diff --git a/samples/WebApplicationConsumer/WebApplicationConsumer.csproj b/samples/WebApplicationConsumer/WebApplicationConsumer.csproj new file mode 100644 index 0000000000..7346fbdd4f --- /dev/null +++ b/samples/WebApplicationConsumer/WebApplicationConsumer.csproj @@ -0,0 +1,24 @@ + + + + Exe + net8.0 + enable + enable + true + + + + + + + + + + + + + + + +