Skip to content

Instantly share code, notes, and snippets.

@rcmilan
Last active May 28, 2022 12:43
Show Gist options
  • Save rcmilan/c995bd8ddb91e26f0ad0ff965b95cea9 to your computer and use it in GitHub Desktop.
Save rcmilan/c995bd8ddb91e26f0ad0ff965b95cea9 to your computer and use it in GitHub Desktop.
Script em F# para consultar a pokeapi
#r "nuget: FSharp.Json"
module PokeApiModule =
open System.Net.Http
module private HttpClientModule =
let GetAsync (client:HttpClient) (endpoint:string) =
async {
let! response = client.GetAsync(endpoint) |> Async.AwaitTask
response.EnsureSuccessStatusCode() |> ignore
let! content = response.Content.ReadAsStringAsync() |> Async.AwaitTask
return content
}
let private _baseUrl = "https://pokeapi.co/api/v2/"
let GetPokemon dexNumber =
async {
use client = new HttpClient()
let endpoint = _baseUrl + "pokemon/" + dexNumber
let! pkmn = HttpClientModule.GetAsync client endpoint
return pkmn
}
type Pokemon = {
id : int
name : string
}
open System
open FSharp.Json
printf "Digite um número: " |> ignore
let num = Console.ReadLine()
let res = PokeApiModule.GetPokemon num
|> Async.RunSynchronously
|> Json.deserialize<Pokemon>
printf "%A : %A" res.id res.name
0
@rcmilan
Copy link
Author

rcmilan commented May 28, 2022

image

@rcmilan
Copy link
Author

rcmilan commented May 28, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment