web: expose raw request function in API
This will be useful for performing requests that don't conform to the
regular patterns. For instance, `/sky/logout` is a GET request that does
not return JSON.
-module Api exposing (get, paginatedGet)
+module Api exposing (Method(..), get, paginatedGet, request)
import Api.Endpoints exposing (Endpoint, toUrl)
import Concourse.Pagination exposing (Paginated)
@@ -8,29 +8,55 @@ import Network.Pagination exposing (parsePagination)
import Task exposing (Task)
-get : Endpoint -> Decoder a -> Task Http.Error a
-get endpoint decoder =
+type Method
+ = Get
+
+
+methodToString : Method -> String
+methodToString m =
+ case m of
+ Get ->
+ "GET"
+
+
+request :
+ { endpoint : Endpoint
+ , method : Method
+ , headers : List Http.Header
+ , body : Http.Body
+ , expect : Http.Expect a
+ }
+ -> Task Http.Error a
+request { endpoint, method, headers, body, expect } =
Http.request
- { method = "GET"
- , headers = []
+ { method = methodToString method
+ , headers = headers
, url = toUrl endpoint
- , body = Http.emptyBody
- , expect = Http.expectJson decoder
+ , body = body
+ , expect = expect
, timeout = Nothing
, withCredentials = False
}
|> Http.toTask
+get : Endpoint -> Decoder a -> Task Http.Error a
+get endpoint decoder =
+ request
+ { method = Get
+ , headers = []
+ , endpoint = endpoint
+ , body = Http.emptyBody
+ , expect = Http.expectJson decoder
+ }
+
+
paginatedGet : Endpoint -> Decoder a -> Task Http.Error (Paginated a)
paginatedGet endpoint decoder =
- Http.request
- { method = "GET"
+ request
+ { method = Get
, headers = []
- , url = toUrl endpoint
+ , endpoint = endpoint
, body = Http.emptyBody
, expect = Http.expectStringResponse (parsePagination decoder)
- , timeout = Nothing
- , withCredentials = False
}
- |> Http.toTask
