CompositionalIT / SAFE-Dojo

An introductory dojo to learn how to develop full stack web applications in F#
379 stars 128 forks source link

Error message to the use of the Net.WebClient class, which is deprecated as of .NET 5.0. #166

Closed mash-qbe closed 1 year ago

mash-qbe commented 1 year ago

DataAccess.fs uses the deprecated WebClient class. Have updated the code to reflect change - tested and works.

module Weather = open System.Net.Http

[<Literal>]
let Sample =
    """{"latitude":51.52,"longitude":-0.08000016,"generationtime_ms":0.27298927307128906,"utc_offset_seconds":0,"timezone":"GMT","timezone_abbreviation":"GMT","elevation":25.0,"current_weather":{"temperature":23.8,"windspeed":13.8,"winddirection":277.0,"weathercode":3.0,"time":"2022-08-19T14:00"}}"""

type OpenMeteoCurrentWeather = JsonProvider<Sample>

let getWeatherForPosition location = async {
    try
        use client = new HttpClient()

        let uri = Uri($"https://api.open-meteo.com/v1/forecast?latitude=%f{location.Latitude}&longitude=%f{location.Longitude}&current_weather=true")

        let! response = client.GetAsync(uri) |> Async.AwaitTask

        if response.IsSuccessStatusCode then
            let! json = response.Content.ReadAsStringAsync() |> Async.AwaitTask
            return (OpenMeteoCurrentWeather.Parse json).CurrentWeather
        else
            printfn "Failed to fetch weather data: %O" response.ReasonPhrase
            return (OpenMeteoCurrentWeather.Parse Sample).CurrentWeather
    with ex ->
        printfn "Failed to fetch weather data: %O" ex
        return (OpenMeteoCurrentWeather.Parse Sample).CurrentWeather
}
mattgallagher92 commented 1 year ago

Thanks for reporting this @mash-qbe! I can confirm that I see the warning.

Your code looks good to me 🙂 would you like to raise a PR?

mattgallagher92 commented 1 year ago

Note to implementer: remember to update both the master branch and the suggested-solution branch.

mash-qbe commented 1 year ago

@mattgallagher92 have PR to master - not quite sure what to do with the suggested-solution branch.

Tom-Sloboda commented 1 year ago

Closing as this change been merged to master #167