Add makeVBA TaskChain
							parent
							
								
									3b0b3264de
								
							
						
					
					
						commit
						50b10c64ca
					
				| 
						 | 
					@ -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
 | 
				
			||||||
| 
						 | 
					@ -98,6 +98,8 @@ type ContextAttr
 | 
				
			||||||
-}
 | 
					-}
 | 
				
			||||||
type Error
 | 
					type Error
 | 
				
			||||||
    = InternetException Http.Error
 | 
					    = InternetException Http.Error
 | 
				
			||||||
 | 
					    | MissingUsername
 | 
				
			||||||
 | 
					    | MissingPassword
 | 
				
			||||||
    | NoSupportedVersion
 | 
					    | NoSupportedVersion
 | 
				
			||||||
    | ServerReturnsBadJSON String
 | 
					    | ServerReturnsBadJSON String
 | 
				
			||||||
    | ServerReturnsError String Json.Value
 | 
					    | ServerReturnsError String Json.Value
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -21,6 +21,8 @@ up-to-date.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import Internal.Api.BaseUrl.Api
 | 
					import Internal.Api.BaseUrl.Api
 | 
				
			||||||
import Internal.Api.Chain as C
 | 
					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.Request as Request
 | 
				
			||||||
import Internal.Api.Versions.Api
 | 
					import Internal.Api.Versions.Api
 | 
				
			||||||
import Internal.Config.Log exposing (Log, log)
 | 
					import Internal.Config.Log exposing (Log, log)
 | 
				
			||||||
| 
						 | 
					@ -51,6 +53,38 @@ type alias UFTask a b =
 | 
				
			||||||
    C.TaskChain Request.Error (EnvelopeUpdate VaultUpdate) 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
 | 
					{-| Get the base URL where the Matrix API can be accessed
 | 
				
			||||||
-}
 | 
					-}
 | 
				
			||||||
getBaseUrl : UFTask a { a | baseUrl : () }
 | 
					getBaseUrl : UFTask a { a | baseUrl : () }
 | 
				
			||||||
| 
						 | 
					@ -70,6 +104,13 @@ getBaseUrl c =
 | 
				
			||||||
                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
 | 
					{-| Get the versions that are potentially supported by the Matrix API
 | 
				
			||||||
-}
 | 
					-}
 | 
				
			||||||
getVersions : UFTask { a | baseUrl : () } { a | baseUrl : (), versions : () }
 | 
					getVersions : UFTask { a | baseUrl : () } { a | baseUrl : (), versions : () }
 | 
				
			||||||
| 
						 | 
					@ -95,6 +136,17 @@ makeVB =
 | 
				
			||||||
    C.andThen getVersions getBaseUrl
 | 
					    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.
 | 
					{-| Transform a completed task into a Cmd.
 | 
				
			||||||
-}
 | 
					-}
 | 
				
			||||||
run : (Backpack -> msg) -> Task -> APIContext {} -> Cmd msg
 | 
					run : (Backpack -> msg) -> Task -> APIContext {} -> Cmd msg
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,5 @@
 | 
				
			||||||
module Internal.Values.Context exposing
 | 
					module Internal.Values.Context exposing
 | 
				
			||||||
    ( Context, AccessToken, init, coder, encode, decoder
 | 
					    ( Context, AccessToken, init, coder, encode, decoder, mostPopularToken
 | 
				
			||||||
    , APIContext, apiFormat, fromApiFormat
 | 
					    , APIContext, apiFormat, fromApiFormat
 | 
				
			||||||
    , setAccessToken, getAccessToken
 | 
					    , setAccessToken, getAccessToken
 | 
				
			||||||
    , setBaseUrl, getBaseUrl
 | 
					    , setBaseUrl, getBaseUrl
 | 
				
			||||||
| 
						 | 
					@ -15,7 +15,7 @@ the Matrix API.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Context
 | 
					## Context
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@docs Context, AccessToken, init, coder, encode, decoder
 | 
					@docs Context, AccessToken, init, coder, encode, decoder, mostPopularToken
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## APIContext
 | 
					## APIContext
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -53,6 +53,7 @@ import Internal.Config.Log exposing (Log)
 | 
				
			||||||
import Internal.Config.Text as Text
 | 
					import Internal.Config.Text as Text
 | 
				
			||||||
import Internal.Tools.Hashdict as Hashdict
 | 
					import Internal.Tools.Hashdict as Hashdict
 | 
				
			||||||
import Internal.Tools.Json as Json
 | 
					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.Context as Context exposing (AccessToken, Context, Versions)
 | 
				
			||||||
import Internal.Values.Settings as Settings
 | 
					import Internal.Values.Settings as Settings
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -80,6 +81,7 @@ type EnvelopeUpdate a
 | 
				
			||||||
    | SetAccessToken AccessToken
 | 
					    | SetAccessToken AccessToken
 | 
				
			||||||
    | SetBaseUrl String
 | 
					    | SetBaseUrl String
 | 
				
			||||||
    | SetDeviceId String
 | 
					    | SetDeviceId String
 | 
				
			||||||
 | 
					    | SetNow Timestamp
 | 
				
			||||||
    | SetRefreshToken String
 | 
					    | SetRefreshToken String
 | 
				
			||||||
    | SetVersions Versions
 | 
					    | SetVersions Versions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -318,6 +320,9 @@ update updateContent eu ({ context } as data) =
 | 
				
			||||||
        SetDeviceId d ->
 | 
					        SetDeviceId d ->
 | 
				
			||||||
            { data | context = { context | deviceId = Just d } }
 | 
					            { data | context = { context | deviceId = Just d } }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        SetNow n ->
 | 
				
			||||||
 | 
					            { data | context = { context | now = Just n } }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        SetRefreshToken r ->
 | 
					        SetRefreshToken r ->
 | 
				
			||||||
            { data | context = { context | refreshToken = Just r } }
 | 
					            { data | context = { context | refreshToken = Just r } }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue