-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
276 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
## Module Node.HTTP.Client | ||
|
||
This module defines low-level bindings to the Node HTTP client. | ||
|
||
#### `Request` | ||
|
||
``` purescript | ||
data Request :: * | ||
``` | ||
|
||
A HTTP request object | ||
|
||
#### `Response` | ||
|
||
``` purescript | ||
data Response :: * | ||
``` | ||
|
||
A HTTP response object | ||
|
||
#### `RequestHeaders` | ||
|
||
``` purescript | ||
newtype RequestHeaders | ||
``` | ||
|
||
A HTTP request object | ||
|
||
##### Instances | ||
``` purescript | ||
instance requestHeadersIsOption :: IsOption RequestHeaders | ||
``` | ||
|
||
#### `RequestOptions` | ||
|
||
``` purescript | ||
data RequestOptions | ||
``` | ||
|
||
The type of HTTP request options | ||
|
||
#### `protocol` | ||
|
||
``` purescript | ||
protocol :: Option RequestOptions String | ||
``` | ||
|
||
The protocol to use | ||
|
||
#### `hostname` | ||
|
||
``` purescript | ||
hostname :: Option RequestOptions String | ||
``` | ||
|
||
Domain name or IP | ||
|
||
#### `port` | ||
|
||
``` purescript | ||
port :: Option RequestOptions Int | ||
``` | ||
|
||
Port of remote server | ||
|
||
#### `method` | ||
|
||
``` purescript | ||
method :: Option RequestOptions String | ||
``` | ||
|
||
The HTTP request method: GET, POST, etc. | ||
|
||
#### `path` | ||
|
||
``` purescript | ||
path :: Option RequestOptions String | ||
``` | ||
|
||
The request path, including query string if appropriate. | ||
|
||
#### `headers` | ||
|
||
``` purescript | ||
headers :: Option RequestOptions String | ||
``` | ||
|
||
#### `auth` | ||
|
||
``` purescript | ||
auth :: Option RequestOptions String | ||
``` | ||
|
||
Basic authentication | ||
|
||
#### `request` | ||
|
||
``` purescript | ||
request :: forall eff. Options RequestOptions -> (Response -> Eff (http :: HTTP | eff) Unit) -> Eff (http :: HTTP | eff) Request | ||
``` | ||
|
||
Make a HTTP request using the specified options and response callback. | ||
|
||
#### `requestAsStream` | ||
|
||
``` purescript | ||
requestAsStream :: forall eff r a. Request -> Writable r (http :: HTTP | eff) a | ||
``` | ||
|
||
Create a writable stream from a request object. | ||
|
||
#### `responseAsStream` | ||
|
||
``` purescript | ||
responseAsStream :: forall eff w a. Response -> Readable w (http :: HTTP | eff) a | ||
``` | ||
|
||
Create a readable stream from a response object. | ||
|
||
#### `httpVersion` | ||
|
||
``` purescript | ||
httpVersion :: Response -> String | ||
``` | ||
|
||
Get the request HTTP version | ||
|
||
#### `responseHeaders` | ||
|
||
``` purescript | ||
responseHeaders :: Response -> StrMap String | ||
``` | ||
|
||
Get the response headers as a hash | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"use strict"; | ||
|
||
// module Node.HTTP.Client | ||
|
||
var http = require('http'); | ||
|
||
exports.requestImpl = function(opts) { | ||
return function(k) { | ||
return function() { | ||
return http.request(opts, function(res) { | ||
k(res)(); | ||
}); | ||
}; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
-- | This module defines low-level bindings to the Node HTTP client. | ||
|
||
module Node.HTTP.Client | ||
( Request() | ||
, Response() | ||
, RequestHeaders() | ||
, RequestOptions() | ||
, protocol | ||
, hostname | ||
, port | ||
, method | ||
, path | ||
, headers | ||
, auth | ||
, request | ||
, requestAsStream | ||
, responseAsStream | ||
, httpVersion | ||
, responseHeaders | ||
) where | ||
|
||
import Prelude | ||
|
||
import Data.Foreign | ||
import Data.Options | ||
import Data.StrMap (StrMap()) | ||
|
||
import Node.HTTP (HTTP()) | ||
import Node.Stream | ||
|
||
import Control.Monad.Eff | ||
|
||
import Unsafe.Coerce (unsafeCoerce) | ||
|
||
-- | A HTTP request object | ||
foreign import data Request :: * | ||
|
||
-- | A HTTP response object | ||
foreign import data Response :: * | ||
|
||
-- | A HTTP request object | ||
newtype RequestHeaders = RequestHeaders (StrMap String) | ||
|
||
instance requestHeadersIsOption :: IsOption RequestHeaders where | ||
assoc k v = assoc (optionFn k) (unsafeCoerce v :: {}) | ||
|
||
-- | The type of HTTP request options | ||
data RequestOptions | ||
|
||
-- | The protocol to use | ||
protocol :: Option RequestOptions String | ||
protocol = opt "protocol" | ||
|
||
-- | Domain name or IP | ||
hostname :: Option RequestOptions String | ||
hostname = opt "hostname" | ||
|
||
-- | Port of remote server | ||
port :: Option RequestOptions Int | ||
port = opt "port" | ||
|
||
-- | The HTTP request method: GET, POST, etc. | ||
method :: Option RequestOptions String | ||
method = opt "method" | ||
|
||
-- | The request path, including query string if appropriate. | ||
path :: Option RequestOptions String | ||
path = opt "path" | ||
|
||
headers :: Option RequestOptions String | ||
headers = opt "headers" | ||
|
||
-- | Basic authentication | ||
auth :: Option RequestOptions String | ||
auth = opt "auth" | ||
|
||
-- | Make a HTTP request using the specified options and response callback. | ||
foreign import requestImpl :: forall eff. Foreign -> (Response -> Eff (http :: HTTP | eff) Unit) -> Eff (http :: HTTP | eff) Request | ||
|
||
-- | Make a HTTP request using the specified options and response callback. | ||
request :: forall eff. Options RequestOptions -> (Response -> Eff (http :: HTTP | eff) Unit) -> Eff (http :: HTTP | eff) Request | ||
request = requestImpl <<< options | ||
|
||
-- | Create a writable stream from a request object. | ||
requestAsStream :: forall eff r a. Request -> Writable r (http :: HTTP | eff) a | ||
requestAsStream = unsafeCoerce | ||
|
||
-- | Create a readable stream from a response object. | ||
responseAsStream :: forall eff w a. Response -> Readable w (http :: HTTP | eff) a | ||
responseAsStream = unsafeCoerce | ||
|
||
-- | Get the request HTTP version | ||
httpVersion :: Response -> String | ||
httpVersion = _.httpVersion <<< unsafeCoerce | ||
|
||
-- | Get the response headers as a hash | ||
responseHeaders :: Response -> StrMap String | ||
responseHeaders = _.headers <<< unsafeCoerce |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"use strict"; | ||
|
||
// module Test.Main | ||
|
||
exports.stdout = process.stdout; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters