Add /whoami API endpoint

pull/1/head
Bram van den Heuvel 2023-03-24 14:56:31 +01:00
parent 4de93e9039
commit c45ecd2da3
10 changed files with 276 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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 }

View File

@ -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)

View File

@ -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

View File

@ -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 }