Skip to content

Commit

Permalink
Retrieve and parse BTC price from CoinDesk
Browse files Browse the repository at this point in the history
  • Loading branch information
webwarrior-ws authored and siwatanejo committed Mar 1, 2024
1 parent 8908d0f commit 0aac0c3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
3 changes: 1 addition & 2 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ base: core22 # the base snap is the execution environment for this snap
version: '0.1' # just for humans, typically '1.2+git' or '1.3.2'
summary: HelloWorld F# app with snap # 79 char long summary
description: |
HelloWorld F# app, that takes dependency on dotnet6, and is packaged
with Ubuntu-22.04 using snap and dotnet plugin.
Parse CoinDesk API to show current BTC price.
grade: devel # must be 'stable' to release into candidate/stable channels
confinement: devmode # use 'strict' once you have the right plugs and slots
Expand Down
4 changes: 4 additions & 0 deletions src/PackWallet/PackWallet.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="FSharp.Data" Version="6.3.0" />
</ItemGroup>

</Project>
45 changes: 42 additions & 3 deletions src/PackWallet/Program.fs
Original file line number Diff line number Diff line change
@@ -1,14 +1,53 @@
open System.Net.Http
open FSharp.Data

type CoindDeskProvider = JsonProvider<"""
{
"time": {
"updated": "Feb 25, 2024 12:27:26 UTC",
"updatedISO": "2024-02-25T12:27:26+00:00",
"updateduk": "Feb 25, 2024 at 12:27 GMT"
},
"disclaimer":"This data was produced from the CoinDesk Bitcoin Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org",
"chartName":"Bitcoin",
"bpi": {
"USD": {
"code": "USD",
"symbol": "&#36;",
"rate": "51,636.062",
"description": "United States Dollar",
"rate_float": 51636.0621
},
"GBP": {
"code": "GBP",
"symbol": "&pound;",
"rate": "40,725.672",
"description": "British Pound Sterling",
"rate_float": 40725.672
},
"EUR": {
"code":"EUR",
"symbol": "&euro;",
"rate":"47,654.25",
"description": "Euro",
"rate_float": 47654.2504
}
}
}
""">

let webClient = new HttpClient()

let address = "https://example.com"
let address = "https://api.coindesk.com/v1/bpi/currentprice.json"

printfn "Retrieving contents of %s\n" address
printfn "Retrieving contents of %s ...\n" address

let contents =
webClient.GetStringAsync address
|> Async.AwaitTask
|> Async.RunSynchronously

printfn "%s" contents

let json = CoindDeskProvider.Parse contents

printfn "BTC price is %.2f USD" json.Bpi.Usd.Rate

0 comments on commit 0aac0c3

Please sign in to comment.