API Delphi Client

From MikroTik Wiki
Revision as of 21:38, 1 May 2009 by Chupaka (talk | contribs) (TRosApiClient)
Jump to: navigation, search

This is implementation of MikroTik RouterOS API Client for Delphi. It supports execution of parallel requests to router and has database-like interface for easy use.

Classes

RouterOSAPI unit contains definition of two classes which you need to work with API protocol from your Delphi programs.

TRosApiClient

This class encapsulates properties and methods to make a connection to router via RouterOS API protocol.


  • function Connect(Hostname, Username, Password: String; Port: String = '8728'): Boolean;

This function connects to the router and performs login procedure. It returns True if login was successful, False otherwise.


  • function Query(Request: array of String; GetAllAfterQuery: Boolean): TROSAPIResult;

Makes a query to the router. Request is array of string, first one being the command and others are parameters. If GetAllAfterQuery is True, then TROSAPIResult.GetAll is executed after sending a query.


  • function Execute(Request: array of String): Boolean;

If you do not need to receive any output from your query, use this method. It simply calls Query function and frees returned object.


  • property Timeout: Integer;

With this property you can set timeout value for network operations (in milliseconds).


  • property LastError: String;

This read-only property contains textual description of last error occured.


  • procedure Disconnect;

Disconnects from the router.

TRosApiResult

Examples

Creating connection to router

At first, we should declare a variable and create an instance of TRosApiClient:

var
  RouterOS: TRosApiClient;

RouterOS := TRosApiClient.Create;

Now we connect to router and perform login procedure:

if RouterOS.Connect('192.168.0.1', 'admin', 'password') then
begin
  //we are connected successfully
end
else
begin
  //an error occured; text error message is in LastError property
end;

Executing queries

All queries are done by calling Query function of TRosApiClient. It returns an instance of TRosApiResult, from which all data are fetched.

var
  Res: TRosApiResult;

Res := RouterOS.Query(['/system/resource/print'], True);

Downloads and suggestions

For downloads and suggestions see forum thread RouterOS API Delphi Client