Skip to content

Commit

Permalink
Update RefitRunner.cs
Browse files Browse the repository at this point in the history
Added example of using Refit with query parameters rather than just routing
  • Loading branch information
SteveDunn authored Oct 22, 2024
1 parent adcf610 commit 54633f4
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions samples/WebApplicationConsumer/RefitExample/RefitRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public static async Task Run()
await GetWeatherForecast(City.From("Peckham"));
Console.WriteLine("=============");

Console.WriteLine("======Weather forecast using URL parameters... =======");

await GetWeatherForecastUsingUrlParameters(City.From("London"));
Console.WriteLine("=============");

Console.WriteLine("======Historical weather forecast... =======");

await GetHistoricalWeatherForecast(HistoricForecastId.FromNewGuid());
Expand All @@ -42,6 +47,22 @@ async Task GetWeatherForecast(City city)
}
}

async Task GetWeatherForecastUsingUrlParameters(City city)
{
try
{
var forecasts = await api.GetWeatherForecastByCityUsingUrlParameters(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}");
}
}

async Task GetHistoricalWeatherForecast(HistoricForecastId historicForecastId)
{
try
Expand All @@ -66,6 +87,9 @@ public interface IJsonPlaceholderApi
[Get("/weatherforecast/{city}")]
Task<List<WeatherForecast>> GetWeatherForecastByCity(City city);

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

[Get("/historicweatherforecast/{historicForecastId}")]
Task<WeatherForecast> GetHistoricForecastId(HistoricForecastId historicForecastId);
}

0 comments on commit 54633f4

Please sign in to comment.