From 44843e0ccd8ebfbd1311f70e9ce439b780ae5e76 Mon Sep 17 00:00:00 2001 From: Nicolas Perriault Date: Tue, 2 May 2017 22:05:46 +0200 Subject: [PATCH] Drop use of Server args in ApiUrl. --- src/Mastodon/ApiUrl.elm | 99 +++++++++++++++++--------------------- src/Mastodon/Http.elm | 87 ++++++++++++++++++++++----------- src/Mastodon/WebSocket.elm | 2 +- 3 files changed, 105 insertions(+), 83 deletions(-) diff --git a/src/Mastodon/ApiUrl.elm b/src/Mastodon/ApiUrl.elm index 64625cb..2f57955 100644 --- a/src/Mastodon/ApiUrl.elm +++ b/src/Mastodon/ApiUrl.elm @@ -32,19 +32,19 @@ type alias Server = String -apps : Server -> String -apps server = - server ++ "/api/v1/apps" +apps : String +apps = + "/api/v1/apps" -oauthAuthorize : Server -> String -oauthAuthorize server = - server ++ "/oauth/authorize" +oauthAuthorize : String +oauthAuthorize = + "/oauth/authorize" -oauthToken : Server -> String -oauthToken server = - server ++ "/oauth/token" +oauthToken : String +oauthToken = + "/oauth/token" accounts : String @@ -57,33 +57,24 @@ account id = accounts ++ (toString id) -follow : Server -> Int -> String -follow server id = - server ++ accounts ++ (toString id) ++ "/follow" +follow : Int -> String +follow id = + accounts ++ (toString id) ++ "/follow" -unfollow : Server -> Int -> String -unfollow server id = - server ++ accounts ++ (toString id) ++ "/unfollow" +unfollow : Int -> String +unfollow id = + accounts ++ (toString id) ++ "/unfollow" -userAccount : Server -> String -userAccount server = - server ++ accounts ++ "verify_credentials" +userAccount : String +userAccount = + accounts ++ "verify_credentials" -searchAccount : Server -> String -> Int -> Bool -> String -searchAccount server query limit resolve = - encodeUrl (server ++ accounts ++ "search") - [ ( "q", query ) - , ( "limit", toString limit ) - , ( "resolve" - , if resolve then - "true" - else - "false" - ) - ] +searchAccount : String +searchAccount = + accounts ++ "search" relationships : List Int -> String @@ -131,41 +122,41 @@ notifications = "/api/v1/notifications" -statuses : Server -> String -statuses server = - server ++ "/api/v1/statuses" +statuses : String +statuses = + "/api/v1/statuses" -context : Server -> Int -> String -context server id = - statuses server ++ "/" ++ (toString id) ++ "/context" +context : Int -> String +context id = + statuses ++ "/" ++ (toString id) ++ "/context" -reblog : Server -> Int -> String -reblog server id = - statuses server ++ "/" ++ (toString id) ++ "/reblog" +reblog : Int -> String +reblog id = + statuses ++ "/" ++ (toString id) ++ "/reblog" -status : Server -> Int -> String -status server id = - statuses server ++ "/" ++ (toString id) +status : Int -> String +status id = + statuses ++ "/" ++ (toString id) -unreblog : Server -> Int -> String -unreblog server id = - statuses server ++ "/" ++ (toString id) ++ "/unreblog" +unreblog : Int -> String +unreblog id = + statuses ++ "/" ++ (toString id) ++ "/unreblog" -favourite : Server -> Int -> String -favourite server id = - statuses server ++ "/" ++ (toString id) ++ "/favourite" +favourite : Int -> String +favourite id = + statuses ++ "/" ++ (toString id) ++ "/favourite" -unfavourite : Server -> Int -> String -unfavourite server id = - statuses server ++ "/" ++ (toString id) ++ "/unfavourite" +unfavourite : Int -> String +unfavourite id = + statuses ++ "/" ++ (toString id) ++ "/unfavourite" -streaming : Server -> String -streaming server = - server ++ "/api/v1/streaming/" +streaming : String +streaming = + "/api/v1/streaming/" diff --git a/src/Mastodon/Http.elm b/src/Mastodon/Http.elm index e89ba02..e1b9c8c 100644 --- a/src/Mastodon/Http.elm +++ b/src/Mastodon/Http.elm @@ -74,23 +74,41 @@ toResponse result = Result.mapError extractError result -fetch : Client -> String -> Decode.Decoder a -> Request a -fetch client endpoint decoder = - HttpBuilder.get (client.server ++ endpoint) - |> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token) - |> HttpBuilder.withExpect (Http.expectJson decoder) +type Method + = GET + | POST + | DELETE + + +fetch : Method -> Client -> String -> Decode.Decoder a -> Request a +fetch method client endpoint decoder = + let + request = + case method of + GET -> + HttpBuilder.get + + POST -> + HttpBuilder.post + + DELETE -> + HttpBuilder.delete + in + request (client.server ++ endpoint) + |> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token) + |> HttpBuilder.withExpect (Http.expectJson decoder) register : String -> String -> String -> String -> String -> Request AppRegistration register server client_name redirect_uri scope website = - HttpBuilder.post (ApiUrl.apps server) + HttpBuilder.post (server ++ ApiUrl.apps) |> HttpBuilder.withExpect (Http.expectJson (appRegistrationDecoder server scope)) |> HttpBuilder.withJsonBody (appRegistrationEncoder client_name redirect_uri scope website) getAuthorizationUrl : AppRegistration -> String getAuthorizationUrl registration = - encodeUrl (ApiUrl.oauthAuthorize registration.server) + encodeUrl (registration.server ++ ApiUrl.oauthAuthorize) [ ( "response_type", "code" ) , ( "client_id", registration.client_id ) , ( "scope", registration.scope ) @@ -100,7 +118,7 @@ getAuthorizationUrl registration = getAccessToken : AppRegistration -> String -> Request AccessTokenResult getAccessToken registration authCode = - HttpBuilder.post (ApiUrl.oauthToken registration.server) + HttpBuilder.post (registration.server ++ ApiUrl.oauthToken) |> HttpBuilder.withExpect (Http.expectJson (accessTokenDecoder registration)) |> HttpBuilder.withJsonBody (authorizationCodeEncoder registration authCode) @@ -112,66 +130,79 @@ send tagger builder = fetchAccount : Client -> Int -> Request Account fetchAccount client accountId = - fetch client (ApiUrl.account accountId) accountDecoder + fetch GET client (ApiUrl.account accountId) accountDecoder fetchUserTimeline : Client -> Request (List Status) fetchUserTimeline client = - fetch client ApiUrl.homeTimeline <| Decode.list statusDecoder + fetch GET client ApiUrl.homeTimeline <| Decode.list statusDecoder fetchRelationships : Client -> List Int -> Request (List Relationship) fetchRelationships client ids = - fetch client (ApiUrl.relationships ids) <| Decode.list relationshipDecoder + -- TODO: use withQueryParams + fetch GET client (ApiUrl.relationships ids) <| Decode.list relationshipDecoder fetchLocalTimeline : Client -> Request (List Status) fetchLocalTimeline client = - fetch client (ApiUrl.publicTimeline (Just "public")) <| Decode.list statusDecoder + -- TODO: use withQueryParams + fetch GET client (ApiUrl.publicTimeline (Just "public")) <| Decode.list statusDecoder fetchGlobalTimeline : Client -> Request (List Status) fetchGlobalTimeline client = - fetch client (ApiUrl.publicTimeline (Nothing)) <| Decode.list statusDecoder + -- TODO: use withQueryParams + fetch GET client (ApiUrl.publicTimeline (Nothing)) <| Decode.list statusDecoder fetchAccountTimeline : Client -> Int -> Request (List Status) fetchAccountTimeline client id = - fetch client (ApiUrl.accountTimeline id) <| Decode.list statusDecoder + fetch GET client (ApiUrl.accountTimeline id) <| Decode.list statusDecoder fetchNotifications : Client -> Request (List Notification) fetchNotifications client = - fetch client (ApiUrl.notifications) <| Decode.list notificationDecoder + fetch GET client (ApiUrl.notifications) <| Decode.list notificationDecoder fetchAccountFollowers : Client -> Int -> Request (List Account) fetchAccountFollowers client accountId = - fetch client (ApiUrl.followers accountId) <| Decode.list accountDecoder + fetch GET client (ApiUrl.followers accountId) <| Decode.list accountDecoder fetchAccountFollowing : Client -> Int -> Request (List Account) fetchAccountFollowing client accountId = - fetch client (ApiUrl.following accountId) <| Decode.list accountDecoder + fetch GET client (ApiUrl.following accountId) <| Decode.list accountDecoder searchAccounts : Client -> String -> Int -> Bool -> Request (List Account) searchAccounts client query limit resolve = - HttpBuilder.get (ApiUrl.searchAccount client.server query limit resolve) + HttpBuilder.get (client.server ++ ApiUrl.searchAccount) |> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token) |> HttpBuilder.withExpect (Http.expectJson (Decode.list accountDecoder)) + |> HttpBuilder.withQueryParams + [ ( "q", query ) + , ( "limit", toString limit ) + , ( "resolve" + , if resolve then + "true" + else + "false" + ) + ] userAccount : Client -> Request Account userAccount client = - HttpBuilder.get (ApiUrl.userAccount client.server) + HttpBuilder.get (client.server ++ ApiUrl.userAccount) |> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token) |> HttpBuilder.withExpect (Http.expectJson accountDecoder) postStatus : Client -> StatusRequestBody -> Request Status postStatus client statusRequestBody = - HttpBuilder.post (ApiUrl.statuses client.server) + HttpBuilder.post (client.server ++ ApiUrl.statuses) |> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token) |> HttpBuilder.withExpect (Http.expectJson statusDecoder) |> HttpBuilder.withJsonBody (statusRequestBodyEncoder statusRequestBody) @@ -179,55 +210,55 @@ postStatus client statusRequestBody = deleteStatus : Client -> Int -> Request Int deleteStatus client id = - HttpBuilder.delete (ApiUrl.status client.server id) + HttpBuilder.delete (client.server ++ (ApiUrl.status id)) |> HttpBuilder.withExpect (Http.expectJson <| Decode.succeed id) |> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token) context : Client -> Int -> Request Context context client id = - HttpBuilder.get (ApiUrl.context client.server id) + HttpBuilder.get (client.server ++ (ApiUrl.context id)) |> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token) |> HttpBuilder.withExpect (Http.expectJson contextDecoder) reblog : Client -> Int -> Request Status reblog client id = - HttpBuilder.post (ApiUrl.reblog client.server id) + HttpBuilder.post (client.server ++ (ApiUrl.reblog id)) |> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token) |> HttpBuilder.withExpect (Http.expectJson statusDecoder) unreblog : Client -> Int -> Request Status unreblog client id = - HttpBuilder.post (ApiUrl.unreblog client.server id) + HttpBuilder.post (client.server ++ (ApiUrl.unreblog id)) |> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token) |> HttpBuilder.withExpect (Http.expectJson statusDecoder) favourite : Client -> Int -> Request Status favourite client id = - HttpBuilder.post (ApiUrl.favourite client.server id) + HttpBuilder.post (client.server ++ (ApiUrl.favourite id)) |> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token) |> HttpBuilder.withExpect (Http.expectJson statusDecoder) unfavourite : Client -> Int -> Request Status unfavourite client id = - HttpBuilder.post (ApiUrl.unfavourite client.server id) + HttpBuilder.post (client.server ++ (ApiUrl.unfavourite id)) |> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token) |> HttpBuilder.withExpect (Http.expectJson statusDecoder) follow : Client -> Int -> Request Relationship follow client id = - HttpBuilder.post (ApiUrl.follow client.server id) + HttpBuilder.post (client.server ++ (ApiUrl.follow id)) |> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token) |> HttpBuilder.withExpect (Http.expectJson relationshipDecoder) unfollow : Client -> Int -> Request Relationship unfollow client id = - HttpBuilder.post (ApiUrl.unfollow client.server id) + HttpBuilder.post (client.server ++ (ApiUrl.unfollow id)) |> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token) |> HttpBuilder.withExpect (Http.expectJson relationshipDecoder) diff --git a/src/Mastodon/WebSocket.elm b/src/Mastodon/WebSocket.elm index aac18a5..76549ef 100644 --- a/src/Mastodon/WebSocket.elm +++ b/src/Mastodon/WebSocket.elm @@ -54,7 +54,7 @@ subscribeToWebSockets client streamType message = url = encodeUrl - (ApiUrl.streaming (replaceSlice "wss" 0 5 client.server)) + (replaceSlice "wss" 0 5 <| client.server ++ ApiUrl.streaming) [ ( "access_token", client.token ) , ( "stream", type_ ) ]