Prev Next

HandleRequest

Inputs

method

  • C++: const char*
  • C#: string

The name of the method to perform. This allows the plugin to make a choice on what action to perform without having to parse the parameters string fully first.

parameters

  • C++: const char*
  • C#: string

A JSON string of parameters. See Details for more information about available parameters.

Outputs via Callbacks

[Optional] Result, LogMessage or SetError - to provide user feedback.

Details

This is the main function to the plug-in. It should handle all incoming 'method' requests and perform the action required, and return any data via the 'Result' callback.

Example Implementation

void ExampleServicePlugin::HandleRequest(const char* method, const char* parameters)

{

    LogMessage(LOG_TRACE, std::string(__FUNCTION__ + " - Method = "s + method).c_str());

    // This example method demonstrates how to extract various parameter types from parameters.

    // This is done here with jsoncpp library but can be done with any compliant JSON library.

    Json::Value jsonParameters;

    if (strlen(parameters))

    {

        std::stringstream(parameters) >> jsonParameters;

    }

    if (std::string(method) == "DoSomething")

    {

        int myNumber = jsonParameters["myNumber"].asInt();

        double myFloat = jsonParameters["myFloat"].asDouble();

        std::string myString = jsonParameters["myString"].asString();

        std::list<int> myArrayOfNumbers;

        for (auto& myValue : jsonParameters["myArrayOfNumbers"])

        {

            myArrayOfNumbers.push_back(myValue.asInt());

        }

        std::list<std::string> myArrayOfStrings;

        for (auto& myValue : jsonParameters["myArrayOfStrings"])

        {

            myArrayOfStrings.push_back(myValue.asString());

        }

        std::string result = "Example User SBPI Service Plugin in C++. DoSomething received parameters: myNumber = " + std::to_string(myNumber)

            + ", myfloat = " + std::to_string(myFloat)

            + ", myString = " + myString;

        // Set the result string.

        Result(result.c_str());

    }

    else if (std::string(method) == "DoSomethingToElement")

    {

        // This example method demonstrates how to respond to a user performing a task on a single element.

        std::string elementID = jsonParameters["elementGUID"].asString();

        Result(std::string("Example User SBPI Plugin in C++. DoSomething to element with GUID: " + elementID).c_str());

    }

    else

    {

        // Set an error string

        SetError(std::string("Unknown method: " + std::string(method)).c_str());

    }

}