From 43c87c79551cd32ab016193855f1e5b4e4f43e1c Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Mon, 12 Feb 2024 21:52:11 +0000 Subject: [PATCH] Add all-gyms --- PureGym.App/AllGyms.fs | 53 ++++++++++++++++++++++++++++++++++ PureGym.App/Program.fs | 4 +++ PureGym.App/PureGym.App.fsproj | 1 + PureGym/Api.fs | 10 ++++--- PureGym/GymSelector.fs | 12 +++++--- PureGym/PureGym.fsproj | 3 +- 6 files changed, 74 insertions(+), 9 deletions(-) create mode 100644 PureGym.App/AllGyms.fs diff --git a/PureGym.App/AllGyms.fs b/PureGym.App/AllGyms.fs new file mode 100644 index 0000000..60289d4 --- /dev/null +++ b/PureGym.App/AllGyms.fs @@ -0,0 +1,53 @@ +namespace PureGym.App + +open System.Threading +open Argu +open PureGym + +type AllGymsArgsFragment = + | [] Terse of bool + + interface IArgParserTemplate with + member s.Usage = + match s with + | Terse _ -> "print only 'id,gym-name'" + +type AllGymsArgs = + { + Creds : Auth + Terse : bool + } + + static member Parse + (auth : Auth) + (args : AllGymsArgsFragment ParseResults) + : Result + = + let terse = + match args.TryGetResult AllGymsArgsFragment.Terse with + | None -> false + | Some x -> x + + { + Creds = auth + Terse = terse + } + |> Ok + +[] +module AllGyms = + + let run (args : AllGymsArgs) = + task { + let! client = Api.makeWithoutRefresh CancellationToken.None args.Creds + let! s = client.GetGyms () + + if args.Terse then + for gym in s do + System.Console.WriteLine $"%i{gym.Id},%s{gym.Name}" + else + for gym in s do + System.Console.WriteLine (string gym) + + return 0 + } diff --git a/PureGym.App/Program.fs b/PureGym.App/Program.fs index ae84bc6..22e014f 100644 --- a/PureGym.App/Program.fs +++ b/PureGym.App/Program.fs @@ -16,6 +16,10 @@ module Program = ("Get information about the physical instantiation of a gym", RequiresAuth (fun auth -> ArgsCrate.make (LookupGymArgs.Parse auth) LookupGym.run)) + "all-gyms", + ("List information about all gyms", + RequiresAuth (fun auth -> ArgsCrate.make (AllGymsArgs.Parse auth) AllGyms.run)) + "fullness", ("Determine how full a gym is", RequiresAuth (fun auth -> ArgsCrate.make (FullnessArgs.Parse auth) Fullness.run)) diff --git a/PureGym.App/PureGym.App.fsproj b/PureGym.App/PureGym.App.fsproj index 2422fb1..2914335 100644 --- a/PureGym.App/PureGym.App.fsproj +++ b/PureGym.App/PureGym.App.fsproj @@ -13,6 +13,7 @@ + diff --git a/PureGym/Api.fs b/PureGym/Api.fs index 3d1bbc3..07d85bd 100644 --- a/PureGym/Api.fs +++ b/PureGym/Api.fs @@ -23,21 +23,23 @@ module Api = let cache = new Cache<_> (AuthToken.get cred, _.ExpiryTime) cache :> _, (fun () -> Async.RunSynchronously (cache.GetCurrentValue ())) - task { + async { let client = new HttpClient () return PureGymApi.make (getToken >> _.AccessToken >> sprintf "Bearer %s") client, cache } + |> Async.StartAsTask /// Create a REST client, authenticated as the specified user. Do not refresh creds. let makeWithoutRefresh (ct : CancellationToken) (auth : Auth) : IPureGymApi Task = - task { + async { let! token = match auth with - | Auth.Token t -> Task.FromResult<_> t - | Auth.User cred -> AuthToken.get cred ct + | Auth.Token t -> async.Return t + | Auth.User cred -> Async.AwaitTask (AuthToken.get cred ct) let client = new HttpClient () return PureGymApi.make (fun () -> $"Bearer %s{token.AccessToken}") client } + |> Async.StartAsTask diff --git a/PureGym/GymSelector.fs b/PureGym/GymSelector.fs index 0694551..f8c0c73 100644 --- a/PureGym/GymSelector.fs +++ b/PureGym/GymSelector.fs @@ -21,14 +21,17 @@ module GymSelector = let canonicalId (client : IPureGymApi) (gym : GymSelector) : int Task = match gym with | GymSelector.Home -> - task { - let! self = client.GetMember () + async { + let! ct = Async.CancellationToken + let! self = Async.AwaitTask (client.GetMember ct) return self.HomeGymId } + |> Async.StartAsTask | GymSelector.Id i -> Task.FromResult<_> i | GymSelector.Name name -> - task { - let! allGyms = client.GetGyms () + async { + let! ct = Async.CancellationToken + let! allGyms = Async.AwaitTask (client.GetGyms ct) if allGyms.IsEmpty then return failwith "PureGym API returned no gyms!" @@ -46,3 +49,4 @@ module GymSelector = return bestGym.Id } + |> Async.StartAsTask diff --git a/PureGym/PureGym.fsproj b/PureGym/PureGym.fsproj index 5b72b9e..b3654ae 100644 --- a/PureGym/PureGym.fsproj +++ b/PureGym/PureGym.fsproj @@ -6,7 +6,7 @@ true FS3559 - 1.4.8 + 2.0.5 @@ -38,6 +38,7 @@ +