Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: applies elm-format on all files #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,096 changes: 121 additions & 975 deletions package-lock.json

Large diffs are not rendered by default.

151 changes: 84 additions & 67 deletions src/elm/Api.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ module Api exposing
( ApiClient
, ApiRequestOutcome
, getApiClient
, reportError
, reportError
)

import Api.Input as Input
import Api.Output as Output
import Data exposing (ApiToken(..), Interactions, User(..), UserInfo, UserInfoWithToken, VoteAction(..))
import Data.Comment exposing (Comment, CommentTree)
import Data.Cuid exposing (Cuid)
import Http exposing (Body, Header)
import Api.Input as Input
import Json.Decode as D exposing (Decoder)
import Json.Encode as E
import Task exposing (Task)
Expand All @@ -28,7 +28,7 @@ type alias Api =
type alias ApiClient =
{ getPostComments : GetPostComments
, getRepliesForComment : GetRepliesForComment
, addComment : AddComment
, addComment : AddComment
, reportError : ReportError
, userLogIn : LogIn
, userSignUp : SignUp
Expand All @@ -38,13 +38,15 @@ type alias ApiClient =
}


type alias ApiRequestOutcome a = Result Http.Error a
type alias ApiRequestOutcome a =
Result Http.Error a


getApiClient : Url -> Url -> ApiClient
getApiClient : Url -> Url -> ApiClient
getApiClient baseUrl siteUrl =
let
api = Api baseUrl siteUrl
api =
Api baseUrl siteUrl
in
{ getPostComments = getPostComments api
, getRepliesForComment = getRepliesForComment api
Expand All @@ -58,44 +60,45 @@ getApiClient baseUrl siteUrl =
}



noParams : List QueryParameter
noParams = []
noParams =
[]


noHeaders : List Header
noHeaders = []
noHeaders =
[]


authHeader : ApiToken -> Header
authHeader (ApiToken token) =
Http.header "Authorization" token



requestResolver : Decoder a -> Http.Resolver Http.Error a
requestResolver decoder =
Http.stringResolver (\response ->
case response of
Http.BadUrl_ url ->
Err (Http.BadUrl url)
Http.stringResolver
(\response ->
case response of
Http.BadUrl_ url ->
Err (Http.BadUrl url)

Http.Timeout_ ->
Err Http.Timeout
Http.Timeout_ ->
Err Http.Timeout

Http.NetworkError_ ->
Err Http.NetworkError
Http.NetworkError_ ->
Err Http.NetworkError

Http.BadStatus_ metadata _ ->
Err (Http.BadStatus metadata.statusCode)
Http.BadStatus_ metadata _ ->
Err (Http.BadStatus metadata.statusCode)

Http.GoodStatus_ _ body ->
case D.decodeString decoder body of
Ok value ->
Ok value
Http.GoodStatus_ _ body ->
case D.decodeString decoder body of
Ok value ->
Ok value

Err err ->
Err (Http.BadBody (D.errorToString err))
Err err ->
Err (Http.BadBody (D.errorToString err))
)


Expand All @@ -111,7 +114,6 @@ getTask url headers resolver =
}



postTask : String -> List Header -> Body -> Http.Resolver Http.Error a -> Task Http.Error a
postTask url headers body resolver =
Http.task
Expand All @@ -138,13 +140,12 @@ makeRequestUrl { baseUrl } routePath queryParams =
else
raw


pathComponents =
if routePath == "/error-reporting" then
[ String.dropLeft 1 routePath ]

else
String.split "/" routePath

in
Url.Builder.crossOrigin
stringifiedUrl
Expand All @@ -166,27 +167,28 @@ getSiteInfo { siteUrl } =







-- Actual API Requests


type alias ErrorInfo =
{ ref : String
, message : String
}

type alias ReportError = ErrorInfo -> Task Http.Error ()

type alias ReportError =
ErrorInfo -> Task Http.Error ()


reportError : Api -> ReportError
reportError api { ref, message } =
let
endpointPath = "/error-reporting"
endpointPath =
"/error-reporting"

body =
E.object
[ ( "ref", E.string ref)
[ ( "ref", E.string ref )
, ( "errorMessage", E.string message )
]
in
Expand All @@ -197,17 +199,18 @@ reportError api { ref, message } =
(requestResolver <| D.succeed ())




type alias GetPostComments =
Task Http.Error CommentTree

getPostComments : Api -> GetPostComments

getPostComments : Api -> GetPostComments
getPostComments api =
let
{ hostname, path } = getSiteInfo api

endpointPath = "embed/sites/" ++ hostname ++ "/comments"
{ hostname, path } =
getSiteInfo api

endpointPath =
"embed/sites/" ++ hostname ++ "/comments"

decoder =
Input.apiResponseDecoder Input.commentTreeDecoder
Expand All @@ -221,21 +224,22 @@ getPostComments api =
(requestResolver decoder)



type alias GetRepliesForComment =
Cuid -> Task Http.Error CommentTree


getRepliesForComment : Api -> GetRepliesForComment
getRepliesForComment api commentId =
let
{ hostname, path } = getSiteInfo api

endpointPath = "embed/sites/" ++ hostname ++ "/comments"
{ hostname, path } =
getSiteInfo api

endpointPath =
"embed/sites/" ++ hostname ++ "/comments"

decoder =
Input.apiResponseDecoder Input.commentTreeDecoder

queryParams =
[ Url.Builder.string "parentCommentId" commentId
, Url.Builder.string "postId" path
Expand All @@ -254,8 +258,9 @@ type alias AddComment =
addComment : Api -> AddComment
addComment api commentContents postId parentCommentId user =
let
endpointPath = "embed/posts/" ++ postId ++ "/comments"

endpointPath =
"embed/posts/" ++ postId ++ "/comments"

decoder =
Input.apiResponseDecoder Input.commentDecoder

Expand All @@ -282,15 +287,15 @@ addComment api commentContents postId parentCommentId user =
(requestResolver decoder)



type alias LogIn =
Output.LogIn -> Task Http.Error UserInfoWithToken


userLogIn : Api -> LogIn
userLogIn : Api -> LogIn
userLogIn api data =
let
endpointPath = "common/signin"
endpointPath =
"common/signin"

signinJson =
E.object
Expand All @@ -312,7 +317,8 @@ type alias SignUp =
userSignUp : Api -> SignUp
userSignUp api data =
let
endpointPath = "common/signup"
endpointPath =
"common/signup"

signUpJson =
E.object
Expand All @@ -327,14 +333,16 @@ userSignUp api data =
(Http.jsonBody signUpJson)
(requestResolver Input.userAndTokenDecoder)


type alias GetUserFromSessionToken =
ApiToken -> Task Http.Error UserInfo


getUserFromSessionToken : Api -> GetUserFromSessionToken
getUserFromSessionToken api apiToken =
let
endpointPath = "common/profile"
endpointPath =
"common/profile"

headers =
[ authHeader apiToken
Expand All @@ -349,16 +357,18 @@ getUserFromSessionToken api apiToken =
(requestResolver decoder)



type alias GetUserInteractions =
ApiToken -> Task Http.Error Interactions
ApiToken -> Task Http.Error Interactions


getUserInteractions : Api -> GetUserInteractions
getUserInteractions api apiToken =
let
endpointPath = "embed/interactions"
endpointPath =
"embed/interactions"

{ path } = getSiteInfo api
{ path } =
getSiteInfo api

headers =
[ authHeader apiToken ]
Expand All @@ -378,18 +388,26 @@ getUserInteractions api apiToken =
type alias SubmitVoteForComment =
ApiToken -> Cuid -> VoteAction -> Task Http.Error ()


submitVoteForComment : Api -> SubmitVoteForComment
submitVoteForComment api apiToken commentId voteType =
let
endpointPath = "embed/comments/" ++ commentId ++ "/vote"
endpointPath =
"embed/comments/" ++ commentId ++ "/vote"

headers = [ authHeader apiToken ]
headers =
[ authHeader apiToken ]

voteInt =
voteInt =
case voteType of
SetUp -> 1
SetDown -> -1
SetNeutral -> 0
SetUp ->
1

SetDown ->
-1

SetNeutral ->
0

body =
E.object
Expand All @@ -402,6 +420,5 @@ submitVoteForComment api apiToken commentId voteType =
postTask
(makeRequestUrl api endpointPath noParams)
headers
(Http.jsonBody body)
(Http.jsonBody body)
(requestResolver decoder)

Loading