API Usage

The API server allows you to interact with the application via HTTP GET requests. This enables you to calculate paths, compare algorithms, and manage settings programmatically.

Starting the API Server

Once you run the macLinuxProjectBuilder.sh or windowsProjectBuilder.bat script, the API server starts automatically. The terminal will display a message similar to this:

API server running on port <port>
Try the longest path -> http://localhost:<port>/path?start=9588784&end=2720178

This confirms that the server is ready to accept requests. The default port is set in the application but can be modified using the CLI command port.


API Endpoints

The following endpoints are available for interacting with the server:

Basic Shortest Path

  • Endpoint: /path

  • Description: Calculates the shortest path between two nodes using A*.

  • Parameters:

    • start (integer): The starting node.

    • end (integer): The ending node.

    • format (optional): Response format (json or xml).

  • Example:

    GET http://localhost:<port>/path?start=1&end=5

    Response (JSON):

    {
      "path": [1, 3, 5],
      "distance": 12.5
    }

Debug Path

  • Endpoint: /debug_path

  • Description: Provides detailed information about the calculated path, including heuristic weight, computation time, and more.

  • Parameters: Same as /path.

  • Example:

    GET http://localhost:<port>/debug_path?start=1&end=5

    Response (JSON):

    {
      "path": [1, 3, 5],
      "distance": 12.5,
      "time_ms": 1.2,
      "heuristic_weight": 1.5
    }

Comparator Path

  • Endpoint: /comp_path

  • Description: Compares the shortest paths calculated by A* and Dijkstra algorithms.

  • Parameters: Same as /path.

  • Example:

    GET http://localhost:<port>/comp_path?start=1&end=5

    Response (JSON):

    {
      "a_star": {
        "path": [1, 3, 5],
        "distance": 12.5,
        "time_ms": 1.2
      },
      "dijkstra": {
        "path": [1, 3, 5],
        "distance": 12.5,
        "time_ms": 1.8
      }
    }

Command Endpoint

  • Endpoint: /cmd

  • Description: Sends a command to the application via the API.

  • Parameters:

    • command (string): The CLI command to execute.

    • format (optional): Response format (json or xml).

  • Example:

    GET http://localhost:<port>/cmd?command=build%20graph

Response Formats

You can request responses in either JSON or XML by appending the format parameter to the query string:

  • JSON (default):

    GET http://localhost:<port>/path?start=1&end=5&format=json
  • XML:

    GET http://localhost:<port>/path?start=1&end=5&format=xml

Stopping the API Server

The API server stops when you exit the CLI. To stop the application:

  • Type exit in the terminal.

Enter a command or the start node: exit
Application terminated.

Last updated