Open twn39 opened 3 years ago
open System.Net.Http
open Newtonsoft.Json
open StackExchange.Redis
let fetchData = async {
let httpClient = new HttpClient()
let! response = httpClient.GetAsync("http://api.example.tech") |> Async.AwaitTask
let! data = response.Content.ReadAsStringAsync() |> Async.AwaitTask
return data
}
[<Struct>]
type ResData = {
Title: string
Version: string
Stage: string
}
[<Struct>]
type ResResult = {
Code: int
Data: ResData
Msg: string
}
let redisConnect: ConnectionMultiplexer = ConnectionMultiplexer.Connect("localhost")
let db: IDatabase = redisConnect.GetDatabase()
[<EntryPoint>]
let main argv =
let result = fetchData
|> Async.RunSynchronously
|> JsonConvert.DeserializeObject<ResResult>
let data = RedisValue(result.Data.Title)
let key = RedisKey("key")
let status = db.StringSet(key, data)
printfn "Hello world %s" result.Data.Title
0 // return an integer exit code
Update:
open System.Net.Http
open System.Net.Http.Json
[<EntryPoint>]
let main argv =
let httpClient = new HttpClient()
let fetchData = async {
let! res = httpClient.GetAsync("https://example.com/json/api") |> Async.AwaitTask
let! body = res.Content.ReadFromJsonAsync() |> Async.AwaitTask
printfn $"{res.StatusCode}, body: {body}"
}
fetchData |> Async.RunSynchronously
0 // return an integer exit code