|
| 1 | +-- | This module defines low-level bindings to the Node HTTP client. |
| 2 | + |
| 3 | +module Node.HTTP.Client |
| 4 | + ( Request() |
| 5 | + , Response() |
| 6 | + , RequestHeaders() |
| 7 | + , RequestOptions() |
| 8 | + , protocol |
| 9 | + , hostname |
| 10 | + , port |
| 11 | + , method |
| 12 | + , path |
| 13 | + , headers |
| 14 | + , auth |
| 15 | + , request |
| 16 | + , requestAsStream |
| 17 | + , responseAsStream |
| 18 | + , httpVersion |
| 19 | + , responseHeaders |
| 20 | + ) where |
| 21 | + |
| 22 | +import Prelude |
| 23 | + |
| 24 | +import Data.Foreign |
| 25 | +import Data.Options |
| 26 | +import Data.StrMap (StrMap()) |
| 27 | + |
| 28 | +import Node.HTTP (HTTP()) |
| 29 | +import Node.Stream |
| 30 | + |
| 31 | +import Control.Monad.Eff |
| 32 | + |
| 33 | +import Unsafe.Coerce (unsafeCoerce) |
| 34 | + |
| 35 | +-- | A HTTP request object |
| 36 | +foreign import data Request :: * |
| 37 | + |
| 38 | +-- | A HTTP response object |
| 39 | +foreign import data Response :: * |
| 40 | + |
| 41 | +-- | A HTTP request object |
| 42 | +newtype RequestHeaders = RequestHeaders (StrMap String) |
| 43 | + |
| 44 | +instance requestHeadersIsOption :: IsOption RequestHeaders where |
| 45 | + assoc k v = assoc (optionFn k) (unsafeCoerce v :: {}) |
| 46 | + |
| 47 | +-- | The type of HTTP request options |
| 48 | +data RequestOptions |
| 49 | + |
| 50 | +-- | The protocol to use |
| 51 | +protocol :: Option RequestOptions String |
| 52 | +protocol = opt "protocol" |
| 53 | + |
| 54 | +-- | Domain name or IP |
| 55 | +hostname :: Option RequestOptions String |
| 56 | +hostname = opt "hostname" |
| 57 | + |
| 58 | +-- | Port of remote server |
| 59 | +port :: Option RequestOptions Int |
| 60 | +port = opt "port" |
| 61 | + |
| 62 | +-- | The HTTP request method: GET, POST, etc. |
| 63 | +method :: Option RequestOptions String |
| 64 | +method = opt "method" |
| 65 | + |
| 66 | +-- | The request path, including query string if appropriate. |
| 67 | +path :: Option RequestOptions String |
| 68 | +path = opt "path" |
| 69 | + |
| 70 | +headers :: Option RequestOptions String |
| 71 | +headers = opt "headers" |
| 72 | + |
| 73 | +-- | Basic authentication |
| 74 | +auth :: Option RequestOptions String |
| 75 | +auth = opt "auth" |
| 76 | + |
| 77 | +-- | Make a HTTP request using the specified options and response callback. |
| 78 | +foreign import requestImpl :: forall eff. Foreign -> (Response -> Eff (http :: HTTP | eff) Unit) -> Eff (http :: HTTP | eff) Request |
| 79 | + |
| 80 | +-- | Make a HTTP request using the specified options and response callback. |
| 81 | +request :: forall eff. Options RequestOptions -> (Response -> Eff (http :: HTTP | eff) Unit) -> Eff (http :: HTTP | eff) Request |
| 82 | +request = requestImpl <<< options |
| 83 | + |
| 84 | +-- | Create a writable stream from a request object. |
| 85 | +requestAsStream :: forall eff r a. Request -> Writable r (http :: HTTP | eff) a |
| 86 | +requestAsStream = unsafeCoerce |
| 87 | + |
| 88 | +-- | Create a readable stream from a response object. |
| 89 | +responseAsStream :: forall eff w a. Response -> Readable w (http :: HTTP | eff) a |
| 90 | +responseAsStream = unsafeCoerce |
| 91 | + |
| 92 | +-- | Get the request HTTP version |
| 93 | +httpVersion :: Response -> String |
| 94 | +httpVersion = _.httpVersion <<< unsafeCoerce |
| 95 | + |
| 96 | +-- | Get the response headers as a hash |
| 97 | +responseHeaders :: Response -> StrMap String |
| 98 | +responseHeaders = _.headers <<< unsafeCoerce |
0 commit comments