From c45ecd2da3eea7b6717ce50230cfbfb77e319cc0 Mon Sep 17 00:00:00 2001 From: Bram van den Heuvel Date: Fri, 24 Mar 2023 14:56:31 +0100 Subject: [PATCH] Add /whoami API endpoint --- src/Internal/Api/WhoAmI/Api.elm | 47 ++++++++++++++++++++ src/Internal/Api/WhoAmI/Main.elm | 49 +++++++++++++++++++++ src/Internal/Api/WhoAmI/V1/SpecObjects.elm | 38 ++++++++++++++++ src/Internal/Api/WhoAmI/V1/SpecObjects.yaml | 9 ++++ src/Internal/Api/WhoAmI/V2/SpecObjects.elm | 42 ++++++++++++++++++ src/Internal/Api/WhoAmI/V2/SpecObjects.yaml | 12 +++++ src/Internal/Api/WhoAmI/V2/Upcast.elm | 9 ++++ src/Internal/Api/WhoAmI/V3/SpecObjects.elm | 45 +++++++++++++++++++ src/Internal/Api/WhoAmI/V3/SpecObjects.yaml | 16 +++++++ src/Internal/Api/WhoAmI/V3/Upcast.elm | 9 ++++ 10 files changed, 276 insertions(+) create mode 100644 src/Internal/Api/WhoAmI/Api.elm create mode 100644 src/Internal/Api/WhoAmI/Main.elm create mode 100644 src/Internal/Api/WhoAmI/V1/SpecObjects.elm create mode 100644 src/Internal/Api/WhoAmI/V1/SpecObjects.yaml create mode 100644 src/Internal/Api/WhoAmI/V2/SpecObjects.elm create mode 100644 src/Internal/Api/WhoAmI/V2/SpecObjects.yaml create mode 100644 src/Internal/Api/WhoAmI/V2/Upcast.elm create mode 100644 src/Internal/Api/WhoAmI/V3/SpecObjects.elm create mode 100644 src/Internal/Api/WhoAmI/V3/SpecObjects.yaml create mode 100644 src/Internal/Api/WhoAmI/V3/Upcast.elm diff --git a/src/Internal/Api/WhoAmI/Api.elm b/src/Internal/Api/WhoAmI/Api.elm new file mode 100644 index 0000000..0be3a1b --- /dev/null +++ b/src/Internal/Api/WhoAmI/Api.elm @@ -0,0 +1,47 @@ +module Internal.Api.WhoAmI.Api exposing (..) + +import Internal.Api.Request as R +import Internal.Api.WhoAmI.V1.SpecObjects as SO1 +import Internal.Api.WhoAmI.V2.SpecObjects as SO2 +import Internal.Api.WhoAmI.V3.SpecObjects as SO3 +import Internal.Tools.Context exposing (Context) +import Internal.Tools.Exceptions as X +import Internal.Tools.SpecEnums as Enums +import Task exposing (Task) + + +type alias WhoAmIInputV1 = + () + + +type alias WhoAmIOutputV1 = + SO1.WhoAmIResponse + + +type alias WhoAmIOutputV2 = + SO2.WhoAmIResponse + + +type alias WhoAmIOutputV3 = + SO3.WhoAmIResponse + + +whoAmIV1 : WhoAmIInputV1 -> Context { a | accessToken : (), baseUrl : () } -> Task X.Error WhoAmIOutputV1 +whoAmIV1 _ = + R.callApi "GET" "/_matrix/client/r0/account/whoami" + >> R.withAttributes [ R.accessToken ] + >> R.toTask SO1.whoAmIResponseDecoder + + +whoAmIV2 : WhoAmIInputV1 -> Context { a | accessToken : (), baseUrl : () } -> Task X.Error WhoAmIOutputV2 +whoAmIV2 _ = + R.callApi "GET" "/_matrix/client/v3/account/whoami" + >> R.withAttributes [ R.accessToken ] + >> R.toTask SO2.whoAmIResponseDecoder + + +whoAmIV3 : WhoAmIInputV1 -> Context { a | accessToken : (), baseUrl : () } -> Task X.Error WhoAmIOutputV3 +whoAmIV3 _ = + R.callApi "GET" "/_matrix/client/v3/account/whoami" + >> R.withAttributes [ R.accessToken ] + >> R.toTask SO3.whoAmIResponseDecoder diff --git a/src/Internal/Api/WhoAmI/Main.elm b/src/Internal/Api/WhoAmI/Main.elm new file mode 100644 index 0000000..0f65309 --- /dev/null +++ b/src/Internal/Api/WhoAmI/Main.elm @@ -0,0 +1,49 @@ +module Internal.Api.WhoAmI.Main exposing (..) + +import Internal.Api.WhoAmI.Api as Api +import Internal.Api.WhoAmI.V2.Upcast as U2 +import Internal.Api.WhoAmI.V3.Upcast as U3 +import Internal.Tools.Context as Context exposing (Context, VBA) +import Internal.Tools.Exceptions as X +import Internal.Tools.VersionControl as VC +import Task exposing (Task) + + +whoAmI : Context (VBA a) -> WhoAmIInput -> Task X.Error WhoAmIOutput +whoAmI context input = + VC.withBottomLayer + { current = Api.whoAmIV1 + , version = "r0.3.0" + } + |> VC.sameForVersion "r0.4.0" + |> VC.sameForVersion "r0.5.0" + |> VC.sameForVersion "r0.6.0" + |> VC.sameForVersion "r0.6.1" + |> VC.addMiddleLayer + { downcast = identity + , current = Api.whoAmIV2 + , upcast = \f c -> Task.map U2.upcastWhoAmIResponse (f c) + , version = "v1.1" + } + |> VC.addMiddleLayer + { downcast = identity + , current = Api.whoAmIV3 + , upcast = \f c -> Task.map U3.upcastWhoAmIResponse (f c) + , version = "v1.2" + } + |> VC.sameForVersion "v1.3" + |> VC.sameForVersion "v1.4" + |> VC.sameForVersion "v1.5" + |> VC.sameForVersion "v1.6" + |> VC.mostRecentFromVersionList (Context.getVersions context) + |> Maybe.withDefault (always <| always <| Task.fail X.UnsupportedSpecVersion) + |> (|>) input + |> (|>) context + + +type alias WhoAmIInput = + Api.WhoAmIInputV1 + + +type alias WhoAmIOutput = + Api.WhoAmIOutputV3 diff --git a/src/Internal/Api/WhoAmI/V1/SpecObjects.elm b/src/Internal/Api/WhoAmI/V1/SpecObjects.elm new file mode 100644 index 0000000..547686b --- /dev/null +++ b/src/Internal/Api/WhoAmI/V1/SpecObjects.elm @@ -0,0 +1,38 @@ +module Internal.Api.WhoAmI.V1.SpecObjects exposing + ( WhoAmIResponse + , encodeWhoAmIResponse + , whoAmIResponseDecoder + ) + +{-| Automatically generated 'SpecObjects' + +Last generated at Unix time 1679665928 + +-} + +import Internal.Tools.EncodeExtra exposing (maybeObject) +import Json.Decode as D +import Json.Encode as E + + +{-| Response telling the user to whom their access token belongs. +-} +type alias WhoAmIResponse = + { userId : String + } + + +encodeWhoAmIResponse : WhoAmIResponse -> E.Value +encodeWhoAmIResponse data = + maybeObject + [ ( "user_id", Just <| E.string data.userId ) + ] + + +whoAmIResponseDecoder : D.Decoder WhoAmIResponse +whoAmIResponseDecoder = + D.map + (\a -> + { userId = a } + ) + (D.field "user_id" D.string) diff --git a/src/Internal/Api/WhoAmI/V1/SpecObjects.yaml b/src/Internal/Api/WhoAmI/V1/SpecObjects.yaml new file mode 100644 index 0000000..68026f0 --- /dev/null +++ b/src/Internal/Api/WhoAmI/V1/SpecObjects.yaml @@ -0,0 +1,9 @@ +version: v1 +name: SpecObjects +objects: + WhoAmIResponse: + description: Response telling the user to whom their access token belongs. + fields: + user_id: + type: string + required: true diff --git a/src/Internal/Api/WhoAmI/V2/SpecObjects.elm b/src/Internal/Api/WhoAmI/V2/SpecObjects.elm new file mode 100644 index 0000000..b25fd3d --- /dev/null +++ b/src/Internal/Api/WhoAmI/V2/SpecObjects.elm @@ -0,0 +1,42 @@ +module Internal.Api.WhoAmI.V2.SpecObjects exposing + ( WhoAmIResponse + , encodeWhoAmIResponse + , whoAmIResponseDecoder + ) + +{-| Automatically generated 'SpecObjects' + +Last generated at Unix time 1679665928 + +-} + +import Internal.Tools.DecodeExtra exposing (opField) +import Internal.Tools.EncodeExtra exposing (maybeObject) +import Json.Decode as D +import Json.Encode as E + + +{-| Response telling the user to whom their access token belongs. +-} +type alias WhoAmIResponse = + { deviceId : Maybe String + , userId : String + } + + +encodeWhoAmIResponse : WhoAmIResponse -> E.Value +encodeWhoAmIResponse data = + maybeObject + [ ( "device_id", Maybe.map E.string data.deviceId ) + , ( "user_id", Just <| E.string data.userId ) + ] + + +whoAmIResponseDecoder : D.Decoder WhoAmIResponse +whoAmIResponseDecoder = + D.map2 + (\a b -> + { deviceId = a, userId = b } + ) + (opField "device_id" D.string) + (D.field "user_id" D.string) diff --git a/src/Internal/Api/WhoAmI/V2/SpecObjects.yaml b/src/Internal/Api/WhoAmI/V2/SpecObjects.yaml new file mode 100644 index 0000000..20997dc --- /dev/null +++ b/src/Internal/Api/WhoAmI/V2/SpecObjects.yaml @@ -0,0 +1,12 @@ +version: v1 +name: SpecObjects +objects: + WhoAmIResponse: + description: Response telling the user to whom their access token belongs. + fields: + user_id: + type: string + required: true + device_id: + type: string + required: false diff --git a/src/Internal/Api/WhoAmI/V2/Upcast.elm b/src/Internal/Api/WhoAmI/V2/Upcast.elm new file mode 100644 index 0000000..ae84171 --- /dev/null +++ b/src/Internal/Api/WhoAmI/V2/Upcast.elm @@ -0,0 +1,9 @@ +module Internal.Api.WhoAmI.V2.Upcast exposing (..) + +import Internal.Api.WhoAmI.V1.SpecObjects as PO +import Internal.Api.WhoAmI.V2.SpecObjects as SO + + +upcastWhoAmIResponse : PO.WhoAmIResponse -> SO.WhoAmIResponse +upcastWhoAmIResponse old = + { userId = old.userId, deviceId = Nothing } diff --git a/src/Internal/Api/WhoAmI/V3/SpecObjects.elm b/src/Internal/Api/WhoAmI/V3/SpecObjects.elm new file mode 100644 index 0000000..8c23bd5 --- /dev/null +++ b/src/Internal/Api/WhoAmI/V3/SpecObjects.elm @@ -0,0 +1,45 @@ +module Internal.Api.WhoAmI.V3.SpecObjects exposing + ( WhoAmIResponse + , encodeWhoAmIResponse + , whoAmIResponseDecoder + ) + +{-| Automatically generated 'SpecObjects' + +Last generated at Unix time 1679665928 + +-} + +import Internal.Tools.DecodeExtra exposing (opField, opFieldWithDefault) +import Internal.Tools.EncodeExtra exposing (maybeObject) +import Json.Decode as D +import Json.Encode as E + + +{-| Response telling the user to whom their access token belongs. +-} +type alias WhoAmIResponse = + { deviceId : Maybe String + , isGuest : Bool + , userId : String + } + + +encodeWhoAmIResponse : WhoAmIResponse -> E.Value +encodeWhoAmIResponse data = + maybeObject + [ ( "device_id", Maybe.map E.string data.deviceId ) + , ( "is_guest", Just <| E.bool data.isGuest ) + , ( "user_id", Just <| E.string data.userId ) + ] + + +whoAmIResponseDecoder : D.Decoder WhoAmIResponse +whoAmIResponseDecoder = + D.map3 + (\a b c -> + { deviceId = a, isGuest = b, userId = c } + ) + (opField "device_id" D.string) + (opFieldWithDefault "is_guest" False D.bool) + (D.field "user_id" D.string) diff --git a/src/Internal/Api/WhoAmI/V3/SpecObjects.yaml b/src/Internal/Api/WhoAmI/V3/SpecObjects.yaml new file mode 100644 index 0000000..814e4bb --- /dev/null +++ b/src/Internal/Api/WhoAmI/V3/SpecObjects.yaml @@ -0,0 +1,16 @@ +version: v1 +name: SpecObjects +objects: + WhoAmIResponse: + description: Response telling the user to whom their access token belongs. + fields: + user_id: + type: string + required: true + device_id: + type: string + required: false + is_guest: + type: bool + required: false + default: false diff --git a/src/Internal/Api/WhoAmI/V3/Upcast.elm b/src/Internal/Api/WhoAmI/V3/Upcast.elm new file mode 100644 index 0000000..6b921a6 --- /dev/null +++ b/src/Internal/Api/WhoAmI/V3/Upcast.elm @@ -0,0 +1,9 @@ +module Internal.Api.WhoAmI.V3.Upcast exposing (..) + +import Internal.Api.WhoAmI.V2.SpecObjects as PO +import Internal.Api.WhoAmI.V3.SpecObjects as SO + + +upcastWhoAmIResponse : PO.WhoAmIResponse -> SO.WhoAmIResponse +upcastWhoAmIResponse old = + { deviceId = old.deviceId, isGuest = False, userId = old.userId }