Skip to content

Commit

Permalink
Added consumer to sample web app that demonstrates use in Refit
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveDunn committed Apr 20, 2024
1 parent 67350c1 commit a691840
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 9 deletions.
6 changes: 6 additions & 0 deletions Consumers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
37 changes: 28 additions & 9 deletions samples/WebApplication/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Refit;
using Vogen;

var builder = WebApplication.CreateBuilder(args);
Expand Down Expand Up @@ -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;
})
Expand All @@ -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;
})
Expand All @@ -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<string>(parsableForStrings: ParsableForStrings.GenerateMethods)]
public partial class City
{
}
}

[ValueObject]
public partial struct Farenheit
{
public static Farenheit FromCentigrade(Centigrade c) => From(32 + (int)(c.Value / 0.5556));
}

[ValueObject]
public partial struct Centigrade
{
}
1 change: 1 addition & 0 deletions samples/WebApplication/WebApplication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.2"/>
<PackageReference Include="Refit" Version="7.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
</ItemGroup>

Expand Down
7 changes: 7 additions & 0 deletions samples/WebApplicationConsumer/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// See https://aka.ms/new-console-template for more information

using RefitExample;

await new RefitRunner().Run();


70 changes: 70 additions & 0 deletions samples/WebApplicationConsumer/RefitExample/RefitRunner.cs
Original file line number Diff line number Diff line change
@@ -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<IJsonPlaceholderApi>("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<List<WeatherForecast>> GetWeatherForecast();

[Get("/weatherforecast/{city}")]
Task<List<WeatherForecast>> GetWeatherForecastByCity(City city);
}

public record WeatherForecast(DateOnly Date, Centigrade TemperatureC, Farenheit TemperatureF, string? Summary, City City)
{
}

[ValueObject<string>]
public partial class City
{
}

[ValueObject]
public partial struct Farenheit
{
}

[ValueObject]
public partial struct Centigrade
{
}
24 changes: 24 additions & 0 deletions samples/WebApplicationConsumer/WebApplicationConsumer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseLocallyBuiltPackage>true</UseLocallyBuiltPackage>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Refit" Version="7.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(UseLocallyBuiltPackage)' != ''">
<PackageReference Include="Vogen" Version="999.9.*" />
</ItemGroup>

<ItemGroup Condition=" '$(UseLocallyBuiltPackage)' == ''">
<PackageReference Include="Vogen" Version="999.9.10219943" />
</ItemGroup>


</Project>

0 comments on commit a691840

Please sign in to comment.