Use FenixEdu API in your application - Java SDK
In this tutorial, you will learn how to use the Java SDK to easily invoke the FenixEdu REST API. Make sure you started by following the tutorial that teaches you how to use FenixEdu in your application.
Table of Contents
- Step 1 - Include the Java SDK in your project libs
- Step 2 - Define your Credentials
- Step 3 - Use the Client
Step 1 - Include the Java SDK in your project libs
In order to ease the use of the FenixEdu API, we developed a Java SDK that you can use in your project by including it in your project libraries.
If you are using Maven to build your Java project, you just need to add the following dependency to your pom.xml
file:
In order for Maven to know where to get this dependency from, you will also need to include our Maven repository in the repositories element of your pom.xml
file:
Step 2 - Define your Credentials
The next step is to create a file named fenixedu.properties
in src/main/resources
and specify both your Consumer Key and Secret:
The library that you included in the previous step will attempt to read this file and auto-config your credentials that will be used in every API call in order to authorize your requests.
Step 3 - Use the Client
When using the client to invoke FenixEdu API endpoints, you should decide either you want to make synchronous invocations, where the invocation is blocked while waiting for the response from the server, or asynchronous, where the invocation returns void instantly and you receive the response through the means of a callback mechanism.
Step 3.1 - Use the Synchronous Client
After you configured your credentials, you can make synchronous invocations to the API like in the following example:
The above example instantiates a new client and invokes a request to retrieve information about the user that gave authorization to your application.
Step 3.2 - Use the Asynchronous Client
If your working mobile (in this example we are using Android), you probably want to use the Asynchronous client. The first thing to do is change your Maven dependency:
Although the authorization part is similar to the synchronous client, the result of the invocation is obtained through the execute of an AsyncTask, which you must be created passing the client:
Attention The asynchronous invocation of the API is made within a new thread. If the main process finishes before the response arrives, the thread will also die before you can process the response. Be sure that the process where the invocation is made continues to live, either through the context of execution (e.g. Android application lifecyle) or through a lock mechanism.