WiDirect API

From WiDirect
Jump to: navigation, search

WiDirect API

The WiDirect API allows customers to custom develop applications such as their own login and registration pages. The WiDirect will send login and registration requests directly to a URL on the customer's web server. It's then up to the customer to handle registration and billing. The customer interacts with the WiDirect via JSON api interface.

Also, customers can generate their own management web pages.

Enabling API

To Enable the API, the file /root/AWICP/etc/api-config.php must be modified. Set the enable to true and configure a unique username and password.

 <?php
    $api_enabled  = false;
    $api_username = "user";
    $api_password = "pass";
 ?>

You will use this username/password combination for all JSON calls to the WiDirect.

Commands Available

Note: these examples show calls all made with CURL (command line web page access program). A javascript example is included at the end

Get User

This uses the GET method and requires the userid. Here we are requesting User id 220:

  curl -i -X GET http://user:password@10.4.1.1/portal/api/api.php/users/217

Sample Result:

  {"userid":"217","username":"bob","firstname":"fname","lastname":"lname","email":null,"organization":"","address":"101 first street","city":"baltimore","state":"md","zip":"21222","membersince":"2013-05-17 13:09:52","registrationdate":"2013-05-17 13:09:52","modifieddate":null,"lastlogin":null,"notes":"","status":"3","connectionscount":"0","primarymac":""}

Remark: Password is not included in this general get. To get the password, see the getPassword API call.

Get All Users

   curl -i -X GET http://user:password@10.4.1.1/portal/api/api.php/users

Remark: Password is not included in this general get. To get the password, see the getPassword API call.

Delete User

There is no check to this command. Once deleted, the user is gone forever as well as the record of their existence. Use with care!

  curl -i -X DELETE http://user:password@10.4.1.1/portal/api/api.php/users/220

Modify User

This allows you to modify a user's attributes. You MUST include each element in the list or it will default to null. If you want to change a password, use the changePassword API command

  curl -i -X PUT -H 'Content-Type: application/json' -d '{"userid" : "217", "username" : "bob", "firstname" : "fname", "lastname" : "lname", "organization": "", "address" : "101 first street", "city": "baltimore", "state" : "md", "zip" : "21222", "notes" : "", "status":"1"}' http://user:password@10.4.1.1/portal/api/api.php/users/217

Remark: User id is included in the URL and in the json data. You must use it in both.

Add User

Creates a new user in the system. If the username is not unique, you will receive a error message. Include all elements listed in the example, or they will default to NULL

  curl -i -X POST -H 'Content-Type: application/json' -d '{ "username" : "bob2", "firstname" : "fname", "lastname" : "lname", "organization": "", "address" : "101 first street", "city": "baltimore", "state" : "md", "zip" : "21222", "notes" : "", "primarymac":"aa:bb:cc:dd:ee:ff", "status":"1"}' http://user:password@10.211.55.10/portal/api/api.php/users

List Plans

Lists all plans available on the system

   curl -i -X GET http://user:password@10.4.1.1/portal/api/api.php/plans

Get Password

Returns the user's password

  curl -i -X GET http://user:pass@10.211.55.10/portal/api/api.php/getpassword/213

Set Password

Allows you to change the password of a user

 curl -i -X PUT -H 'Content-Type: application/json' -d '{"password" : "newpassword", "userid": "213"}'  http://user:pass@10.4.1.1/portal/api/api.php/setpassword/213

Create Purchase

Every user needs an associated purchase entry. For a user to access the system, they need a plan (purchased even if it's free) added to their account. After you create a new user, you are going to need to call this function to assign a plan.

 curl -i -X POST -H 'Content-Type: application/json' -d '{ "userid" : "211", "planid" : "2" }' http://user:password@10.211.55.10/portal/api/api.php/purchases

Add User Example

Step 1: Create the User

      curl -i -X POST -H 'Content-Type: application/json' -d '{ "username" : "bob2", "firstname" : "fname", "lastname" : "lname", "organization": "", "address" : "101 first street", "city": "baltimore", "state" : "md", "zip" : "21222", "notes" : "", "status":"1"}' http://user:password@10.211.55.10/portal/api/api.php/users

Step 2: Retrieve UserID from the create - [In this case 222]

Step 3: Set the user's password

       curl -i -X PUT -H 'Content-Type: application/json' -d '{"password" : "newpassword",  "userid": "222"}'  http://user:pass@10.4.1.1/portal/api/api.php/setpassword/222

Step 4: Create a Purchase for the user

     curl -i -X POST -H 'Content-Type: application/json' -d '{ "userid" : "222", "planid" : "1" }' http://user:password@10.211.55.10/portal/api/api.php/purchases

Javascript Examples


function updateUser() {
   $.ajax({
       type: 'PUT',
       contentType: 'application/json',
       url: rootURL + '/' + $('#userid').val(),
       dataType: "json",
       data: formToJSON(),
       success: function(data, textStatus, jqXHR){
           alert('User updated successfully');
       },
       error: function(jqXHR, textStatus, errorThrown){
           alert('updateUser error: ' + textStatus);
       }
   });
}
 
function deleteUser() {
   console.log('deleteUser');
   $.ajax({
       type: 'DELETE',
       url: rootURL + '/' + $('#userid').val(),
       success: function(data, textStatus, jqXHR){
           alert('User deleted successfully');
       },
       error: function(jqXHR, textStatus, errorThrown){
           alert('deleteUser error');
       }
   });
}
 
 // Pull data from to form
function formToJSON() {
   return JSON.stringify({
       "userid": $('#userid').val(),
       "username": $('#username').val(),
       "address": $('#address').val(),
       "city": $('#city').val(),
       });
}