REST API Usage – Arma Reforger
Jump to navigation
Jump to search
Lou Montana (talk | contribs) m (Fix data unit) |
Lou Montana (talk | contribs) m (Remove after class' semicolons) |
||
(3 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
This page explains the terminology and how to use the scripting API. | This page explains the terminology and how to use the scripting API. | ||
{{Feature|informative|See also the {{ | {{Feature|informative|See also the {{Link|https://en.wikipedia.org/wiki/Representational_state_transfer}} Wikipedia article.}} | ||
{{Feature|important| | {{Feature|important| | ||
The REST API is only meant to provide basic REST functionality (GET and POST methods - see {{ | The REST API is only meant to provide basic REST functionality (GET and POST methods - see {{Link|https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods|HTTP request methods}}), ''not'' full server functionality (like e.g {{Link|https://curl.se/|curl}}). | ||
* Data size accepted by the API has to be '''below 1 MB''' | * Data size accepted by the API has to be '''below 1 MB''' | ||
* There is no support for custom headers | * There is no support for custom headers | ||
Line 50: | Line 50: | ||
{ | { | ||
Print("OnError()"); | Print("OnError()"); | ||
} | } | ||
//------------------------------------------------------------------------------------------------ | //------------------------------------------------------------------------------------------------ | ||
Line 56: | Line 56: | ||
{ | { | ||
Print("OnTimeout()"); | Print("OnTimeout()"); | ||
} | } | ||
//------------------------------------------------------------------------------------------------ | //------------------------------------------------------------------------------------------------ | ||
Line 64: | Line 64: | ||
if (dataSize > 0) | if (dataSize > 0) | ||
Print(data); // note that Print() will not output strings longer than 1024b to console, check the dataSize! | Print(data); // note that Print() will not output strings longer than 1024b to console, check the dataSize! | ||
} | } | ||
} | } | ||
class HttpElement | class HttpElement | ||
Line 87: | Line 87: | ||
// ctx.GET(m_callbackExample, "get?x=10&y=5"); | // ctx.GET(m_callbackExample, "get?x=10&y=5"); | ||
} | } | ||
} | } | ||
</enforce> | </enforce> | ||
Latest revision as of 11:23, 19 May 2023
The REST API (RestApi) is a simplified way to call REST requests and handle results/data, allowing to communicate with a web API. This page explains the terminology and how to use the scripting API.
Definitions
Context
The context is simply the website URL with which transactions will happen, e.g https://httpbin.org/.
A RestContext context can be created with the following:
Callback
A callback is a script class handling a request's success, error or timeout.
Once the request happens (asynchronously) the callback methods are called to process the received result.
The scripter is responsible for the callback object's lifetime. Inherit from RestCallback and implement the desired methods to create a custom callback:
Example
Declaration
class RestCallbackExample : RestCallback
{
//------------------------------------------------------------------------------------------------
override void OnError(int errorCode)
{
Print("OnError()");
}
//------------------------------------------------------------------------------------------------
override void OnTimeout()
{
Print("OnTimeout()");
}
//------------------------------------------------------------------------------------------------
override void OnSuccess(string data, int dataSize)
{
Print("OnSuccess() - data size = " + dataSize + " bytes");
if (dataSize > 0)
Print(data); // note that Print() will not output strings longer than 1024b to console, check the dataSize!
}
}
class HttpElement
{
protected ref RestCallbackExample m_callbackExample;
//------------------------------------------------------------------------------------------------
void ExecuteRequest()
{
if (!m_callbackExample)
m_callbackExample = new RestCallbackExample();
string contextURL = "https://httpbin.org/";
RestContext context = GetGame().GetRestApi().GetContext(contextURL);
// executed request is "get" in "https://httpbin.org/" context (i.e "https://httpbin.org/get")
// using the HTTP's GET method
context.GET(m_callbackExample, "get");
// it is possible to assemble a more complex request using arguments, like this:
// ctx.GET(m_callbackExample, "get?x=10&y=5");
}
}
Call
class HolderClass
{
protected ref HttpElement m_HttpElement;
void PerformGETRequest()
{
if (!m_HttpElement)
m_HttpElement = new HttpElement();
m_HttpElement.ExecuteRequest();
}
}