-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added consumer to sample web app that demonstrates use in Refit
- Loading branch information
Showing
6 changed files
with
136 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
70
samples/WebApplicationConsumer/RefitExample/RefitRunner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
samples/WebApplicationConsumer/WebApplicationConsumer.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |