Please enable JavaScript to view this site.

Navigation: Advanced topics > Programming topics > PHPRunner's REST API

REST API: inRestApi

Scroll Prev Next More

 

Tells if the current request is executed as a part of REST API call

Syntax

inRestAPI();

Arguments

No arguments

Return value

Returns true if current request is executed via REST API, returns false otherwise.

Sample code

In this example we will show you how to implement a very basic rate limiting for your REST API. We would assume that limit is set as number of requests per user per month and users table has two additional fields: ratelimitquota (the current number of requests) and ratelimit (the max allowed number of requests). The following code goes to AfterSuccessfulLogin event.

 

 

if (inRestApi()) {
//Updating the quota for this user.
$UpdatedData = array();
$UpdatedData["ratelimitquota"] = $data["ratelimitquota"] + 1;
DB::Update("users", $UpdatedData, "username = '" . $username . "'" );
if ( $UpdatedData["ratelimitquota"] > $data["ratelimit"] )
{
       API::sendError( 'Too many requests', 429 );
}
}

 

 

Created with Help+Manual 7 and styled with Premium Pack Version 3 © by EC Software