Add makeVBA TaskChain

4-compiler-bug
Bram 2024-05-24 16:19:13 +02:00
parent 3b0b3264de
commit 50b10c64ca
5 changed files with 98 additions and 2 deletions

View File

@ -0,0 +1,37 @@
module Internal.Api.Now.Api exposing (getNow)
{-|
# Now
Get the current time.
@docs getNow
-}
import Internal.Api.Api as A
import Internal.Config.Log exposing (log)
import Internal.Values.Context as Context
import Internal.Values.Envelope as E
import Task
import Time
getNow : A.TaskChain a { a | now : () }
getNow _ =
Task.map
(\now ->
{ messages = [ E.SetNow now ]
, logs =
[ "Identified current time at Unix time "
, now |> Time.posixToMillis |> String.fromInt
]
|> String.concat
|> log.debug
|> List.singleton
, contextChange = Context.setNow now
}
)
Time.now

View File

@ -98,6 +98,8 @@ type ContextAttr
-}
type Error
= InternetException Http.Error
| MissingUsername
| MissingPassword
| NoSupportedVersion
| ServerReturnsBadJSON String
| ServerReturnsError String Json.Value

View File

@ -21,6 +21,8 @@ up-to-date.
import Internal.Api.BaseUrl.Api
import Internal.Api.Chain as C
import Internal.Api.LoginWithUsernameAndPassword.Api
import Internal.Api.Now.Api
import Internal.Api.Request as Request
import Internal.Api.Versions.Api
import Internal.Config.Log exposing (Log, log)
@ -51,6 +53,38 @@ type alias UFTask a b =
C.TaskChain Request.Error (EnvelopeUpdate VaultUpdate) a b
{-| Get an access token to talk to the Matrix API
-}
getAccessToken : UFTask { a | now : () } { a | accessToken : (), now : () }
getAccessToken c =
case Context.fromApiFormat c of
context ->
case ( Context.mostPopularToken context, context.username, context.password ) of
( Just a, _, _ ) ->
C.succeed
{ messages = []
, logs = [ log.debug "Using cached access token from Vault" ]
, contextChange = Context.setAccessToken a
}
c
( Nothing, Just u, Just p ) ->
Internal.Api.LoginWithUsernameAndPassword.Api.loginWithUsernameAndPassword
{ deviceId = Context.fromApiFormat c |> .deviceId
, enableRefreshToken = Just True -- TODO: Turn this into a setting
, initialDeviceDisplayName = Nothing -- TODO: Turn this into a setting
, password = p
, username = u
}
c
( Nothing, Nothing, _ ) ->
C.fail Request.MissingUsername c
( Nothing, Just _, Nothing ) ->
C.fail Request.MissingPassword c
{-| Get the base URL where the Matrix API can be accessed
-}
getBaseUrl : UFTask a { a | baseUrl : () }
@ -70,6 +104,13 @@ getBaseUrl c =
c
{-| Get the current timestamp
-}
getNow : UFTask { a | baseUrl : () } { a | baseUrl : (), now : () }
getNow =
Internal.Api.Now.Api.getNow
{-| Get the versions that are potentially supported by the Matrix API
-}
getVersions : UFTask { a | baseUrl : () } { a | baseUrl : (), versions : () }
@ -95,6 +136,17 @@ makeVB =
C.andThen getVersions getBaseUrl
{-| Establish a Task Chain context where the base URL and supported list of
versions are known, and where an access token is available to make an
authenticated API call.
-}
makeVBA : UFTask a { a | accessToken : (), baseUrl : (), now : (), versions : () }
makeVBA =
makeVB
|> C.andThen getNow
|> C.andThen getAccessToken
{-| Transform a completed task into a Cmd.
-}
run : (Backpack -> msg) -> Task -> APIContext {} -> Cmd msg

View File

@ -1,5 +1,5 @@
module Internal.Values.Context exposing
( Context, AccessToken, init, coder, encode, decoder
( Context, AccessToken, init, coder, encode, decoder, mostPopularToken
, APIContext, apiFormat, fromApiFormat
, setAccessToken, getAccessToken
, setBaseUrl, getBaseUrl
@ -15,7 +15,7 @@ the Matrix API.
## Context
@docs Context, AccessToken, init, coder, encode, decoder
@docs Context, AccessToken, init, coder, encode, decoder, mostPopularToken
## APIContext

View File

@ -53,6 +53,7 @@ import Internal.Config.Log exposing (Log)
import Internal.Config.Text as Text
import Internal.Tools.Hashdict as Hashdict
import Internal.Tools.Json as Json
import Internal.Tools.Timestamp exposing (Timestamp)
import Internal.Values.Context as Context exposing (AccessToken, Context, Versions)
import Internal.Values.Settings as Settings
@ -80,6 +81,7 @@ type EnvelopeUpdate a
| SetAccessToken AccessToken
| SetBaseUrl String
| SetDeviceId String
| SetNow Timestamp
| SetRefreshToken String
| SetVersions Versions
@ -318,6 +320,9 @@ update updateContent eu ({ context } as data) =
SetDeviceId d ->
{ data | context = { context | deviceId = Just d } }
SetNow n ->
{ data | context = { context | now = Just n } }
SetRefreshToken r ->
{ data | context = { context | refreshToken = Just r } }