Use WoofWare.Myriad entirely to generate the REST API (#9)
Co-authored-by: Smaug123 <patrick+github@patrickstevens.co.uk> Reviewed-on: #9
This commit is contained in:
@@ -2,26 +2,42 @@ namespace PureGym
|
||||
|
||||
open System
|
||||
open System.Net.Http
|
||||
open System.Threading
|
||||
open System.Threading.Tasks
|
||||
|
||||
/// Methods for interacting with the PureGym REST API.
|
||||
[<RequireQualifiedAccess>]
|
||||
module Api =
|
||||
/// Create a REST client, authenticated as the specified user.
|
||||
let make (auth : Auth) : IPureGymApi Task =
|
||||
|
||||
/// Create a REST client, authenticated as the specified user. Creds will be refreshed if possible as long as
|
||||
/// the returned disposable is not disposed.
|
||||
let make (auth : Auth) : (IPureGymApi * IDisposable) Task =
|
||||
let cache, getToken =
|
||||
match auth with
|
||||
| Auth.Token t ->
|
||||
{ new IDisposable with
|
||||
member _.Dispose () = ()
|
||||
},
|
||||
fun () -> t
|
||||
| Auth.User cred ->
|
||||
let cache = new Cache<_> (AuthToken.get cred, _.ExpiryTime)
|
||||
cache :> _, (fun () -> Async.RunSynchronously (cache.GetCurrentValue ()))
|
||||
|
||||
task {
|
||||
let client = new HttpClient ()
|
||||
|
||||
return PureGymApi.make (getToken >> _.AccessToken >> sprintf "Bearer %s") client, cache
|
||||
}
|
||||
|
||||
/// Create a REST client, authenticated as the specified user. Do not refresh creds.
|
||||
let makeWithoutRefresh (ct : CancellationToken) (auth : Auth) : IPureGymApi Task =
|
||||
task {
|
||||
let! token =
|
||||
match auth with
|
||||
| Auth.Token t -> Task.FromResult<_> t
|
||||
| Auth.User cred -> AuthToken.get cred
|
||||
| Auth.User cred -> AuthToken.get cred ct
|
||||
|
||||
let client = new HttpClient ()
|
||||
client.BaseAddress <- Uri "https://capi.puregym.com/api/"
|
||||
|
||||
client.DefaultRequestHeaders.Authorization <-
|
||||
Headers.AuthenticationHeaderValue ("Bearer", token.AccessToken)
|
||||
|
||||
client.DefaultRequestHeaders.Add ("User-Agent", "PureGym/1523 CFNetwork/1312 Darwin/21.0.0")
|
||||
|
||||
return PureGymApi.make client
|
||||
return PureGymApi.make (fun () -> $"Bearer %s{token.AccessToken}") client
|
||||
}
|
||||
|
Reference in New Issue
Block a user