We are planning to fetch the data using API instead of SQL query do you have any examples.
Hi @ibrahim.k_Ibrahim, I’ve grabbed this directly from the developer.morpheusdata.com docs here. This shows an example in [any] plugin type utilizing a REST call:
HTTP API Client
The morpheus-plugin-api
library comes with a utility for facilitating quick and easy API calls to external integrations. This is called teh HttpApiClient
and replaces the use of the RestApiUtil
.
The developer is, of course, able to utilize any HTTP or SDK/API client they choose within the java ecosystem if so desired. However, this client provides some common functions such as SSL Validation, Throttling, Keep-Alive, Etc. and is based on the Apache HTTP Client.
TIP Reuse the same client instance when dealing with periodic refresh methods related to caching for best performance. Be mindful that throttling might be necessary to slow down the calls to the service.
Example
Below is an example API Call taken from the Infoblox
plugin.
import com.morpheusdata.core.util.HttpApiClient
HttpApiClient infobloxClient = new HttpApiClient()
try {
def results = infobloxClient.callJsonApi(serviceUrl, apiPath, poolServer.serviceUsername, poolServer.servicePassword, new HttpApiClient.RequestOptions(headers:['Content-Type':'application/json'],
queryParams:pageQuery, ignoreSSL: poolServer.ignoreSsl), 'GET')
} catch(e) {
log.error("verifyPoolServer error: ${e}", e)
} finally {
infobloxClient.shutdownClient()
}
return rtn
There are several methods to call the api and automatically handle certain payload formats such as callJsonApi
, callXmlApi
, or plain callApi
.
Options can be passed relating to the request via the HttpApiClient.RequestOptions
object. Please refer to the java api doc for available options.
TIP Remember to always shutdown the client after it is used to clean up the connection manager in a try{} finally{}
type block
Thanks for the details. I am planning to retrieve the few information using API and show it in the custom report. In that document I didn’t see any details for renderer maping.
Below is the example code:
import groovy.json.JsonSlurper
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.OutputStream;
import java.util.Base64.Encoder
// Create a trust manager that does not validate certificate chains like the default
TrustManager trustAllCerts = new TrustManager{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers()
{
return null;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
{
//No need to implement.
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
{
//No need to implement.
}
}
};
// Install the all-trusting trust manager
try
{
SSLContext sc = SSLContext.getInstance(“SSL”);
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
catch (Exception e)
{
System.out.println(e);
}
def message = ‘{“username”:“eve.holt@reqres.in”, “password”:“cityslicka”}’
def post = new URL(“https://reqres.in/api/login”).openConnection();
post.setRequestMethod(“POST”)
post.setDoOutput(true)
post.setRequestProperty(“Content-Type”, “application/json”)
post.getOutputStream().write(message.getBytes(“UTF-8”))
def postRC = post.getResponseCode()
def results = post.getInputStream().getText()
println(results)
def slurper = new groovy.json.JsonSlurper()
def json = slurper.parseText(results)
println(json)
It will return the token I wants to store and return the value in renderer.hbs