From 27b3fc562ec5ce2ec6f0f4c1c870c538903af7d6 Mon Sep 17 00:00:00 2001 From: Bram van den Heuvel Date: Tue, 14 Mar 2023 22:31:55 +0100 Subject: [PATCH] Eliminate unused code --- src/Internal/Api/Chain.elm | 10 +- src/Internal/Api/Helpers.elm | 30 +++- src/Internal/Api/Request.elm | 2 + src/Internal/Api/VaultUpdate.elm | 7 +- src/Internal/Tools/Exceptions.elm | 6 +- src/Internal/Tools/ValueGetter.elm | 271 ----------------------------- 6 files changed, 41 insertions(+), 285 deletions(-) delete mode 100644 src/Internal/Tools/ValueGetter.elm diff --git a/src/Internal/Api/Chain.elm b/src/Internal/Api/Chain.elm index ddf9301..0b1a390 100644 --- a/src/Internal/Api/Chain.elm +++ b/src/Internal/Api/Chain.elm @@ -30,6 +30,7 @@ type as a message to the Credentials to update certain information. -} +import Internal.Api.Helpers as Helpers import Internal.Tools.Context as Context exposing (Context) import Internal.Tools.Exceptions as X import Task exposing (Task) @@ -120,10 +121,5 @@ When set to 1 or lower, the task will only try once. -} tryNTimes : Int -> TaskChain u a b -> TaskChain u a b tryNTimes n f context = - if n <= 1 then - f context - - else - (\_ -> tryNTimes (n - 1) f context) - |> Task.onError - |> (|>) (f context) + f context + |> Helpers.retryTask (n - 1) diff --git a/src/Internal/Api/Helpers.elm b/src/Internal/Api/Helpers.elm index 108f11c..c545692 100644 --- a/src/Internal/Api/Helpers.elm +++ b/src/Internal/Api/Helpers.elm @@ -44,10 +44,36 @@ ratelimited task = 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.Error a -> Task X.Error a retryTask n task = if n <= 0 then task else - Task.onError (\_ -> retryTask (n - 1) task) task + Task.onError + (\err -> + let + retry : Task X.Error a + retry = + retryTask (n - 1) task + in + case err of + X.InternetException (Http.BadUrl _) -> + Task.fail err + + X.InternetException _ -> + retry + + X.SDKException (X.ServerReturnsBadJSON _) -> + retry + + X.SDKException _ -> + Task.fail err + + X.ServerException _ -> + Task.fail err + + X.UnsupportedSpecVersion -> + Task.fail err + ) + task diff --git a/src/Internal/Api/Request.elm b/src/Internal/Api/Request.elm index 742800d..129a2bb 100644 --- a/src/Internal/Api/Request.elm +++ b/src/Internal/Api/Request.elm @@ -1,6 +1,7 @@ module Internal.Api.Request exposing (..) import Http +import Internal.Api.Helpers as Helpers import Internal.Tools.Context as Context exposing (Context) import Internal.Tools.Exceptions as X import Json.Decode as D @@ -111,6 +112,7 @@ toTask decoder (ApiCall data) = |> List.reverse |> List.head } + |> Helpers.ratelimited getUrl : ApiCall a -> String diff --git a/src/Internal/Api/VaultUpdate.elm b/src/Internal/Api/VaultUpdate.elm index fdb6295..c133c91 100644 --- a/src/Internal/Api/VaultUpdate.elm +++ b/src/Internal/Api/VaultUpdate.elm @@ -208,6 +208,7 @@ redact input = ) Redact.redact input + |> Chain.tryNTimes 5 {-| Send a message event to a room. @@ -223,6 +224,7 @@ sendMessageEvent input = ) SendMessageEvent.sendMessageEvent input + |> Chain.tryNTimes 5 {-| Send a state key event to a room. @@ -238,6 +240,7 @@ sendStateEvent input = ) SendStateKey.sendStateKey input + |> Chain.tryNTimes 5 {-| Sync the latest updates. @@ -259,12 +262,14 @@ sync input = -} versions : Maybe V.Versions -> TaskChain VaultUpdate { a | baseUrl : () } (VB a) versions mVersions = - case mVersions of + (case mVersions of Just vs -> withVersions vs Nothing -> getVersions + ) + |> Chain.tryNTimes 5 {-| Create a task that insert the base URL into the context. diff --git a/src/Internal/Tools/Exceptions.elm b/src/Internal/Tools/Exceptions.elm index c950cfa..7823e72 100644 --- a/src/Internal/Tools/Exceptions.elm +++ b/src/Internal/Tools/Exceptions.elm @@ -39,7 +39,6 @@ input. -} type ClientError = ServerReturnsBadJSON String - | CouldntGetTimestamp | NotSupportedYet String | NoAccessToken @@ -211,9 +210,8 @@ errorToString e = SDKException (ServerReturnsBadJSON s) -> ES.serverReturnsBadJSON s - SDKException CouldntGetTimestamp -> - ES.couldNotGetTimestamp - + -- SDKException CouldntGetTimestamp -> + -- ES.couldNotGetTimestamp ServerException (M_FORBIDDEN data) -> ES.serverSaysForbidden data.error diff --git a/src/Internal/Tools/ValueGetter.elm b/src/Internal/Tools/ValueGetter.elm deleted file mode 100644 index 4dfe90b..0000000 --- a/src/Internal/Tools/ValueGetter.elm +++ /dev/null @@ -1,271 +0,0 @@ -module Internal.Tools.ValueGetter exposing (..) - -{-| This module creates task pipelines that help gather information -in the Matrix API. - -For example, it might happen that you need to make multiple API calls: - - - Authenticate - - Log in - - Get a list of channels - - Send a message in every room - -For each of these API requests, you might need certain information like -which spec version the homeserver supports. - -This module takes care of this. That way, functions can be written simply by -saying "I need THESE values" and you will then be able to assign them to each -HTTP call that needs that value. - --} - -import Task exposing (Task) - - -{-| A ValueGetter x type takes care of values that MIGHT be available. -If a value is not available, then the task can be used to get a new value. --} -type alias ValueGetter x a = - { value : Maybe a, getValue : Task x a } - - -{-| Convert a `ValueGetter` type to a task. If a previous value has already been given, -then use that value. Otherwise, use the `getValue` task to get a new value. --} -toTask : ValueGetter x a -> Task x a -toTask { value, getValue } = - Maybe.map Task.succeed value - |> Maybe.withDefault getValue - - -withInfo : (a -> Task x result) -> ValueGetter x a -> Task x result -withInfo task info1 = - Task.andThen - (\a -> - task a - ) - (toTask info1) - - -withInfo2 : - (a -> b -> Task x result) - -> ValueGetter x a - -> ValueGetter x b - -> Task x result -withInfo2 task info1 info2 = - Task.andThen - (\a -> - Task.andThen - (\b -> - task a b - ) - (toTask info2) - ) - (toTask info1) - - -withInfo3 : - (a -> b -> c -> Task x result) - -> ValueGetter x a - -> ValueGetter x b - -> ValueGetter x c - -> Task x result -withInfo3 task info1 info2 info3 = - Task.andThen - (\a -> - Task.andThen - (\b -> - Task.andThen - (\c -> - task a b c - ) - (toTask info3) - ) - (toTask info2) - ) - (toTask info1) - - -withInfo4 : - (a -> b -> c -> d -> Task x result) - -> ValueGetter x a - -> ValueGetter x b - -> ValueGetter x c - -> ValueGetter x d - -> Task x result -withInfo4 task info1 info2 info3 info4 = - Task.andThen - (\a -> - Task.andThen - (\b -> - Task.andThen - (\c -> - Task.andThen - (\d -> - task a b c d - ) - (toTask info4) - ) - (toTask info3) - ) - (toTask info2) - ) - (toTask info1) - - -withInfo5 : - (a -> b -> c -> d -> e -> Task x result) - -> ValueGetter x a - -> ValueGetter x b - -> ValueGetter x c - -> ValueGetter x d - -> ValueGetter x e - -> Task x result -withInfo5 task info1 info2 info3 info4 info5 = - Task.andThen - (\a -> - Task.andThen - (\b -> - Task.andThen - (\c -> - Task.andThen - (\d -> - Task.andThen - (\e -> - task a b c d e - ) - (toTask info5) - ) - (toTask info4) - ) - (toTask info3) - ) - (toTask info2) - ) - (toTask info1) - - -withInfo6 : - (a -> b -> c -> d -> e -> f -> Task x result) - -> ValueGetter x a - -> ValueGetter x b - -> ValueGetter x c - -> ValueGetter x d - -> ValueGetter x e - -> ValueGetter x f - -> Task x result -withInfo6 task info1 info2 info3 info4 info5 info6 = - Task.andThen - (\a -> - Task.andThen - (\b -> - Task.andThen - (\c -> - Task.andThen - (\d -> - Task.andThen - (\e -> - Task.andThen - (\f -> - task a b c d e f - ) - (toTask info6) - ) - (toTask info5) - ) - (toTask info4) - ) - (toTask info3) - ) - (toTask info2) - ) - (toTask info1) - - -withInfo7 : - (a -> b -> c -> d -> e -> f -> g -> Task x result) - -> ValueGetter x a - -> ValueGetter x b - -> ValueGetter x c - -> ValueGetter x d - -> ValueGetter x e - -> ValueGetter x f - -> ValueGetter x g - -> Task x result -withInfo7 task info1 info2 info3 info4 info5 info6 info7 = - Task.andThen - (\a -> - Task.andThen - (\b -> - Task.andThen - (\c -> - Task.andThen - (\d -> - Task.andThen - (\e -> - Task.andThen - (\f -> - Task.andThen - (\g -> - task a b c d e f g - ) - (toTask info7) - ) - (toTask info6) - ) - (toTask info5) - ) - (toTask info4) - ) - (toTask info3) - ) - (toTask info2) - ) - (toTask info1) - - -withInfo8 : - (a -> b -> c -> d -> e -> f -> g -> h -> Task x result) - -> ValueGetter x a - -> ValueGetter x b - -> ValueGetter x c - -> ValueGetter x d - -> ValueGetter x e - -> ValueGetter x f - -> ValueGetter x g - -> ValueGetter x h - -> Task x result -withInfo8 task info1 info2 info3 info4 info5 info6 info7 info8 = - Task.andThen - (\a -> - Task.andThen - (\b -> - Task.andThen - (\c -> - Task.andThen - (\d -> - Task.andThen - (\e -> - Task.andThen - (\f -> - Task.andThen - (\g -> - Task.andThen - (\h -> - task a b c d e f g h - ) - (toTask info8) - ) - (toTask info7) - ) - (toTask info6) - ) - (toTask info5) - ) - (toTask info4) - ) - (toTask info3) - ) - (toTask info2) - ) - (toTask info1)