Refactored API URL (#67)

This commit is contained in:
Nicolas Lœuillet 2017-04-26 17:07:43 +02:00 committed by GitHub
parent 0aab73896a
commit d40f110b59
2 changed files with 114 additions and 14 deletions

View File

@ -49,6 +49,7 @@ import Json.Encode as Encode
import Util
import WebSocket
import List.Extra exposing (groupWhile)
import Mastodon.ApiUrl as ApiUrl
-- Types
@ -620,14 +621,14 @@ registrationEncoder registration =
register : Server -> String -> String -> String -> String -> Request AppRegistration
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.withJsonBody (appRegistrationEncoder client_name redirect_uri scope website)
getAuthorizationUrl : AppRegistration -> String
getAuthorizationUrl registration =
encodeUrl (registration.server ++ "/oauth/authorize")
encodeUrl (ApiUrl.oauthAuthorize registration.server)
[ ( "response_type", "code" )
, ( "client_id", registration.client_id )
, ( "scope", registration.scope )
@ -637,7 +638,7 @@ getAuthorizationUrl registration =
getAccessToken : AppRegistration -> AuthCode -> Request AccessTokenResult
getAccessToken registration authCode =
HttpBuilder.post (registration.server ++ "/oauth/token")
HttpBuilder.post (ApiUrl.oauthToken registration.server)
|> HttpBuilder.withExpect (Http.expectJson (accessTokenDecoder registration))
|> HttpBuilder.withJsonBody (authorizationCodeEncoder registration authCode)
@ -649,32 +650,32 @@ send tagger builder =
fetchAccount : Client -> AccountId -> Request Account
fetchAccount client accountId =
fetch client ("/api/v1/accounts/" ++ (toString accountId)) accountDecoder
fetch client (ApiUrl.account accountId) accountDecoder
fetchUserTimeline : Client -> Request (List Status)
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 =
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 =
fetch client "/api/v1/timelines/public" <| Decode.list statusDecoder
fetch client (ApiUrl.publicTimeline (Nothing)) <| Decode.list statusDecoder
fetchNotifications : Client -> Request (List Notification)
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 =
HttpBuilder.post (client.server ++ "/api/v1/statuses")
HttpBuilder.post (ApiUrl.statuses client.server)
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|> HttpBuilder.withExpect (Http.expectJson statusDecoder)
|> HttpBuilder.withJsonBody (statusRequestBodyEncoder statusRequestBody)
@ -682,28 +683,28 @@ postStatus client statusRequestBody =
reblog : Client -> Int -> Request Status
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.withExpect (Http.expectJson statusDecoder)
unreblog : Client -> Int -> Request Status
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.withExpect (Http.expectJson statusDecoder)
favourite : Client -> Int -> Request Status
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.withExpect (Http.expectJson statusDecoder)
unfavourite : Client -> Int -> Request Status
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.withExpect (Http.expectJson statusDecoder)
@ -724,7 +725,7 @@ subscribeToWebSockets client streamType message =
url =
encodeUrl
((Util.replace "https:" "wss:" client.server) ++ "/api/v1/streaming/")
(ApiUrl.streaming (Util.replace "https:" "wss:" client.server))
[ ( "access_token", client.token )
, ( "stream", type_ )
]

99
src/Mastodon/ApiUrl.elm Normal file
View 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/"