module Api.Endpoint exposing (Endpoint, login, request, tags, todo, todoList, user, users) import Http import Todo.UUID as UUID exposing (UUID) import Url.Builder exposing (QueryParameter) import Username exposing (Username) {-| Http.request, except it takes an Endpoint instead of a Url. -} request : { body : Http.Body , expect : Http.Expect a , headers : List Http.Header , method : String , timeout : Maybe Float , url : Endpoint , tracker : Maybe String } -> Cmd a request config = Http.request { body = config.body , expect = config.expect , headers = config.headers , method = config.method , timeout = config.timeout , url = unwrap config.url , tracker = config.tracker } -- TYPES {-| Get a URL to the Conduit API. This is not publicly exposed, because we want to make sure the only way to get one of these URLs is from this module. -} type Endpoint = Endpoint String unwrap : Endpoint -> String unwrap (Endpoint str) = str url : List String -> List QueryParameter -> Endpoint url paths queryParams = -- NOTE: Url.Builder takes care of percent-encoding special URL characters. -- See https://package.elm-lang.org/packages/elm/url/latest/Url#percentEncode Url.Builder.crossOrigin "https://conduit.productionready.io" ("api" :: paths) queryParams |> Endpoint -- ENDPOINTS login : Endpoint login = url [ "users", "login" ] [] user : Endpoint user = url [ "user" ] [] users : Endpoint users = url [ "users" ] [] follow : Username -> Endpoint follow uname = url [ "profiles", Username.toString uname, "follow" ] [] -- ARTICLE ENDPOINTS todo : UUID -> Endpoint todo uuid = url [ "articles", UUID.toString uuid ] [] todoList : List QueryParameter -> Endpoint todoList params = url [ "articles" ] params tags : Endpoint tags = url [ "tags" ] []