1
0
Fork 0
tooty/src/Mastodon/Http.elm

265 lines
8.5 KiB
Elm
Raw Normal View History

2017-04-27 14:34:27 +00:00
module Mastodon.Http
exposing
( Request
, context
2017-04-27 14:34:27 +00:00
, reblog
, unreblog
, favourite
, unfavourite
, follow
, unfollow
2017-04-27 14:34:27 +00:00
, register
, getAuthorizationUrl
, getAccessToken
, fetchAccount
2017-04-27 20:01:51 +00:00
, fetchAccountTimeline
, fetchAccountFollowers
, fetchAccountFollowing
2017-04-27 14:34:27 +00:00
, fetchLocalTimeline
, fetchNotifications
, fetchGlobalTimeline
, fetchUserTimeline
, fetchRelationships
2017-04-27 14:34:27 +00:00
, postStatus
2017-04-29 07:20:26 +00:00
, deleteStatus
, userAccount
2017-04-27 14:34:27 +00:00
, send
, searchAccounts
2017-04-27 14:34:27 +00:00
)
import Http
import HttpBuilder
import Json.Decode as Decode
import Mastodon.ApiUrl as ApiUrl
import Mastodon.Decoder exposing (..)
import Mastodon.Encoder exposing (..)
import Mastodon.Model exposing (..)
type alias Request a =
HttpBuilder.RequestBuilder a
extractMastodonError : Int -> String -> String -> Error
extractMastodonError statusCode statusMsg body =
case Decode.decodeString mastodonErrorDecoder body of
Ok errRecord ->
MastodonError statusCode statusMsg errRecord
Err err ->
ServerError statusCode statusMsg err
extractError : Http.Error -> Error
extractError error =
case error of
Http.BadStatus { status, body } ->
extractMastodonError status.code status.message body
Http.BadPayload str { status } ->
ServerError
status.code
status.message
("Failed decoding JSON: " ++ str)
Http.Timeout ->
TimeoutError
_ ->
NetworkError
toResponse : Result Http.Error a -> Result Error a
toResponse result =
Result.mapError extractError result
2017-05-02 20:05:46 +00:00
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)
2017-04-27 14:34:27 +00:00
register : String -> String -> String -> String -> String -> Request AppRegistration
register server client_name redirect_uri scope website =
2017-05-02 20:05:46 +00:00
HttpBuilder.post (server ++ ApiUrl.apps)
2017-04-27 14:34:27 +00:00
|> HttpBuilder.withExpect (Http.expectJson (appRegistrationDecoder server scope))
|> HttpBuilder.withJsonBody (appRegistrationEncoder client_name redirect_uri scope website)
getAuthorizationUrl : AppRegistration -> String
getAuthorizationUrl registration =
2017-05-02 20:05:46 +00:00
encodeUrl (registration.server ++ ApiUrl.oauthAuthorize)
2017-04-27 14:34:27 +00:00
[ ( "response_type", "code" )
, ( "client_id", registration.client_id )
, ( "scope", registration.scope )
, ( "redirect_uri", registration.redirect_uri )
]
getAccessToken : AppRegistration -> String -> Request AccessTokenResult
getAccessToken registration authCode =
2017-05-02 20:05:46 +00:00
HttpBuilder.post (registration.server ++ ApiUrl.oauthToken)
2017-04-27 14:34:27 +00:00
|> HttpBuilder.withExpect (Http.expectJson (accessTokenDecoder registration))
|> HttpBuilder.withJsonBody (authorizationCodeEncoder registration authCode)
send : (Result Error a -> msg) -> Request a -> Cmd msg
send tagger builder =
builder |> HttpBuilder.send (toResponse >> tagger)
fetchAccount : Client -> Int -> Request Account
fetchAccount client accountId =
2017-05-02 20:05:46 +00:00
fetch GET client (ApiUrl.account accountId) accountDecoder
2017-04-27 14:34:27 +00:00
fetchUserTimeline : Client -> Request (List Status)
fetchUserTimeline client =
2017-05-02 20:05:46 +00:00
fetch GET client ApiUrl.homeTimeline <| Decode.list statusDecoder
2017-04-27 14:34:27 +00:00
fetchRelationships : Client -> List Int -> Request (List Relationship)
fetchRelationships client ids =
2017-05-02 20:05:46 +00:00
-- TODO: use withQueryParams
fetch GET client (ApiUrl.relationships ids) <| Decode.list relationshipDecoder
2017-04-27 14:34:27 +00:00
fetchLocalTimeline : Client -> Request (List Status)
fetchLocalTimeline client =
2017-05-02 20:05:46 +00:00
-- TODO: use withQueryParams
fetch GET client (ApiUrl.publicTimeline (Just "public")) <| Decode.list statusDecoder
2017-04-27 14:34:27 +00:00
fetchGlobalTimeline : Client -> Request (List Status)
fetchGlobalTimeline client =
2017-05-02 20:05:46 +00:00
-- TODO: use withQueryParams
fetch GET client (ApiUrl.publicTimeline (Nothing)) <| Decode.list statusDecoder
2017-04-27 14:34:27 +00:00
2017-04-27 20:01:51 +00:00
fetchAccountTimeline : Client -> Int -> Request (List Status)
fetchAccountTimeline client id =
2017-05-02 20:05:46 +00:00
fetch GET client (ApiUrl.accountTimeline id) <| Decode.list statusDecoder
2017-04-27 20:01:51 +00:00
2017-04-27 14:34:27 +00:00
fetchNotifications : Client -> Request (List Notification)
fetchNotifications client =
2017-05-02 20:05:46 +00:00
fetch GET client (ApiUrl.notifications) <| Decode.list notificationDecoder
2017-04-27 14:34:27 +00:00
fetchAccountFollowers : Client -> Int -> Request (List Account)
fetchAccountFollowers client accountId =
2017-05-02 20:05:46 +00:00
fetch GET client (ApiUrl.followers accountId) <| Decode.list accountDecoder
fetchAccountFollowing : Client -> Int -> Request (List Account)
fetchAccountFollowing client accountId =
2017-05-02 20:05:46 +00:00
fetch GET client (ApiUrl.following accountId) <| Decode.list accountDecoder
searchAccounts : Client -> String -> Int -> Bool -> Request (List Account)
searchAccounts client query limit resolve =
2017-05-02 20:05:46 +00:00
HttpBuilder.get (client.server ++ ApiUrl.searchAccount)
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|> HttpBuilder.withExpect (Http.expectJson (Decode.list accountDecoder))
2017-05-02 20:05:46 +00:00
|> HttpBuilder.withQueryParams
[ ( "q", query )
, ( "limit", toString limit )
, ( "resolve"
, if resolve then
"true"
else
"false"
)
]
userAccount : Client -> Request Account
userAccount client =
2017-05-02 20:05:46 +00:00
HttpBuilder.get (client.server ++ ApiUrl.userAccount)
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|> HttpBuilder.withExpect (Http.expectJson accountDecoder)
2017-04-27 14:34:27 +00:00
postStatus : Client -> StatusRequestBody -> Request Status
postStatus client statusRequestBody =
2017-05-02 20:05:46 +00:00
HttpBuilder.post (client.server ++ ApiUrl.statuses)
2017-04-27 14:34:27 +00:00
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|> HttpBuilder.withExpect (Http.expectJson statusDecoder)
|> HttpBuilder.withJsonBody (statusRequestBodyEncoder statusRequestBody)
2017-04-29 07:20:26 +00:00
deleteStatus : Client -> Int -> Request Int
deleteStatus client id =
2017-05-02 20:05:46 +00:00
HttpBuilder.delete (client.server ++ (ApiUrl.status id))
2017-04-29 07:20:26 +00:00
|> HttpBuilder.withExpect (Http.expectJson <| Decode.succeed id)
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
context : Client -> Int -> Request Context
context client id =
2017-05-02 20:05:46 +00:00
HttpBuilder.get (client.server ++ (ApiUrl.context id))
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|> HttpBuilder.withExpect (Http.expectJson contextDecoder)
2017-04-27 14:34:27 +00:00
reblog : Client -> Int -> Request Status
reblog client id =
2017-05-02 20:05:46 +00:00
HttpBuilder.post (client.server ++ (ApiUrl.reblog id))
2017-04-27 14:34:27 +00:00
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|> HttpBuilder.withExpect (Http.expectJson statusDecoder)
unreblog : Client -> Int -> Request Status
unreblog client id =
2017-05-02 20:05:46 +00:00
HttpBuilder.post (client.server ++ (ApiUrl.unreblog id))
2017-04-27 14:34:27 +00:00
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|> HttpBuilder.withExpect (Http.expectJson statusDecoder)
favourite : Client -> Int -> Request Status
favourite client id =
2017-05-02 20:05:46 +00:00
HttpBuilder.post (client.server ++ (ApiUrl.favourite id))
2017-04-27 14:34:27 +00:00
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|> HttpBuilder.withExpect (Http.expectJson statusDecoder)
unfavourite : Client -> Int -> Request Status
unfavourite client id =
2017-05-02 20:05:46 +00:00
HttpBuilder.post (client.server ++ (ApiUrl.unfavourite id))
2017-04-27 14:34:27 +00:00
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|> HttpBuilder.withExpect (Http.expectJson statusDecoder)
follow : Client -> Int -> Request Relationship
follow client id =
2017-05-02 20:05:46 +00:00
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 =
2017-05-02 20:05:46 +00:00
HttpBuilder.post (client.server ++ (ApiUrl.unfollow id))
|> HttpBuilder.withHeader "Authorization" ("Bearer " ++ client.token)
|> HttpBuilder.withExpect (Http.expectJson relationshipDecoder)