Refactored API URL (#67)
This commit is contained in:
parent
0aab73896a
commit
d40f110b59
@ -49,6 +49,7 @@ import Json.Encode as Encode
|
|||||||
import Util
|
import Util
|
||||||
import WebSocket
|
import WebSocket
|
||||||
import List.Extra exposing (groupWhile)
|
import List.Extra exposing (groupWhile)
|
||||||
|
import Mastodon.ApiUrl as ApiUrl
|
||||||
|
|
||||||
|
|
||||||
-- Types
|
-- Types
|
||||||
@ -620,14 +621,14 @@ registrationEncoder registration =
|
|||||||
|
|
||||||
register : Server -> String -> String -> String -> String -> Request AppRegistration
|
register : Server -> String -> String -> String -> String -> Request AppRegistration
|
||||||
register server client_name redirect_uri scope website =
|
register server client_name redirect_uri scope website =
|
||||||
HttpBuilder.post (server ++ "/api/v1/apps")
|
HttpBuilder.post (ApiUrl.apps server)
|
||||||
|> HttpBuilder.withExpect (Http.expectJson (appRegistrationDecoder server scope))
|
|> HttpBuilder.withExpect (Http.expectJson (appRegistrationDecoder server scope))
|
||||||
|> HttpBuilder.withJsonBody (appRegistrationEncoder client_name redirect_uri scope website)
|
|> HttpBuilder.withJsonBody (appRegistrationEncoder client_name redirect_uri scope website)
|
||||||
|
|
||||||
|
|
||||||
getAuthorizationUrl : AppRegistration -> String
|
getAuthorizationUrl : AppRegistration -> String
|
||||||
getAuthorizationUrl registration =
|
getAuthorizationUrl registration =
|
||||||
encodeUrl (registration.server ++ "/oauth/authorize")
|
encodeUrl (ApiUrl.oauthAuthorize registration.server)
|
||||||
[ ( "response_type", "code" )
|
[ ( "response_type", "code" )
|
||||||
, ( "client_id", registration.client_id )
|
, ( "client_id", registration.client_id )
|
||||||
, ( "scope", registration.scope )
|
, ( "scope", registration.scope )
|
||||||
@ -637,7 +638,7 @@ getAuthorizationUrl registration =
|
|||||||
|
|
||||||
getAccessToken : AppRegistration -> AuthCode -> Request AccessTokenResult
|
getAccessToken : AppRegistration -> AuthCode -> Request AccessTokenResult
|
||||||
getAccessToken registration authCode =
|
getAccessToken registration authCode =
|
||||||
HttpBuilder.post (registration.server ++ "/oauth/token")
|
HttpBuilder.post (ApiUrl.oauthToken registration.server)
|
||||||
|> HttpBuilder.withExpect (Http.expectJson (accessTokenDecoder registration))
|
|> HttpBuilder.withExpect (Http.expectJson (accessTokenDecoder registration))
|
||||||
|> HttpBuilder.withJsonBody (authorizationCodeEncoder registration authCode)
|
|> HttpBuilder.withJsonBody (authorizationCodeEncoder registration authCode)
|
||||||
|
|
||||||
@ -649,32 +650,32 @@ send tagger builder =
|
|||||||
|
|
||||||
fetchAccount : Client -> AccountId -> Request Account
|
fetchAccount : Client -> AccountId -> Request Account
|
||||||
fetchAccount client accountId =
|
fetchAccount client accountId =
|
||||||
fetch client ("/api/v1/accounts/" ++ (toString accountId)) accountDecoder
|
fetch client (ApiUrl.account accountId) accountDecoder
|
||||||
|
|
||||||
|
|
||||||
fetchUserTimeline : Client -> Request (List Status)
|
fetchUserTimeline : Client -> Request (List Status)
|
||||||
fetchUserTimeline client =
|
fetchUserTimeline client =
|
||||||
fetch client "/api/v1/timelines/home" <| Decode.list statusDecoder
|
fetch client ApiUrl.homeTimeline <| Decode.list statusDecoder
|
||||||
|
|
||||||
|
|
||||||
fetchLocalTimeline : Client -> Request (List Status)
|
fetchLocalTimeline : Client -> Request (List Status)
|
||||||
fetchLocalTimeline client =
|
fetchLocalTimeline client =
|
||||||
fetch client "/api/v1/timelines/public?local=true" <| Decode.list statusDecoder
|
fetch client (ApiUrl.publicTimeline (Just "public")) <| Decode.list statusDecoder
|
||||||
|
|
||||||
|
|
||||||
fetchGlobalTimeline : Client -> Request (List Status)
|
fetchGlobalTimeline : Client -> Request (List Status)
|
||||||
fetchGlobalTimeline client =
|
fetchGlobalTimeline client =
|
||||||
fetch client "/api/v1/timelines/public" <| Decode.list statusDecoder
|
fetch client (ApiUrl.publicTimeline (Nothing)) <| Decode.list statusDecoder
|
||||||
|
|
||||||
|
|
||||||
fetchNotifications : Client -> Request (List Notification)
|
fetchNotifications : Client -> Request (List Notification)
|
||||||
fetchNotifications client =
|
fetchNotifications client =
|
||||||
fetch client "/api/v1/notifications" <| Decode.list notificationDecoder
|
fetch client (ApiUrl.notifications) <| Decode.list notificationDecoder
|
||||||
|
|
||||||
|
|
||||||
postStatus : Client -> StatusRequestBody -> Request Status
|
postStatus : Client -> StatusRequestBody -> Request Status
|
||||||
postStatus client statusRequestBody =
|
postStatus client statusRequestBody =
|
||||||
HttpBuilder.post (client.server ++ "/api/v1/statuses")
|
HttpBuilder.post (ApiUrl.statuses client.server)
|
||||||
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|
||||||
|> HttpBuilder.withExpect (Http.expectJson statusDecoder)
|
|> HttpBuilder.withExpect (Http.expectJson statusDecoder)
|
||||||
|> HttpBuilder.withJsonBody (statusRequestBodyEncoder statusRequestBody)
|
|> HttpBuilder.withJsonBody (statusRequestBodyEncoder statusRequestBody)
|
||||||
@ -682,28 +683,28 @@ postStatus client statusRequestBody =
|
|||||||
|
|
||||||
reblog : Client -> Int -> Request Status
|
reblog : Client -> Int -> Request Status
|
||||||
reblog client id =
|
reblog client id =
|
||||||
HttpBuilder.post (client.server ++ "/api/v1/statuses/" ++ (toString id) ++ "/reblog")
|
HttpBuilder.post (ApiUrl.reblog client.server id)
|
||||||
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|
||||||
|> HttpBuilder.withExpect (Http.expectJson statusDecoder)
|
|> HttpBuilder.withExpect (Http.expectJson statusDecoder)
|
||||||
|
|
||||||
|
|
||||||
unreblog : Client -> Int -> Request Status
|
unreblog : Client -> Int -> Request Status
|
||||||
unreblog client id =
|
unreblog client id =
|
||||||
HttpBuilder.post (client.server ++ "/api/v1/statuses/" ++ (toString id) ++ "/unreblog")
|
HttpBuilder.post (ApiUrl.unreblog client.server id)
|
||||||
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|
||||||
|> HttpBuilder.withExpect (Http.expectJson statusDecoder)
|
|> HttpBuilder.withExpect (Http.expectJson statusDecoder)
|
||||||
|
|
||||||
|
|
||||||
favourite : Client -> Int -> Request Status
|
favourite : Client -> Int -> Request Status
|
||||||
favourite client id =
|
favourite client id =
|
||||||
HttpBuilder.post (client.server ++ "/api/v1/statuses/" ++ (toString id) ++ "/favourite")
|
HttpBuilder.post (ApiUrl.favourite client.server id)
|
||||||
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|
||||||
|> HttpBuilder.withExpect (Http.expectJson statusDecoder)
|
|> HttpBuilder.withExpect (Http.expectJson statusDecoder)
|
||||||
|
|
||||||
|
|
||||||
unfavourite : Client -> Int -> Request Status
|
unfavourite : Client -> Int -> Request Status
|
||||||
unfavourite client id =
|
unfavourite client id =
|
||||||
HttpBuilder.post (client.server ++ "/api/v1/statuses/" ++ (toString id) ++ "/unfavourite")
|
HttpBuilder.post (ApiUrl.unfavourite client.server id)
|
||||||
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|
||||||
|> HttpBuilder.withExpect (Http.expectJson statusDecoder)
|
|> HttpBuilder.withExpect (Http.expectJson statusDecoder)
|
||||||
|
|
||||||
@ -724,7 +725,7 @@ subscribeToWebSockets client streamType message =
|
|||||||
|
|
||||||
url =
|
url =
|
||||||
encodeUrl
|
encodeUrl
|
||||||
((Util.replace "https:" "wss:" client.server) ++ "/api/v1/streaming/")
|
(ApiUrl.streaming (Util.replace "https:" "wss:" client.server))
|
||||||
[ ( "access_token", client.token )
|
[ ( "access_token", client.token )
|
||||||
, ( "stream", type_ )
|
, ( "stream", type_ )
|
||||||
]
|
]
|
||||||
|
99
src/Mastodon/ApiUrl.elm
Normal file
99
src/Mastodon/ApiUrl.elm
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
module Mastodon.ApiUrl
|
||||||
|
exposing
|
||||||
|
( apps
|
||||||
|
, oauthAuthorize
|
||||||
|
, oauthToken
|
||||||
|
, account
|
||||||
|
, homeTimeline
|
||||||
|
, publicTimeline
|
||||||
|
, notifications
|
||||||
|
, statuses
|
||||||
|
, reblog
|
||||||
|
, unreblog
|
||||||
|
, favourite
|
||||||
|
, unfavourite
|
||||||
|
, streaming
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
type alias Server =
|
||||||
|
String
|
||||||
|
|
||||||
|
|
||||||
|
apps : Server -> String
|
||||||
|
apps server =
|
||||||
|
server ++ "/api/v1/apps"
|
||||||
|
|
||||||
|
|
||||||
|
oauthAuthorize : Server -> String
|
||||||
|
oauthAuthorize server =
|
||||||
|
server ++ "/oauth/authorize"
|
||||||
|
|
||||||
|
|
||||||
|
oauthToken : Server -> String
|
||||||
|
oauthToken server =
|
||||||
|
server ++ "/oauth/token"
|
||||||
|
|
||||||
|
|
||||||
|
accounts : String
|
||||||
|
accounts =
|
||||||
|
"/api/v1/accounts/"
|
||||||
|
|
||||||
|
|
||||||
|
account : Int -> String
|
||||||
|
account id =
|
||||||
|
accounts ++ (toString id)
|
||||||
|
|
||||||
|
|
||||||
|
homeTimeline : String
|
||||||
|
homeTimeline =
|
||||||
|
"/api/v1/timelines/home"
|
||||||
|
|
||||||
|
|
||||||
|
publicTimeline : Maybe String -> String
|
||||||
|
publicTimeline local =
|
||||||
|
let
|
||||||
|
isLocal =
|
||||||
|
case local of
|
||||||
|
Just local ->
|
||||||
|
"?local=true"
|
||||||
|
|
||||||
|
Nothing ->
|
||||||
|
""
|
||||||
|
in
|
||||||
|
"/api/v1/timelines/public" ++ isLocal
|
||||||
|
|
||||||
|
|
||||||
|
notifications : String
|
||||||
|
notifications =
|
||||||
|
"/api/v1/notifications"
|
||||||
|
|
||||||
|
|
||||||
|
statuses : Server -> String
|
||||||
|
statuses server =
|
||||||
|
server ++ "/api/v1/statuses"
|
||||||
|
|
||||||
|
|
||||||
|
reblog : Server -> Int -> String
|
||||||
|
reblog server id =
|
||||||
|
statuses server ++ (toString id) ++ "/reblog"
|
||||||
|
|
||||||
|
|
||||||
|
unreblog : Server -> Int -> String
|
||||||
|
unreblog server id =
|
||||||
|
statuses server ++ (toString id) ++ "/unreblog"
|
||||||
|
|
||||||
|
|
||||||
|
favourite : Server -> Int -> String
|
||||||
|
favourite server id =
|
||||||
|
statuses server ++ (toString id) ++ "/favourite"
|
||||||
|
|
||||||
|
|
||||||
|
unfavourite : Server -> Int -> String
|
||||||
|
unfavourite server id =
|
||||||
|
statuses server ++ (toString id) ++ "/unfavourite"
|
||||||
|
|
||||||
|
|
||||||
|
streaming : Server -> String
|
||||||
|
streaming server =
|
||||||
|
server ++ "/api/v1/streaming/"
|
Loading…
Reference in New Issue
Block a user