Finish files before API end functions refactor

pull/1/head
Bram van den Heuvel 2023-02-21 16:57:58 +01:00
parent 305a312b72
commit c844c94564
8 changed files with 70 additions and 31 deletions

View File

@ -9,9 +9,11 @@ import Internal.Api.Versions.Main as Versions
import Internal.Tools.Exceptions as X import Internal.Tools.Exceptions as X
import Task exposing (Task) import Task exposing (Task)
type alias Future a = type alias Future a =
Task X.Error a Task X.Error a
{-| Get a specific event from the Matrix API. {-| Get a specific event from the Matrix API.
-} -}
getEvent : List String -> Maybe (GetEvent.EventInput -> Future GetEvent.EventOutput) getEvent : List String -> Maybe (GetEvent.EventInput -> Future GetEvent.EventOutput)

View File

@ -1,9 +1,10 @@
module Internal.Api.Helpers exposing (..) module Internal.Api.Helpers exposing (..)
import Http
import Internal.Tools.Exceptions as X import Internal.Tools.Exceptions as X
import Process import Process
import Task exposing (Task) import Task exposing (Task)
import Http
{-| Sometimes, a URL endpoint might be ratelimited. In such a case, {-| Sometimes, a URL endpoint might be ratelimited. In such a case,
the homeserver tells the SDK to wait for a while and then send its response again. the homeserver tells the SDK to wait for a while and then send its response again.
@ -11,40 +12,42 @@ the homeserver tells the SDK to wait for a while and then send its response agai
ratelimited : Task X.Error a -> Task X.Error a ratelimited : Task X.Error a -> Task X.Error a
ratelimited task = ratelimited task =
task task
|> Task.onError |> Task.onError
(\e -> (\e ->
case e of case e of
X.ServerException (X.M_LIMIT_EXCEEDED { retryAfterMs }) -> X.ServerException (X.M_LIMIT_EXCEEDED { retryAfterMs }) ->
case retryAfterMs of case retryAfterMs of
Just interval -> Just interval ->
interval interval
|> (+) 1 |> (+) 1
|> toFloat |> toFloat
|> Process.sleep
|> Task.andThen (\_ -> task)
|> ratelimited
Nothing ->
Task.fail e
X.InternetException (Http.BadStatus 429) ->
1000
|> Process.sleep |> Process.sleep
|> Task.andThen (\_ -> task) |> Task.andThen (\_ -> task)
|> ratelimited |> ratelimited
Nothing -> _ ->
Task.fail e Task.fail e
)
X.InternetException (Http.BadStatus 429) ->
1000
|> Process.sleep
|> Task.andThen (\_ -> task)
|> ratelimited
_ ->
Task.fail e
)
{-| Sometimes, you don't really care if something went wrong - you just want to try again. {-| Sometimes, you don't really care if something went wrong - you just want to try again.
This task will only return an error if it went wrong on the n'th attempt. This task will only return an error if it went wrong on the n'th attempt.
-} -}
retryTask : Int -> Task x a -> Task x a retryTask : Int -> Task x a -> Task x a
retryTask n task = retryTask n task =
if n <= 0 then if n <= 0 then
task task
else
Task.onError (\_ -> retryTask (n - 1) task ) task
else
Task.onError (\_ -> retryTask (n - 1) task) task

View File

@ -5,6 +5,7 @@ import Internal.Tools.Exceptions as X
import Internal.Tools.VersionControl as VC import Internal.Tools.VersionControl as VC
import Task exposing (Task) import Task exposing (Task)
sendMessageEvent : List String -> Maybe (SendMessageEventInput -> Task X.Error SendMessageEventOutput) sendMessageEvent : List String -> Maybe (SendMessageEventInput -> Task X.Error SendMessageEventOutput)
sendMessageEvent versions = sendMessageEvent versions =
VC.withBottomLayer VC.withBottomLayer

View File

@ -5,6 +5,7 @@ import Internal.Tools.Exceptions as X
import Internal.Tools.VersionControl as VC import Internal.Tools.VersionControl as VC
import Task exposing (Task) import Task exposing (Task)
sendStateKey : List String -> Maybe (SendStateKeyInput -> Task X.Error SendStateKeyOutput) sendStateKey : List String -> Maybe (SendStateKeyInput -> Task X.Error SendStateKeyOutput)
sendStateKey versions = sendStateKey versions =
VC.withBottomLayer VC.withBottomLayer

View File

@ -37,4 +37,4 @@ serverSaysForbidden error =
unsupportedVersion : String unsupportedVersion : String
unsupportedVersion = unsupportedVersion =
"UNSUPPORTED HOMESERVER: the homeserver only supports Spec versions that this library doesn't support." "UNSUPPORTED HOMESERVER: the homeserver only supports spec versions that this library doesn't support."

View File

@ -10,12 +10,14 @@ import Internal.Tools.DecodeExtra exposing (opField)
import Json.Decode as D import Json.Decode as D
import Json.Encode as E import Json.Encode as E
{-| Errors that may return in any circumstance: {-| Errors that may return in any circumstance:
- `InternetException` Errors that the `elm/http` library might raise. - `InternetException` Errors that the `elm/http` library might raise.
- `SDKException` Errors that this SDK might raise if it doesn't like its own input - `SDKException` Errors that this SDK might raise if it doesn't like its own input
- `ServerException` Errors that the homeserver might bring - `ServerException` Errors that the homeserver might bring
- `UnsupportedSpecVersion` This SDK does not support the needed spec versions for certain operations - usually because a homeserver is extremely old. - `UnsupportedSpecVersion` This SDK does not support the needed spec versions for certain operations - usually because a homeserver is extremely old.
-} -}
type Error type Error
= InternetException Http.Error = InternetException Http.Error
@ -32,12 +34,14 @@ input.
- `CouldntGetTimestamp` The Elm core somehow failed to get the current - `CouldntGetTimestamp` The Elm core somehow failed to get the current
Unix timestamp. Unix timestamp.
- `NotSupportedYet` Some part of the SDK is intended to be implemented - but it isn't yet. - `NotSupportedYet` Some part of the SDK is intended to be implemented - but it isn't yet.
- `NoAccessToken` There is no more access token and no way of getting a new one.
-} -}
type ClientError type ClientError
= ServerReturnsBadJSON String = ServerReturnsBadJSON String
| CouldntGetTimestamp | CouldntGetTimestamp
| NotSupportedYet String | NotSupportedYet String
| NoAccessToken
{-| Potential error codes that the server may return. If the error is not a {-| Potential error codes that the server may return. If the error is not a
@ -201,7 +205,7 @@ errorDecoder name code decoder =
errorToString : Error -> String errorToString : Error -> String
errorToString e = errorToString e =
case e of case e of
UnsupportedVersion -> UnsupportedSpecVersion ->
ES.unsupportedVersion ES.unsupportedVersion
SDKException (ServerReturnsBadJSON s) -> SDKException (ServerReturnsBadJSON s) ->

View File

@ -1,7 +1,8 @@
module Internal.Tools.Timestamp exposing (Timestamp, encodeTimestamp, timestampDecoder) module Internal.Tools.Timestamp exposing (Timestamp, encodeTimestamp, generateTransactionId, timestampDecoder)
import Json.Decode as D import Json.Decode as D
import Json.Encode as E import Json.Encode as E
import Task exposing (Task)
import Time import Time
@ -21,3 +22,13 @@ encodeTimestamp =
timestampDecoder : D.Decoder Timestamp timestampDecoder : D.Decoder Timestamp
timestampDecoder = timestampDecoder =
D.map Time.millisToPosix D.int D.map Time.millisToPosix D.int
{-| Generate a transaction id from the current Unix timestamp
-}
generateTransactionId : Task x String
generateTransactionId =
Time.now
|> Task.map Time.posixToMillis
|> Task.map String.fromInt
|> Task.map ((++) "elm")

View File

@ -2,6 +2,7 @@ module Internal.Tools.VersionControl exposing
( VersionControl, withBottomLayer ( VersionControl, withBottomLayer
, MiddleLayer, addMiddleLayer , MiddleLayer, addMiddleLayer
, toDict, fromVersion, fromVersionList , toDict, fromVersion, fromVersionList
, isSupported
, mostRecentFromVersionList, sameForVersion , mostRecentFromVersionList, sameForVersion
) )
@ -62,6 +63,11 @@ you prefer to use.
@docs toDict, fromVersion, mostRecentFromVerionList, fromVersionList @docs toDict, fromVersion, mostRecentFromVerionList, fromVersionList
# Checking functions
@docs isSupported
-} -}
import Dict exposing (Dict) import Dict exposing (Dict)
@ -138,6 +144,17 @@ fromVersionList versionList vc =
versionList versionList
{-| Sometimes, you may not wish to "just" get the info.
Sometimes, all you're interested in, is whether a given version is supported.
In such a case, you can use this function to check whether a given version is supported.
-}
isSupported : String -> VersionControl a b -> Bool
isSupported version (VersionControl d) =
Dict.member version d.versions
{-| Get a dict of all available functions. {-| Get a dict of all available functions.
-} -}
toDict : VersionControl a b -> Dict String (a -> b) toDict : VersionControl a b -> Dict String (a -> b)