Skip to content

Instantly share code, notes, and snippets.

@MikuAuahDark
Last active August 5, 2017 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikuAuahDark/a9848bb2c35a336ff56c2ee7f3a1e404 to your computer and use it in GitHub Desktop.
Save MikuAuahDark/a9848bb2c35a336ff56c2ee7f3a1e404 to your computer and use it in GitHub Desktop.
NPPS3 module action handler example.
<?php
// Example NPPS3 module action handler
return new class implements NPPS\ActionHandler
{
// You can add your variables in here.
private $YourVariables;
// Constructor must be public. One-time initialization can be done here.
public function __construct() {}
// Destructor must be public too. Do cleanup in here
public function __destruct() {}
// Access level (permission) of the module action handler.
public function Permissions(): int
{
// These permissions can be combined with bitwise OR
// * NPPS_PERMISSION_NOMULTI - This module can't be accessed under /api
// * NPPS_PERMISSION_NOXMC - X-Message-Code header is not needed for this
// One of these access level can be specificed. You can use bitwise OR to combine
// access level and permissions.
// * NPPS_ACCESSLEVEL_UNAUTHENTICATED - Anyone can access this, even without token
// * NPPS_ACCESSLEVEL_FIRSTSTEPTOKEN - This action only can be accessed with firststep token (authkey)
// * NPPS_ACCESSLEVEL_AUTHENTICATED - This action only can be accessed when authenticated (login)
return
NPPS_ACCESSLEVEL_UNAUTHENTICATED | // Anyone can access this, even without token
NPPS_PERMISSION_NOMULTI | // This module can't be accessed under /api
NPPS_PERMISSION_NOXMC; // X-Message-Code header is not needed for this
}
// Expected JSON parameters. Used for parameter checking. Empty array means no arguments
public function Parameters(): array
{
// Available datatypes. Combine with bitwise OR
// * NPPS_DATATYPE_NULL - NULL value. Meant to be bitwise OR'd
// * NPPS_DATATYPE_NUMBER - Int and float
// * NPPS_DATATYPE_STRING - String. This includes empty string
// * NPPS_DATATYPE_ARRAY - Array. Additionally, using empty array will work too. Array values meant
// to be used for nested arguments.
// * NPPS_DATATYPE_BOOLEAN - true or false.
// Example: unit_id must be number, unit_name must be string, and inside data array, hello must be string (can be null).
return [
'unit_id' => NPPS_DATATYPE_NUMBER,
'unit_name' => NPPS_DATATYPE_STRING,
'data' => [
'hello' => NPPS_DATATYPE_STRING | NPPS_DATATYPE_NULL
]
];
}
// Execute/handler function. The action code starts here.
public function Execute(array $request_data): NPPS\ActionHandlerResult
{
if(!empty($request_data['data']['hello']) && strcasecmp($request_data['data']['hello'], 'world') == 0)
// NPPS\ActionResponse class is used for successful response. "new" keyword is optional.
return new NPPS\ActionResponse(['hello' => 'NPPS3']);
else
// NPPS\ActionError class is used for errors. Like, event ranking is unavailable. "new" keyword is optional.
return new NPPS\ActionError(ERROR_CODE_UNAVAILABLE_WORDS);
end
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment