Add all-gyms
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/build Pipeline failed
ci/woodpecker/pr/all-checks-complete unknown status

This commit is contained in:
Smaug123
2024-02-12 21:52:11 +00:00
parent f182c6ebad
commit 43c87c7955
6 changed files with 74 additions and 9 deletions

53
PureGym.App/AllGyms.fs Normal file
View File

@@ -0,0 +1,53 @@
namespace PureGym.App
open System.Threading
open Argu
open PureGym
type AllGymsArgsFragment =
| [<Unique ; EqualsAssignmentOrSpaced>] 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<AllGymsArgs, ArguParseException>
=
let terse =
match args.TryGetResult AllGymsArgsFragment.Terse with
| None -> false
| Some x -> x
{
Creds = auth
Terse = terse
}
|> Ok
[<RequireQualifiedAccess>]
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> gym)
return 0
}

View File

@@ -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))

View File

@@ -13,6 +13,7 @@
<Compile Include="Authenticate.fs" />
<Compile Include="Fullness.fs" />
<Compile Include="LookupGym.fs" />
<Compile Include="AllGyms.fs" />
<Compile Include="MemberActivity.fs" />
<Compile Include="Sessions.fs" />
<Compile Include="Program.fs" />

View File

@@ -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

View File

@@ -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

View File

@@ -6,7 +6,7 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarnOn>FS3559</WarnOn>
<WoofWareMyriadPluginVersion>1.4.8</WoofWareMyriadPluginVersion>
<WoofWareMyriadPluginVersion>2.0.5</WoofWareMyriadPluginVersion>
</PropertyGroup>
<ItemGroup>
@@ -38,6 +38,7 @@
<PackageReference Include="Fastenshtein" Version="1.0.0.8" />
<PackageReference Include="Myriad.Sdk" Version="0.8.3" PrivateAssets="all" />
<PackageReference Include="WoofWare.Myriad.Plugins" Version="$(WoofWareMyriadPluginVersion)" />
<PackageReference Include="WoofWare.Myriad.Plugins.Attributes" Version="$(WoofWareMyriadPluginVersion)" />
</ItemGroup>
</Project>