Files
puregym-unofficial-dotnet/PureGym/Api.fs
Smaug123 6e2cf29fc0
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/pr/build Pipeline failed
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/all-checks-complete unknown status
Split out cache
2024-01-29 23:01:46 +00:00

44 lines
1.5 KiB
Forth

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. 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 ct
let client = new HttpClient ()
return PureGymApi.make (fun () -> $"Bearer %s{token.AccessToken}") client
}