1
0
Fork 0

Generalize use of fetch to other Http functions.

This commit is contained in:
Nicolas Perriault 2017-05-02 22:26:13 +02:00
parent 44843e0ccd
commit ce27e30297
No known key found for this signature in database
GPG Key ID: DA5E4C83904F7A2A
1 changed files with 43 additions and 65 deletions

View File

@ -28,7 +28,7 @@ module Mastodon.Http
) )
import Http import Http
import HttpBuilder import HttpBuilder as Build
import Json.Decode as Decode import Json.Decode as Decode
import Mastodon.ApiUrl as ApiUrl import Mastodon.ApiUrl as ApiUrl
import Mastodon.Decoder exposing (..) import Mastodon.Decoder exposing (..)
@ -37,7 +37,7 @@ import Mastodon.Model exposing (..)
type alias Request a = type alias Request a =
HttpBuilder.RequestBuilder a Build.RequestBuilder a
extractMastodonError : Int -> String -> String -> Error extractMastodonError : Int -> String -> String -> Error
@ -80,30 +80,37 @@ type Method
| DELETE | DELETE
fetch : Method -> Client -> String -> Decode.Decoder a -> Request a fetch : Client -> Method -> String -> Decode.Decoder a -> Request a
fetch method client endpoint decoder = fetch client method endpoint decoder =
let let
request = request =
case method of case method of
GET -> GET ->
HttpBuilder.get Build.get
POST -> POST ->
HttpBuilder.post Build.post
DELETE -> DELETE ->
HttpBuilder.delete Build.delete
in in
request (client.server ++ endpoint) request (client.server ++ endpoint)
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token) |> Build.withHeader "Authorization" ("Bearer " ++ client.token)
|> HttpBuilder.withExpect (Http.expectJson decoder) |> Build.withExpect (Http.expectJson decoder)
register : String -> String -> String -> String -> String -> Request AppRegistration register : String -> String -> String -> String -> String -> Request AppRegistration
register server client_name redirect_uri scope website = register server clientName redirectUri scope website =
HttpBuilder.post (server ++ ApiUrl.apps) Build.post (server ++ ApiUrl.apps)
|> HttpBuilder.withExpect (Http.expectJson (appRegistrationDecoder server scope)) |> Build.withExpect (Http.expectJson (appRegistrationDecoder server scope))
|> HttpBuilder.withJsonBody (appRegistrationEncoder client_name redirect_uri scope website) |> Build.withJsonBody (appRegistrationEncoder clientName redirectUri scope website)
getAccessToken : AppRegistration -> String -> Request AccessTokenResult
getAccessToken registration authCode =
Build.post (registration.server ++ ApiUrl.oauthToken)
|> Build.withExpect (Http.expectJson (accessTokenDecoder registration))
|> Build.withJsonBody (authorizationCodeEncoder registration authCode)
getAuthorizationUrl : AppRegistration -> String getAuthorizationUrl : AppRegistration -> String
@ -116,72 +123,63 @@ getAuthorizationUrl registration =
] ]
getAccessToken : AppRegistration -> String -> Request AccessTokenResult
getAccessToken registration authCode =
HttpBuilder.post (registration.server ++ ApiUrl.oauthToken)
|> HttpBuilder.withExpect (Http.expectJson (accessTokenDecoder registration))
|> HttpBuilder.withJsonBody (authorizationCodeEncoder registration authCode)
send : (Result Error a -> msg) -> Request a -> Cmd msg send : (Result Error a -> msg) -> Request a -> Cmd msg
send tagger builder = send tagger builder =
builder |> HttpBuilder.send (toResponse >> tagger) Build.send (toResponse >> tagger) builder
fetchAccount : Client -> Int -> Request Account fetchAccount : Client -> Int -> Request Account
fetchAccount client accountId = fetchAccount client accountId =
fetch GET client (ApiUrl.account accountId) accountDecoder fetch client GET (ApiUrl.account accountId) accountDecoder
fetchUserTimeline : Client -> Request (List Status) fetchUserTimeline : Client -> Request (List Status)
fetchUserTimeline client = fetchUserTimeline client =
fetch GET client ApiUrl.homeTimeline <| Decode.list statusDecoder fetch client GET ApiUrl.homeTimeline <| Decode.list statusDecoder
fetchRelationships : Client -> List Int -> Request (List Relationship) fetchRelationships : Client -> List Int -> Request (List Relationship)
fetchRelationships client ids = fetchRelationships client ids =
-- TODO: use withQueryParams -- TODO: use withQueryParams
fetch GET client (ApiUrl.relationships ids) <| Decode.list relationshipDecoder fetch client GET (ApiUrl.relationships ids) <| Decode.list relationshipDecoder
fetchLocalTimeline : Client -> Request (List Status) fetchLocalTimeline : Client -> Request (List Status)
fetchLocalTimeline client = fetchLocalTimeline client =
-- TODO: use withQueryParams -- TODO: use withQueryParams
fetch GET client (ApiUrl.publicTimeline (Just "public")) <| Decode.list statusDecoder fetch client GET (ApiUrl.publicTimeline (Just "public")) <| Decode.list statusDecoder
fetchGlobalTimeline : Client -> Request (List Status) fetchGlobalTimeline : Client -> Request (List Status)
fetchGlobalTimeline client = fetchGlobalTimeline client =
-- TODO: use withQueryParams -- TODO: use withQueryParams
fetch GET client (ApiUrl.publicTimeline (Nothing)) <| Decode.list statusDecoder fetch client GET (ApiUrl.publicTimeline (Nothing)) <| Decode.list statusDecoder
fetchAccountTimeline : Client -> Int -> Request (List Status) fetchAccountTimeline : Client -> Int -> Request (List Status)
fetchAccountTimeline client id = fetchAccountTimeline client id =
fetch GET client (ApiUrl.accountTimeline id) <| Decode.list statusDecoder fetch client GET (ApiUrl.accountTimeline id) <| Decode.list statusDecoder
fetchNotifications : Client -> Request (List Notification) fetchNotifications : Client -> Request (List Notification)
fetchNotifications client = fetchNotifications client =
fetch GET client (ApiUrl.notifications) <| Decode.list notificationDecoder fetch client GET (ApiUrl.notifications) <| Decode.list notificationDecoder
fetchAccountFollowers : Client -> Int -> Request (List Account) fetchAccountFollowers : Client -> Int -> Request (List Account)
fetchAccountFollowers client accountId = fetchAccountFollowers client accountId =
fetch GET client (ApiUrl.followers accountId) <| Decode.list accountDecoder fetch client GET (ApiUrl.followers accountId) <| Decode.list accountDecoder
fetchAccountFollowing : Client -> Int -> Request (List Account) fetchAccountFollowing : Client -> Int -> Request (List Account)
fetchAccountFollowing client accountId = fetchAccountFollowing client accountId =
fetch GET client (ApiUrl.following accountId) <| Decode.list accountDecoder fetch client GET (ApiUrl.following accountId) <| Decode.list accountDecoder
searchAccounts : Client -> String -> Int -> Bool -> Request (List Account) searchAccounts : Client -> String -> Int -> Bool -> Request (List Account)
searchAccounts client query limit resolve = searchAccounts client query limit resolve =
HttpBuilder.get (client.server ++ ApiUrl.searchAccount) fetch client GET ApiUrl.searchAccount (Decode.list accountDecoder)
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token) |> Build.withQueryParams
|> HttpBuilder.withExpect (Http.expectJson (Decode.list accountDecoder))
|> HttpBuilder.withQueryParams
[ ( "q", query ) [ ( "q", query )
, ( "limit", toString limit ) , ( "limit", toString limit )
, ( "resolve" , ( "resolve"
@ -195,70 +193,50 @@ searchAccounts client query limit resolve =
userAccount : Client -> Request Account userAccount : Client -> Request Account
userAccount client = userAccount client =
HttpBuilder.get (client.server ++ ApiUrl.userAccount) fetch client GET ApiUrl.userAccount accountDecoder
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|> HttpBuilder.withExpect (Http.expectJson accountDecoder)
postStatus : Client -> StatusRequestBody -> Request Status postStatus : Client -> StatusRequestBody -> Request Status
postStatus client statusRequestBody = postStatus client statusRequestBody =
HttpBuilder.post (client.server ++ ApiUrl.statuses) fetch client POST ApiUrl.statuses statusDecoder
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token) |> Build.withJsonBody (statusRequestBodyEncoder statusRequestBody)
|> HttpBuilder.withExpect (Http.expectJson statusDecoder)
|> HttpBuilder.withJsonBody (statusRequestBodyEncoder statusRequestBody)
deleteStatus : Client -> Int -> Request Int deleteStatus : Client -> Int -> Request Int
deleteStatus client id = deleteStatus client id =
HttpBuilder.delete (client.server ++ (ApiUrl.status id)) fetch client DELETE (ApiUrl.status id) <| Decode.succeed id
|> HttpBuilder.withExpect (Http.expectJson <| Decode.succeed id)
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
context : Client -> Int -> Request Context context : Client -> Int -> Request Context
context client id = context client id =
HttpBuilder.get (client.server ++ (ApiUrl.context id)) fetch client GET (ApiUrl.context id) contextDecoder
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|> HttpBuilder.withExpect (Http.expectJson contextDecoder)
reblog : Client -> Int -> Request Status reblog : Client -> Int -> Request Status
reblog client id = reblog client id =
HttpBuilder.post (client.server ++ (ApiUrl.reblog id)) fetch client POST (ApiUrl.reblog id) statusDecoder
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|> 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 ++ (ApiUrl.unreblog id)) fetch client POST (ApiUrl.unreblog id) statusDecoder
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|> 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 ++ (ApiUrl.favourite id)) fetch client POST (ApiUrl.favourite id) statusDecoder
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|> 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 ++ (ApiUrl.unfavourite id)) fetch client POST (ApiUrl.unfavourite id) statusDecoder
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|> HttpBuilder.withExpect (Http.expectJson statusDecoder)
follow : Client -> Int -> Request Relationship follow : Client -> Int -> Request Relationship
follow client id = follow client id =
HttpBuilder.post (client.server ++ (ApiUrl.follow id)) fetch client POST (ApiUrl.follow id) relationshipDecoder
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|> HttpBuilder.withExpect (Http.expectJson relationshipDecoder)
unfollow : Client -> Int -> Request Relationship unfollow : Client -> Int -> Request Relationship
unfollow client id = unfollow client id =
HttpBuilder.post (client.server ++ (ApiUrl.unfollow id)) fetch client POST (ApiUrl.unfollow id) relationshipDecoder
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|> HttpBuilder.withExpect (Http.expectJson relationshipDecoder)