# <%=pkg.name%> SDK for Node.js

This is a node module which provides a JavaScript API for accessing <%=pkg.name%>.

## Installation

You can install via npm or directly include the library in your node application.

Once installed, you can use the module by requiring it.

_NOTE: You must set the `baseurl` to the correct URL location of the server endpoint._

```javascript
var API = require('<%=sdkname%>');
API.baseurl = 'http://localhost:8080';
```

<% if (securityMessage) { -%>
## Security

<%- securityMessage('API',objectModel,sdktype) %>
<% } -%>

## APIs
<% 
	Object.keys(objectModel.apis).forEach(function (group) {
		var endpoints = objectModel.apis[group].endpoints;
		endpoints.forEach(function (api) {
			var json, body, params;
			var model = api.model && objectModel.models[api.model];
			api.parameters && Object.keys(api.parameters).forEach(function (key) {
				var entry = api.parameters[key];
				if (model && model.fields && model.fields[key]) {
					var field = model.fields[key];
					if (field.readonly) {
						return;
					}
				}
				if (entry.type === 'path') {
					params = params || {};
					params[key] = '';
					return; 
				}
				if (entry.type === 'body') {
					body = body || {};
					body[key] = '';
				} else {
					json = json || {};
					json[key] = '';
				}
			});
			var path = api.path;
			var index = api.path.indexOf(':');
			if (index) {
				path = path.substring(0,index-1);
				if (/\/$/.test(path)) {
					path = path.substring(0,path.length-1);
				}
			}
-%>

### <%= api.nickname ? api.nickname : path || api.path %>

<%- api.description %>

`<%=api.method%> <%=api.path%>`

<% if (api.parameters && Object.keys(api.parameters).length) {
	var found = {};
-%>
#### API Parameters

Name | Description | Type | Optional
:----| :---------- | :--- | :-------
<%
	Object.keys(api.parameters).forEach(function (key) {
		var entry = api.parameters[key];
		if (model && model.fields && model.fields[key]) {
			var field = model.fields[key];
			if (field.readonly) {
				return;
			}
		}
		found[key]=1;
-%>
<%= key %> | <%- entry.description %> | <%= entry.type %> | <%= entry.optional ? 'yes':'no' %>
<% }); -%>

<% } -%>

<% if (model) { -%>
#### API Result `<%=model.name%>`

Name | Description | Type
:----| :---------- | :---
<% Object.keys(model.fields).forEach(function (key) {
	var entry = model.fields[key];
-%>
<%= key %> | <%- entry.description %> | <%= entry.type %>
<% }); -%>

<% } -%>

#### Usage Example

```javascript
var API = require('<%=sdkname%>');
API.baseurl = 'http://localhost:8080';
<% if (api.nickname) { -%>
var api = new API('<%= api.nickname%>');
<% } else { -%>
var api = new API('<%= api.path %>','<%= api.method%>');
<% } -%>
<% if (params) { -%>
api.params(<%- JSON.stringify(params,null,'\t') %>);
<% } -%>
<% if (json) { -%>
api.json(<%- JSON.stringify(json,null,'\t') %>);
<% } -%>
<% if (body) { -%>
api.body(<%- JSON.stringify(body,null,'\t') %>);
<% } -%>
api.send(function (err,resp,json) {
	console.log(json);
});
```

<%	}) -%>
<%	}) -%>

## Other SDK APIs

The following additional APIs are available:

### API.baseurl

Set the base url for the Arrow server.

#### Usage Example

```javascript
API.baseurl = 'http://localhost:8080';
```

### api.json

Set the JSON body.

#### Usage Example

```javascript
api.json({
	foo: true
});
```

### api.body

Set the body. The body can include one or more files (provide a valid file path as the value) to automatically send `multipart/form-data`. To specify a file for uploading, pass an instance of a [stream.Readable](http://nodejs.org/api/stream.html#stream_class_stream_readable).

#### Usage Example

```javascript
api.body({
	file: fs.createReadStream('/path/to/myfile.json'),
	name: 'this is a name'
});
```

### api.query

Set query parameters for the URL. These will automatically be appended correctly to the request URL.

#### Usage Example

```javascript
api.query({
	pretty_json: true
});
```

### api.params

Set URL parameters in the request URL.

#### Usage Example

```javascript
api.params({
	username: 'test'
});
```

### api.header

Add an HTTP request headers.

#### Usage Example

```javascript
api.header('X-Foo','Bar');
```

You can add multiple headers by chaining the methods:

```javascript
api.header('X-Foo','Bar')
	.header('X-Bar','Foo');
```

### API.debug

Turn on/off debug logging.

#### Usage Example

```javascript
API.debug = false;
```

You can set either by passing `debug` in API constructor as part of the config object (first parameter) or by setting the environment variable `DEBUG` to `arrow:sdk`.

#### Usage Example

```javascript
var api = new API({debug:true},'/api/login');
```

```bash
DEBUG=arrow:sdk node app
```


### Events

The following events can also be listened to:

- `error`: fired when an error occurs during an API request
- `response`: fired when an API response is received
- `timeout`: fired when an API request times out

#### Usage Example

```javascript
var api = new API('myapi')
	.on('error',function (err) {
		console.error("An error occurred.",err);
	})
	.on('timeout',function (err) {
		console.error("A timeout occurred.",err);
	})
	.on('response',function (resp,json) {
		console.log(json);
	})
	.send({
		username: 'foo',
		password: 'bar'
	});
```

## Dependencies

To use this library, you must add an appropriate npm dependency for the request library. It can be any of the following compatible modules:

- [appc-request-ssl](https://github.com/appcelerator/appc-request-ssl)
- [request-ssl](https://github.com/jhaynie/request-ssl)
- [request](https://github.com/request/request)

Add the appropriate request module to your package.json dependencies.

## Notes

This SDK was generated using the [Appcelerator](http://www.appcelerator.com) Arrow SDK generator.
Generated on <%= new Date()%> by <%=username%>.
