Add all-gyms
This commit is contained in:
53
PureGym.App/AllGyms.fs
Normal file
53
PureGym.App/AllGyms.fs
Normal 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
|
||||||
|
}
|
@@ -16,6 +16,10 @@ module Program =
|
|||||||
("Get information about the physical instantiation of a gym",
|
("Get information about the physical instantiation of a gym",
|
||||||
RequiresAuth (fun auth -> ArgsCrate.make (LookupGymArgs.Parse auth) LookupGym.run))
|
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",
|
"fullness",
|
||||||
("Determine how full a gym is",
|
("Determine how full a gym is",
|
||||||
RequiresAuth (fun auth -> ArgsCrate.make (FullnessArgs.Parse auth) Fullness.run))
|
RequiresAuth (fun auth -> ArgsCrate.make (FullnessArgs.Parse auth) Fullness.run))
|
||||||
|
@@ -13,6 +13,7 @@
|
|||||||
<Compile Include="Authenticate.fs" />
|
<Compile Include="Authenticate.fs" />
|
||||||
<Compile Include="Fullness.fs" />
|
<Compile Include="Fullness.fs" />
|
||||||
<Compile Include="LookupGym.fs" />
|
<Compile Include="LookupGym.fs" />
|
||||||
|
<Compile Include="AllGyms.fs" />
|
||||||
<Compile Include="MemberActivity.fs" />
|
<Compile Include="MemberActivity.fs" />
|
||||||
<Compile Include="Sessions.fs" />
|
<Compile Include="Sessions.fs" />
|
||||||
<Compile Include="Program.fs" />
|
<Compile Include="Program.fs" />
|
||||||
|
@@ -23,21 +23,23 @@ module Api =
|
|||||||
let cache = new Cache<_> (AuthToken.get cred, _.ExpiryTime)
|
let cache = new Cache<_> (AuthToken.get cred, _.ExpiryTime)
|
||||||
cache :> _, (fun () -> Async.RunSynchronously (cache.GetCurrentValue ()))
|
cache :> _, (fun () -> Async.RunSynchronously (cache.GetCurrentValue ()))
|
||||||
|
|
||||||
task {
|
async {
|
||||||
let client = new HttpClient ()
|
let client = new HttpClient ()
|
||||||
|
|
||||||
return PureGymApi.make (getToken >> _.AccessToken >> sprintf "Bearer %s") client, cache
|
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.
|
/// Create a REST client, authenticated as the specified user. Do not refresh creds.
|
||||||
let makeWithoutRefresh (ct : CancellationToken) (auth : Auth) : IPureGymApi Task =
|
let makeWithoutRefresh (ct : CancellationToken) (auth : Auth) : IPureGymApi Task =
|
||||||
task {
|
async {
|
||||||
let! token =
|
let! token =
|
||||||
match auth with
|
match auth with
|
||||||
| Auth.Token t -> Task.FromResult<_> t
|
| Auth.Token t -> async.Return t
|
||||||
| Auth.User cred -> AuthToken.get cred ct
|
| Auth.User cred -> Async.AwaitTask (AuthToken.get cred ct)
|
||||||
|
|
||||||
let client = new HttpClient ()
|
let client = new HttpClient ()
|
||||||
|
|
||||||
return PureGymApi.make (fun () -> $"Bearer %s{token.AccessToken}") client
|
return PureGymApi.make (fun () -> $"Bearer %s{token.AccessToken}") client
|
||||||
}
|
}
|
||||||
|
|> Async.StartAsTask
|
||||||
|
@@ -21,14 +21,17 @@ module GymSelector =
|
|||||||
let canonicalId (client : IPureGymApi) (gym : GymSelector) : int Task =
|
let canonicalId (client : IPureGymApi) (gym : GymSelector) : int Task =
|
||||||
match gym with
|
match gym with
|
||||||
| GymSelector.Home ->
|
| GymSelector.Home ->
|
||||||
task {
|
async {
|
||||||
let! self = client.GetMember ()
|
let! ct = Async.CancellationToken
|
||||||
|
let! self = Async.AwaitTask (client.GetMember ct)
|
||||||
return self.HomeGymId
|
return self.HomeGymId
|
||||||
}
|
}
|
||||||
|
|> Async.StartAsTask
|
||||||
| GymSelector.Id i -> Task.FromResult<_> i
|
| GymSelector.Id i -> Task.FromResult<_> i
|
||||||
| GymSelector.Name name ->
|
| GymSelector.Name name ->
|
||||||
task {
|
async {
|
||||||
let! allGyms = client.GetGyms ()
|
let! ct = Async.CancellationToken
|
||||||
|
let! allGyms = Async.AwaitTask (client.GetGyms ct)
|
||||||
|
|
||||||
if allGyms.IsEmpty then
|
if allGyms.IsEmpty then
|
||||||
return failwith "PureGym API returned no gyms!"
|
return failwith "PureGym API returned no gyms!"
|
||||||
@@ -46,3 +49,4 @@ module GymSelector =
|
|||||||
|
|
||||||
return bestGym.Id
|
return bestGym.Id
|
||||||
}
|
}
|
||||||
|
|> Async.StartAsTask
|
||||||
|
@@ -6,7 +6,7 @@
|
|||||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||||
<WarnOn>FS3559</WarnOn>
|
<WarnOn>FS3559</WarnOn>
|
||||||
|
|
||||||
<WoofWareMyriadPluginVersion>1.4.8</WoofWareMyriadPluginVersion>
|
<WoofWareMyriadPluginVersion>2.0.5</WoofWareMyriadPluginVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -38,6 +38,7 @@
|
|||||||
<PackageReference Include="Fastenshtein" Version="1.0.0.8" />
|
<PackageReference Include="Fastenshtein" Version="1.0.0.8" />
|
||||||
<PackageReference Include="Myriad.Sdk" Version="0.8.3" PrivateAssets="all" />
|
<PackageReference Include="Myriad.Sdk" Version="0.8.3" PrivateAssets="all" />
|
||||||
<PackageReference Include="WoofWare.Myriad.Plugins" Version="$(WoofWareMyriadPluginVersion)" />
|
<PackageReference Include="WoofWare.Myriad.Plugins" Version="$(WoofWareMyriadPluginVersion)" />
|
||||||
|
<PackageReference Include="WoofWare.Myriad.Plugins.Attributes" Version="$(WoofWareMyriadPluginVersion)" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
Reference in New Issue
Block a user