Skip to content

Commit

Permalink
Add Ip and Device traffic methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dkenzox committed Jun 3, 2016
1 parent e19a1bd commit 6e8753f
Show file tree
Hide file tree
Showing 22 changed files with 625 additions and 406 deletions.
28 changes: 24 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ api.setApiToken('YOUR NEW TOKEN');
* [Plans](#plans)
* [Memberships](#memberships)
* [Invitations](#invitations)
* [Ips](#Ips)
* [Transfers](#transfer)
* [Users](#user)
* [Events](#events)
Expand Down Expand Up @@ -60,6 +61,12 @@ To make the documentation simpler and easier to read I'll put here comments on g
### Invite To Project
`api.inviteToProject(id, user, callback);`

### Request more IPs for a single project
`api.transferProject(id, request, callback);`

### Get Project's IP Reservations
`api.getProjectIpReservations(id, parameters, callback);`

### Devices
#### Parameters
`projectId` Project's ID.
Expand All @@ -84,6 +91,12 @@ To make the documentation simpler and easier to read I'll put here comments on g
### Device Actions
`api.deviceAction(id, action, callback);`

### Assign IP to Device
`api.assignIp(id, ip, callback);`

### Device Traffic
`api.getDeviceTraffic(id, parameters, callback);`

### Facilities
#### Parameters
`callback` Callback Function used to return the api's response.
Expand Down Expand Up @@ -125,6 +138,15 @@ To make the documentation simpler and easier to read I'll put here comments on g
#### Remove invitation
`api.removeInvitation(id, callback);`

### Ips
#### Parameters
`id` Ip's ID.
`callback` Callback Function used to return the api's response.
#### Get a single Ip
`api.getIp(id, callback);`
#### Remove invitation
`api.removeIp(id, callback);`

### Transfer
#### Parameters
`id` Membership's ID
Expand All @@ -138,12 +160,10 @@ To make the documentation simpler and easier to read I'll put here comments on g

### User
#### Parameters
`id` User's ID
`user` User object
`callback` Callback Function used to return the api's response.
#### Get Users
**Note:** Leaving *id* as false will return all the users available to the logged user,
`àpi.getUsers(id, callback);`
#### Get User
`api.getUser(callback);`
#### Update current user
`api.updateCurrentUser(user, callback);`

Expand Down
89 changes: 89 additions & 0 deletions lib/http-methods/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
(function() {
'use strict';
module.exports = function(Packet) {
var request = require('request');
Packet.prototype._get = function(url, parameters, callback) {
var self = this;
request(
{
method: 'GET',
url: self.buildUrl(url),
qs: parameters,
headers: self.getAuthHeaders(),
json:true
}, function(err, res, body) {
callback(handleRequestError(err, body), body || {});
}
);
};

Packet.prototype._post = function(url, postData, callback) {
var self = this;
request(
{
method: 'POST',
url: self.buildUrl(url),
body: postData,
headers: self.getAuthHeaders(),
json:true
}, function(err, res, body) {
callback(handleRequestError(err, body), body || {});
}
);
};

Packet.prototype._patch = function(url, postData, callback) {
var self = this;
request(
{
method: 'PATCH',
url: self.buildUrl(url),
body: postData,
headers: self.getAuthHeaders(),
json:true
}, function(err, res, body) {
callback(handleRequestError(err, body), body || {});
}
);
};

Packet.prototype._delete = function(url, callback) {
var self = this;
request(
{
method: 'DELETE',
url: self.buildUrl(url),
headers: self.getAuthHeaders(),
json:true
}, function(err, res, body) {
callback(handleRequestError(err, body), body || {});
}
);
};

Packet.prototype.getAuthHeaders = function() {
return {
'X-Auth-Token': this.apiKey,
'Content-Type': 'application/json',
'Accept': 'application/json'
};
};

Packet.prototype.buildUrl = function(url) {
return this.currentEnvironment + url;
};

Packet.prototype.setApiToken = function(token) {
this.apiKey = token;
};

function handleRequestError(err, body) {

if (body && (body.error || body.errors)) {
return new Error(body.errors || body.error);
} else {
return err;
}
}
};
})();
Loading

0 comments on commit 6e8753f

Please sign in to comment.