Page 1 of 1

Call an external rest api inside a crontab script

PostPosted:Mon Dec 06, 2021 8:24 am
by mamad
Hi guys
Im using openkm 6.3 CE
I want to perform a get request to an external rest web service with basic authentication in a crontab script to get some info from that web service on a scheduled plan and do some changes in openkm files and folders.
Does openkm source has any utility class or method that can be used in crontab scripts for actions like calling a web service, parse and decode the json entries and ... or i have to write all the required methods by myself ?
If the answer is no, how can i use external libraries like jackson in a cronjob script ? I mean, where i have to put the jars ? Do i need to define the libraries in pom.xml file and recompile the openkm source ?
Thank you in advance

Re: Call an external rest api inside a crontab script

PostPosted:Sat Dec 11, 2021 8:39 am
by jllort
In the professional edition, we have a RestClient client class for this matter but in CE we do not have it. You should implement the client, take the sample code below as a guide of what are you looking for.
Code: Select all
import okhttp3.*;
....

/**
	 * GET
	 */
	public String get(String uri, String format) throws RestClientException, IOException {
		log.debug("get({}, {})", uri, format);

		// Authentication
		OkHttpClient client = getHttpClient(); // here is build the client with credentials etc... 

		// Request
		Request request = new Request.Builder().url(uri).build();
		Response response = client.newCall(request).execute();

		return handleResponse(response);
	}
The maven dependency should be something like:
Code: Select all
<dependency>
      <groupId>com.squareup.okhttp3</groupId>
      <artifactId>okhttp</artifactId>
      <version>4.9.1</version>
    </dependency>
   <dependency>
      <groupId>commons-httpclient</groupId>
      <artifactId>commons-httpclient</artifactId>
      <version>3.1</version>
      <exclusions>
        <exclusion>
          <groupId>commons-codec</groupId>
          <artifactId>commons-codec</artifactId>
        </exclusion>
        <exclusion>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

Re: Call an external rest api inside a crontab script

PostPosted:Sat Dec 11, 2021 10:33 am
by mamad
Well, thank you so much jllort